forked from sumatrapdfreader/sumatrapdf
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStressTesting.cpp
More file actions
898 lines (766 loc) · 27.7 KB
/
Copy pathStressTesting.cpp
File metadata and controls
898 lines (766 loc) · 27.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
/* Copyright 2018 the SumatraPDF project authors (see AUTHORS file).
License: GPLv3 */
#include "BaseUtil.h"
#include "ScopedWin.h"
#include "DirIter.h"
#include "FileUtil.h"
#include "HtmlParserLookup.h"
#include "HtmlWindow.h"
#include "Mui.h"
#include "SimpleLog.h"
#include "Timer.h"
#include "WinUtil.h"
#include "BaseEngine.h"
#include "EngineManager.h"
#include "EbookBase.h"
#include "HtmlFormatter.h"
#include "EbookFormatter.h"
#include "Doc.h"
#include "SettingsStructs.h"
#include "Controller.h"
#include "ChmModel.h"
#include "DisplayModel.h"
#include "EbookController.h"
#include "GlobalPrefs.h"
#include "RenderCache.h"
#include "TextSelection.h"
#include "TextSearch.h"
#include "SumatraPDF.h"
#include "WindowInfo.h"
#include "TabInfo.h"
#include "AppTools.h"
#include "ParseCommandLine.h"
#include "Search.h"
#include "StressTesting.h"
#define FIRST_STRESS_TIMER_ID 101
static slog::Logger* gLog;
#define logbench(msg, ...) gLog->LogFmt(msg, __VA_ARGS__)
static bool gIsStressTesting = false;
static int gCurrStressTimerId = FIRST_STRESS_TIMER_ID;
bool IsStressTesting() {
return gIsStressTesting;
}
static bool IsInRange(Vec<PageRange>& ranges, int pageNo) {
for (size_t i = 0; i < ranges.size(); i++) {
if (ranges.at(i).start <= pageNo && pageNo <= ranges.at(i).end)
return true;
}
return false;
}
static void BenchLoadRender(BaseEngine* engine, int pagenum) {
Timer t;
bool ok = engine->BenchLoadPage(pagenum);
t.Stop();
if (!ok) {
logbench(L"Error: failed to load page %d", pagenum);
return;
}
double timeMs = t.GetTimeInMs();
logbench(L"pageload %3d: %.2f ms", pagenum, timeMs);
t.Start();
RenderedBitmap* rendered = engine->RenderBitmap(pagenum, 1.0, 0);
t.Stop();
if (!rendered) {
logbench(L"Error: failed to render page %d", pagenum);
return;
}
delete rendered;
timeMs = t.GetTimeInMs();
logbench(L"pagerender %3d: %.2f ms", pagenum, timeMs);
}
static int FormatWholeDoc(Doc& doc) {
int PAGE_DX = 640;
int PAGE_DY = 520;
PoolAllocator textAllocator;
HtmlFormatterArgs* formatterArgs = CreateFormatterArgsDoc(doc, PAGE_DX, PAGE_DY, &textAllocator);
HtmlFormatter* formatter = doc.CreateFormatter(formatterArgs);
int nPages = 0;
for (HtmlPage* pd = formatter->Next(); pd; pd = formatter->Next()) {
delete pd;
++nPages;
}
delete formatterArgs;
delete formatter;
return nPages;
}
static int TimeOneMethod(Doc& doc, TextRenderMethod method, const WCHAR* methodName) {
SetTextRenderMethod(method);
Timer t;
int nPages = FormatWholeDoc(doc);
double timesms = t.Stop();
logbench(L"%s: %.2f ms", methodName, timesms);
return nPages;
}
// this is to compare the time it takes to layout a whole ebook file
// using different text measurement method (since the time is mostly
// dominated by text measure)
void BenchEbookLayout(const WCHAR* filePath) {
bool deleteLog = false;
if (!gLog) {
gLog = new slog::StderrLogger();
deleteLog = true;
}
logbench(L"Starting: %s", filePath);
if (!file::Exists(filePath)) {
logbench(L"Error: file doesn't exist");
return;
}
if (!Doc::IsSupportedFile(filePath)) {
logbench(L"Error: not an ebook file");
return;
}
Timer t;
Doc doc = Doc::CreateFromFile(filePath);
if (doc.LoadingFailed()) {
logbench(L"Error: failed to load the file as doc");
doc.Delete();
return;
}
double timeMs = t.Stop();
logbench(L"load: %.2f ms", timeMs);
int nPages = TimeOneMethod(doc, TextRenderMethodGdi, L"gdi ");
TimeOneMethod(doc, TextRenderMethodGdiplus, L"gdi+ ");
TimeOneMethod(doc, TextRenderMethodGdiplusQuick, L"gdi+ quick");
// do it twice because the first run is very unfair to the first version that runs
// (probably because of font caching)
TimeOneMethod(doc, TextRenderMethodGdi, L"gdi ");
TimeOneMethod(doc, TextRenderMethodGdiplus, L"gdi+ ");
TimeOneMethod(doc, TextRenderMethodGdiplusQuick, L"gdi+ quick");
doc.Delete();
logbench(L"pages: %d", nPages);
if (deleteLog) {
delete gLog;
}
}
static void BenchChmLoadOnly(const WCHAR* filePath) {
Timer total;
logbench(L"Starting: %s", filePath);
Timer t;
ChmModel* chmModel = ChmModel::Create(filePath, nullptr);
if (!chmModel) {
logbench(L"Error: failed to load %s", filePath);
return;
}
double timeMs = t.Stop();
logbench(L"load: %.2f ms", timeMs);
delete chmModel;
total.Stop();
logbench(L"Finished (in %.2f ms): %s", total.GetTimeInMs(), filePath);
}
static void BenchFile(const WCHAR* filePath, const WCHAR* pagesSpec) {
if (!file::Exists(filePath)) {
return;
}
// ad-hoc: if enabled times layout instead of rendering and does layout
// using all text rendering methods, so that we can compare and find
// docs that take a long time to load
if (Doc::IsSupportedFile(filePath) && !gGlobalPrefs->ebookUI.useFixedPageUI) {
BenchEbookLayout(filePath);
return;
}
if (ChmModel::IsSupportedFile(filePath) && !gGlobalPrefs->chmUI.useFixedPageUI) {
BenchChmLoadOnly(filePath);
return;
}
Timer total;
logbench(L"Starting: %s", filePath);
Timer t;
BaseEngine* engine = EngineManager::CreateEngine(filePath);
if (!engine) {
logbench(L"Error: failed to load %s", filePath);
return;
}
double timeMs = t.Stop();
logbench(L"load: %.2f ms", timeMs);
int pages = engine->PageCount();
logbench(L"page count: %d", pages);
if (nullptr == pagesSpec) {
for (int i = 1; i <= pages; i++) {
BenchLoadRender(engine, i);
}
}
AssertCrash(!pagesSpec || IsBenchPagesInfo(pagesSpec));
Vec<PageRange> ranges;
if (ParsePageRanges(pagesSpec, ranges)) {
for (size_t i = 0; i < ranges.size(); i++) {
for (int j = ranges.at(i).start; j <= ranges.at(i).end; j++) {
if (1 <= j && j <= pages)
BenchLoadRender(engine, j);
}
}
}
delete engine;
total.Stop();
logbench(L"Finished (in %.2f ms): %s", total.GetTimeInMs(), filePath);
}
static bool IsFileToBench(const WCHAR* fileName) {
if (EngineManager::IsSupportedFile(fileName))
return true;
if (Doc::IsSupportedFile(fileName))
return true;
return false;
}
static void CollectFilesToBench(WCHAR* dir, WStrVec& files) {
DirIter di(dir, true /* recursive */);
for (const WCHAR* filePath = di.First(); filePath; filePath = di.Next()) {
if (IsFileToBench(filePath)) {
files.Append(str::Dup(filePath));
}
}
}
static void BenchDir(WCHAR* dir) {
WStrVec files;
CollectFilesToBench(dir, files);
for (size_t i = 0; i < files.size(); i++) {
BenchFile(files.at(i), nullptr);
}
}
void BenchFileOrDir(WStrVec& pathsToBench) {
gLog = new slog::StderrLogger();
size_t n = pathsToBench.size() / 2;
for (size_t i = 0; i < n; i++) {
WCHAR* path = pathsToBench.at(2 * i);
if (file::Exists(path))
BenchFile(path, pathsToBench.at(2 * i + 1));
else if (dir::Exists(path))
BenchDir(path);
else
logbench(L"Error: file or dir %s doesn't exist", path);
}
delete gLog;
}
inline bool IsSpecialDir(const WCHAR* s) {
return str::Eq(s, L".") || str::Eq(s, L"..");
}
static bool IsStressTestSupportedFile(const WCHAR* filePath, const WCHAR* filter) {
if (filter && !path::Match(path::GetBaseName(filePath), filter))
return false;
if (EngineManager::IsSupportedFile(filePath) || Doc::IsSupportedFile(filePath))
return true;
if (!filter)
return false;
// sniff the file's content if it matches the filter but
// doesn't have a known extension
return EngineManager::IsSupportedFile(filePath, true) || Doc::IsSupportedFile(filePath, true);
}
static bool CollectStressTestSupportedFilesFromDirectory(const WCHAR* dirPath, const WCHAR* filter, WStrVec& paths) {
bool hasFiles = false;
DirIter di(dirPath);
for (const WCHAR* filePath = di.First(); filePath; filePath = di.Next()) {
if (IsStressTestSupportedFile(filePath, filter)) {
paths.Append(str::Dup(filePath));
hasFiles = true;
}
}
return hasFiles;
}
// return t1 - t2 in seconds
static int SystemTimeDiffInSecs(SYSTEMTIME& t1, SYSTEMTIME& t2) {
FILETIME ft1, ft2;
SystemTimeToFileTime(&t1, &ft1);
SystemTimeToFileTime(&t2, &ft2);
return FileTimeDiffInSecs(ft1, ft2);
}
static int SecsSinceSystemTime(SYSTEMTIME& time) {
SYSTEMTIME currTime;
GetSystemTime(&currTime);
return SystemTimeDiffInSecs(currTime, time);
}
static WCHAR* FormatTime(int totalSecs) {
int secs = totalSecs % 60;
int totalMins = totalSecs / 60;
int mins = totalMins % 60;
int hrs = totalMins / 60;
if (hrs > 0)
return str::Format(L"%d hrs %d mins %d secs", hrs, mins, secs);
if (mins > 0)
return str::Format(L"%d mins %d secs", mins, secs);
return str::Format(L"%d secs", secs);
}
static void FormatTime(int totalSecs, str::Str<char>* s) {
int secs = totalSecs % 60;
int totalMins = totalSecs / 60;
int mins = totalMins % 60;
int hrs = totalMins / 60;
if (hrs > 0)
s->AppendFmt("%d hrs %d mins %d secs", hrs, mins, secs);
if (mins > 0)
s->AppendFmt("%d mins %d secs", mins, secs);
s->AppendFmt("%d secs", secs);
}
static void MakeRandomSelection(WindowInfo* win, int pageNo) {
DisplayModel* dm = win->AsFixed();
if (!dm->ValidPageNo(pageNo))
pageNo = 1;
if (!dm->ValidPageNo(pageNo))
return;
// try a random position in the page
int x = rand() % 640;
int y = rand() % 480;
if (dm->textSelection->IsOverGlyph(pageNo, x, y)) {
dm->textSelection->StartAt(pageNo, x, y);
dm->textSelection->SelectUpTo(pageNo, rand() % 640, rand() % 480);
}
}
// encapsulates the logic of getting the next file to test, so
// that we can implement different strategies
class TestFileProvider {
public:
virtual ~TestFileProvider() {}
// returns path of the next file to test or nullptr if done (caller needs to free() the result)
virtual WCHAR* NextFile() = 0;
// start the iteration from the beginning
virtual void Restart() = 0;
};
class FilesProvider : public TestFileProvider {
WStrVec files;
size_t provided;
public:
explicit FilesProvider(const WCHAR* path) {
files.Append(str::Dup(path));
provided = 0;
}
FilesProvider(WStrVec& newFiles, int n, int offset) {
// get every n-th file starting at offset
for (size_t i = offset; i < newFiles.size(); i += n) {
const WCHAR* f = newFiles.at(i);
files.Append(str::Dup(f));
}
provided = 0;
}
virtual ~FilesProvider() {}
virtual WCHAR* NextFile() {
if (provided >= files.size())
return nullptr;
return str::Dup(files.at(provided++));
}
virtual void Restart() { provided = 0; }
};
class DirFileProvider : public TestFileProvider {
AutoFreeW startDir;
AutoFreeW fileFilter;
// current state of directory traversal
WStrVec filesToOpen;
WStrVec dirsToVisit;
bool OpenDir(const WCHAR* dirPath);
public:
DirFileProvider(const WCHAR* path, const WCHAR* filter);
virtual ~DirFileProvider();
virtual WCHAR* NextFile();
virtual void Restart();
};
DirFileProvider::DirFileProvider(const WCHAR* path, const WCHAR* filter) {
startDir.SetCopy(path);
if (filter && !str::Eq(filter, L"*"))
fileFilter.SetCopy(filter);
OpenDir(path);
}
DirFileProvider::~DirFileProvider() {}
bool DirFileProvider::OpenDir(const WCHAR* dirPath) {
AssertCrash(filesToOpen.size() == 0);
bool hasFiles = CollectStressTestSupportedFilesFromDirectory(dirPath, fileFilter, filesToOpen);
filesToOpen.SortNatural();
AutoFreeW pattern(str::Format(L"%s\\*", dirPath));
bool hasSubDirs = CollectPathsFromDirectory(pattern, dirsToVisit, true);
return hasFiles || hasSubDirs;
}
WCHAR* DirFileProvider::NextFile() {
if (filesToOpen.size() > 0) {
return filesToOpen.PopAt(0);
}
if (dirsToVisit.size() > 0) {
// test next directory
AutoFreeW path(dirsToVisit.PopAt(0));
OpenDir(path);
return NextFile();
}
return nullptr;
}
void DirFileProvider::Restart() {
OpenDir(startDir);
}
static size_t GetAllMatchingFiles(const WCHAR* dir, const WCHAR* filter, WStrVec& files, bool showProgress) {
WStrVec dirsToVisit;
dirsToVisit.Append(str::Dup(dir));
while (dirsToVisit.size() > 0) {
if (showProgress) {
wprintf(L".");
fflush(stdout);
}
AutoFreeW path(dirsToVisit.PopAt(0));
CollectStressTestSupportedFilesFromDirectory(path, filter, files);
AutoFreeW pattern(str::Format(L"%s\\*", path));
CollectPathsFromDirectory(pattern, dirsToVisit, true);
}
return files.size();
}
/* The idea of StressTest is to render a lot of PDFs sequentially, simulating
a human advancing one page at a time. This is mostly to run through a large number
of PDFs before a release to make sure we're crash proof. */
class StressTest {
WindowInfo* win;
Timer currPageRenderTime;
int currPage;
int pageForSearchStart;
int filesCount; // number of files processed so far
int timerId;
bool exitWhenDone;
SYSTEMTIME stressStartTime;
int cycles;
Vec<PageRange> pageRanges;
// range of files to render (files get a new index when going through several cycles)
Vec<PageRange> fileRanges;
int fileIndex;
// owned by StressTest
TestFileProvider* fileProvider;
bool OpenFile(const WCHAR* fileName);
bool GoToNextPage();
bool GoToNextFile();
void TickTimer();
void Finished(bool success);
public:
StressTest(WindowInfo* win, bool exitWhenDone)
: win(win),
currPage(0),
pageForSearchStart(0),
filesCount(0),
cycles(1),
fileIndex(0),
fileProvider(nullptr),
exitWhenDone(exitWhenDone) {
timerId = gCurrStressTimerId++;
}
~StressTest() { delete fileProvider; }
void Start(const WCHAR* path, const WCHAR* filter, const WCHAR* ranges, int cycles);
void Start(TestFileProvider* fileProvider, int cycles);
void OnTimer(int timerIdGot);
void GetLogInfo(str::Str<char>* s);
};
void StressTest::Start(TestFileProvider* fileProvider, int cycles) {
GetSystemTime(&stressStartTime);
this->fileProvider = fileProvider;
this->cycles = cycles;
if (pageRanges.size() == 0)
pageRanges.Append(PageRange());
if (fileRanges.size() == 0)
fileRanges.Append(PageRange());
TickTimer();
}
void StressTest::Start(const WCHAR* path, const WCHAR* filter, const WCHAR* ranges, int cycles) {
if (file::Exists(path)) {
FilesProvider* filesProvider = new FilesProvider(path);
ParsePageRanges(ranges, pageRanges);
Start(filesProvider, cycles);
} else if (dir::Exists(path)) {
DirFileProvider* dirFileProvider = new DirFileProvider(path, filter);
ParsePageRanges(ranges, fileRanges);
Start(dirFileProvider, cycles);
} else {
// Note: string dev only, don't translate
AutoFreeW s(str::Format(L"Path '%s' doesn't exist", path));
win->ShowNotification(s, NOS_WARNING, NG_STRESS_TEST_SUMMARY);
Finished(false);
}
}
void StressTest::Finished(bool success) {
win->stressTest = nullptr; // make sure we're not double-deleted
if (success) {
int secs = SecsSinceSystemTime(stressStartTime);
AutoFreeW tm(FormatTime(secs));
AutoFreeW s(str::Format(L"Stress test complete, rendered %d files in %s", filesCount, tm));
win->ShowNotification(s, NOS_PERSIST, NG_STRESS_TEST_SUMMARY);
}
CloseWindow(win, exitWhenDone && MayCloseWindow(win));
delete this;
}
bool StressTest::GoToNextFile() {
for (;;) {
AutoFreeW nextFile(fileProvider->NextFile());
if (nextFile) {
if (!IsInRange(fileRanges, ++fileIndex))
continue;
if (OpenFile(nextFile))
return true;
continue;
}
if (--cycles <= 0)
return false;
fileProvider->Restart();
}
}
bool StressTest::OpenFile(const WCHAR* fileName) {
wprintf(L"%s\n", fileName);
fflush(stdout);
LoadArgs args(fileName);
args.forceReuse = rand() % 3 != 1;
WindowInfo* w = LoadDocument(args);
if (!w)
return false;
if (w == win) { // WindowInfo reused
if (!win->IsDocLoaded())
return false;
} else if (!w->IsDocLoaded()) { // new WindowInfo
CloseWindow(w, false);
return false;
}
// transfer ownership of stressTest object to a new window and close the
// current one
AssertCrash(this == win->stressTest);
if (w != win) {
if (win->IsDocLoaded()) {
// try to provoke a crash in RenderCache cleanup code
ClientRect rect(win->hwndFrame);
rect.Inflate(rand() % 10, rand() % 10);
SendMessage(win->hwndFrame, WM_SIZE, 0, MAKELONG(rect.dx, rect.dy));
if (win->AsFixed())
win->cbHandler->RequestRendering(1);
win->RepaintAsync();
}
WindowInfo* toClose = win;
w->stressTest = win->stressTest;
win->stressTest = nullptr;
win = w;
CloseWindow(toClose, false);
}
if (!win->IsDocLoaded())
return false;
win->ctrl->SetDisplayMode(DM_CONTINUOUS);
win->ctrl->SetZoomVirtual(ZOOM_FIT_PAGE, nullptr);
win->ctrl->GoToFirstPage();
if (win->tocVisible || gGlobalPrefs->showFavorites)
SetSidebarVisibility(win, win->tocVisible, gGlobalPrefs->showFavorites);
currPage = pageRanges.at(0).start;
win->ctrl->GoToPage(currPage, false);
currPageRenderTime.Start();
++filesCount;
pageForSearchStart = (rand() % win->ctrl->PageCount()) + 1;
// search immediately in single page documents
if (1 == pageForSearchStart) {
// use text that is unlikely to be found, so that we search all pages
win::SetText(win->hwndFindBox, L"!z_yt");
FindTextOnThread(win, FIND_FORWARD, true);
}
int secs = SecsSinceSystemTime(stressStartTime);
AutoFreeW tm(FormatTime(secs));
AutoFreeW s(str::Format(L"File %d: %s, time: %s", filesCount, fileName, tm));
win->ShowNotification(s, NOS_PERSIST, NG_STRESS_TEST_SUMMARY);
return true;
}
bool StressTest::GoToNextPage() {
double pageRenderTime = currPageRenderTime.GetTimeInMs();
AutoFreeW s(str::Format(L"Page %d rendered in %d milliseconds", currPage, (int)pageRenderTime));
win->ShowNotification(s, NOS_DEFAULT, NG_STRESS_TEST_BENCHMARK);
++currPage;
while (!IsInRange(pageRanges, currPage) && currPage <= win->ctrl->PageCount()) {
currPage++;
}
if (currPage > win->ctrl->PageCount()) {
if (GoToNextFile())
return true;
Finished(true);
return false;
}
win->ctrl->GoToPage(currPage, false);
currPageRenderTime.Start();
// start text search when we're in the middle of the document, so that
// search thread touches both pages that were already rendered and not yet
// rendered
// TODO: it would be nice to also randomize search starting page but the
// current API doesn't make it easy
if (currPage == pageForSearchStart) {
// use text that is unlikely to be found, so that we search all pages
win::SetText(win->hwndFindBox, L"!z_yt");
FindTextOnThread(win, FIND_FORWARD, true);
}
if (1 == rand() % 3) {
ClientRect rect(win->hwndFrame);
int deltaX = (rand() % 40) - 23;
rect.dx += deltaX;
if (rect.dx < 300)
rect.dx += (abs(deltaX) * 3);
int deltaY = (rand() % 40) - 23;
rect.dy += deltaY;
if (rect.dy < 300)
rect.dy += (abs(deltaY) * 3);
SendMessage(win->hwndFrame, WM_SIZE, 0, MAKELONG(rect.dx, rect.dy));
}
return true;
}
void StressTest::TickTimer() {
SetTimer(win->hwndFrame, timerId, USER_TIMER_MINIMUM, nullptr);
}
void StressTest::OnTimer(int timerIdGot) {
CrashIf(timerId != timerIdGot);
KillTimer(win->hwndFrame, timerId);
if (!win->IsDocLoaded()) {
if (!GoToNextFile()) {
Finished(true);
return;
}
TickTimer();
return;
}
// chm documents aren't rendered and we block until we show them
// so we can assume previous page has been shown and go to next page
if (!win->AsFixed()) {
if (!GoToNextPage())
return;
goto Next;
}
// For non-image files, we detect if a page was rendered by checking the cache
// (but we don't wait more than 3 seconds).
// Image files are always fully rendered in WM_PAINT, so we know the page
// has already been rendered.
DisplayModel* dm = win->AsFixed();
bool didRender = gRenderCache.Exists(dm, currPage, dm->GetRotation());
if (!didRender && dm->ShouldCacheRendering(currPage)) {
double timeInMs = currPageRenderTime.GetTimeInMs();
if (timeInMs > 3.0 * 1000) {
if (!GoToNextPage())
return;
}
} else if (!GoToNextPage()) {
return;
}
MakeRandomSelection(win, currPage);
Next:
TickTimer();
}
// note: used from CrashHandler, shouldn't allocate memory
void StressTest::GetLogInfo(str::Str<char>* s) {
s->AppendFmt(", stress test rendered %d files in ", filesCount);
FormatTime(SecsSinceSystemTime(stressStartTime), s);
s->AppendFmt(", currPage: %d", currPage);
}
// note: used from CrashHandler.cpp, should not allocate memory
void GetStressTestInfo(str::Str<char>* s) {
// only add paths to files encountered during an explicit stress test
// (for privacy reasons, users should be able to decide themselves
// whether they want to share what files they had opened during a crash)
if (!IsStressTesting())
return;
for (size_t i = 0; i < gWindows.size(); i++) {
WindowInfo* w = gWindows.at(i);
if (!w || !w->currentTab || !w->currentTab->filePath)
continue;
s->Append("File: ");
char buf[256];
str::conv::ToCodePageBuf(buf, dimof(buf), w->currentTab->filePath, CP_UTF8);
s->Append(buf);
w->stressTest->GetLogInfo(s);
s->Append("\r\n");
}
}
// Select random files to test. We want to test each file type equally, so
// we first group them by file extension and then select up to maxPerType
// for each extension, randomly, and inter-leave the files with different
// extensions, so their testing is evenly distributed.
// Returns result in <files>.
static void RandomizeFiles(WStrVec& files, int maxPerType) {
WStrVec fileExts;
Vec<WStrVec*> filesPerType;
for (size_t i = 0; i < files.size(); i++) {
const WCHAR* file = files.at(i);
const WCHAR* ext = path::GetExt(file);
CrashAlwaysIf(!ext);
int typeNo = fileExts.FindI(ext);
if (-1 == typeNo) {
fileExts.Append(str::Dup(ext));
filesPerType.Append(new WStrVec());
typeNo = (int)filesPerType.size() - 1;
}
filesPerType.at(typeNo)->Append(str::Dup(file));
}
for (size_t j = 0; j < filesPerType.size(); j++) {
WStrVec* all = filesPerType.at(j);
WStrVec* random = new WStrVec();
for (int n = 0; n < maxPerType && all->size() > 0; n++) {
int idx = rand() % all->size();
WCHAR* file = all->at(idx);
random->Append(file);
all->RemoveAtFast(idx);
}
filesPerType.at(j) = random;
delete all;
}
files.Reset();
bool gotAll = false;
while (!gotAll) {
gotAll = true;
for (size_t j = 0; j < filesPerType.size(); j++) {
WStrVec* random = filesPerType.at(j);
if (random->size() > 0) {
gotAll = false;
WCHAR* file = random->at(0);
files.Append(file);
random->RemoveAtFast(0);
}
}
}
for (size_t j = 0; j < filesPerType.size(); j++) {
delete filesPerType.at(j);
}
}
void StartStressTest(CommandLineInfo* i, WindowInfo* win) {
gIsStressTesting = true;
// TODO: for now stress testing only supports the non-ebook ui
gGlobalPrefs->ebookUI.useFixedPageUI = true;
gGlobalPrefs->chmUI.useFixedPageUI = true;
// TODO: make stress test work with tabs?
gGlobalPrefs->useTabs = false;
// forbid entering sleep mode during tests
SetThreadExecutionState(ES_CONTINUOUS | ES_SYSTEM_REQUIRED | ES_DISPLAY_REQUIRED);
srand((unsigned int)time(nullptr));
// redirect stderr to NUL to disable (MuPDF) logging
FILE* nul;
freopen_s(&nul, "NUL", "w", stderr);
int n = i->stressParallelCount;
if (n > 1 || i->stressRandomizeFiles) {
WindowInfo** windows = AllocArray<WindowInfo*>(n);
windows[0] = win;
for (int j = 1; j < n; j++) {
windows[j] = CreateAndShowWindowInfo();
if (!windows[j])
return;
}
WStrVec filesToTest;
wprintf(L"Scanning for files in directory %s\n", i->stressTestPath.Get());
fflush(stdout);
size_t filesCount = GetAllMatchingFiles(i->stressTestPath, i->stressTestFilter, filesToTest, true);
if (0 == filesCount) {
wprintf(L"Didn't find any files matching filter '%s'\n", i->stressTestFilter.Get());
return;
}
wprintf(L"Found %d files", (int)filesCount);
fflush(stdout);
if (i->stressRandomizeFiles) {
// TODO: should probably allow over-writing the 100 limit
RandomizeFiles(filesToTest, 100);
filesCount = filesToTest.size();
wprintf(L"\nAfter randomization: %d files", (int)filesCount);
}
wprintf(L"\n");
fflush(stdout);
for (int j = 0; j < n; j++) {
// dst will be deleted when the stress ends
win = windows[j];
StressTest* dst = new StressTest(win, i->exitWhenDone);
win->stressTest = dst;
// divide filesToTest among each window
FilesProvider* filesProvider = new FilesProvider(filesToTest, n, j);
dst->Start(filesProvider, i->stressTestCycles);
}
free(windows);
} else {
// dst will be deleted when the stress ends
StressTest* dst = new StressTest(win, i->exitWhenDone);
win->stressTest = dst;
dst->Start(i->stressTestPath, i->stressTestFilter, i->stressTestRanges, i->stressTestCycles);
}
}
void OnStressTestTimer(WindowInfo* win, int timerId) {
win->stressTest->OnTimer(timerId);
}
void FinishStressTest(WindowInfo* win) {
delete win->stressTest;
}