forked from Sneeds-Feed-and-Seed/sneedacity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBenchmark.cpp
583 lines (468 loc) · 15.5 KB
/
Benchmark.cpp
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
/**********************************************************************
Sneedacity: A Digital Audio Editor
Benchmark.cpp
Dominic Mazzoni
*******************************************************************//**
\class BenchmarkDialog
\brief BenchmarkDialog is used for measuring performance and accuracy
of sample block storage.
*//*******************************************************************/
#include "Benchmark.h"
#include <wx/app.h>
#include <wx/log.h>
#include <wx/textctrl.h>
#include <wx/button.h>
#include <wx/checkbox.h>
#include <wx/choice.h>
#include <wx/dialog.h>
#include <wx/sizer.h>
#include <wx/stattext.h>
#include <wx/timer.h>
#include <wx/utils.h>
#include <wx/valgen.h>
#include <wx/valtext.h>
#include <wx/intl.h>
#include "SampleBlock.h"
#include "ShuttleGui.h"
#include "Project.h"
#include "WaveClip.h"
#include "WaveTrack.h"
#include "Sequence.h"
#include "Prefs.h"
#include "ProjectSettings.h"
#include "ViewInfo.h"
#include "FileNames.h"
#include "widgets/SneedacityMessageBox.h"
#include "widgets/wxPanelWrapper.h"
// Change these to the desired format...should probably make the
// choice available in the dialog
#define SampleType short
#define SampleFormat int16Sample
class BenchmarkDialog final : public wxDialogWrapper
{
public:
// constructors and destructors
BenchmarkDialog( wxWindow *parent, SneedacityProject &project );
void MakeBenchmarkDialog();
private:
// WDR: handler declarations
void OnRun( wxCommandEvent &event );
void OnSave( wxCommandEvent &event );
void OnClear( wxCommandEvent &event );
void OnClose( wxCommandEvent &event );
void Printf(const TranslatableString &str);
void HoldPrint(bool hold);
void FlushPrint();
SneedacityProject &mProject;
const ProjectSettings &mSettings;
bool mHoldPrint;
wxString mToPrint;
wxString mBlockSizeStr;
wxString mDataSizeStr;
wxString mNumEditsStr;
wxString mRandSeedStr;
bool mBlockDetail;
bool mEditDetail;
wxTextCtrl *mText;
private:
DECLARE_EVENT_TABLE()
};
void RunBenchmark( wxWindow *parent, SneedacityProject &project )
{
/*
int action = SneedacityMessageBox(
XO("This will close all project windows (without saving)\nand open the Sneedacity Benchmark dialog.\n\nAre you sure you want to do this?"),
XO("Benchmark"),
wxYES_NO | wxICON_EXCLAMATION,
NULL);
if (action != wxYES)
return;
for ( auto pProject : AllProjects{} )
GetProjectFrame( *pProject ).Close();
*/
BenchmarkDialog dlog{ parent, project };
dlog.CentreOnParent();
dlog.ShowModal();
}
//
// BenchmarkDialog
//
enum {
RunID = 1000,
BSaveID,
ClearID,
StaticTextID,
BlockSizeID,
DataSizeID,
NumEditsID,
RandSeedID
};
BEGIN_EVENT_TABLE(BenchmarkDialog, wxDialogWrapper)
EVT_BUTTON( RunID, BenchmarkDialog::OnRun )
EVT_BUTTON( BSaveID, BenchmarkDialog::OnSave )
EVT_BUTTON( ClearID, BenchmarkDialog::OnClear )
EVT_BUTTON( wxID_CANCEL, BenchmarkDialog::OnClose )
END_EVENT_TABLE()
BenchmarkDialog::BenchmarkDialog(
wxWindow *parent, SneedacityProject &project)
:
/* i18n-hint: Benchmark means a software speed test */
wxDialogWrapper( parent, 0, XO("Benchmark"),
wxDefaultPosition, wxDefaultSize,
wxDEFAULT_DIALOG_STYLE |
wxRESIZE_BORDER)
, mProject(project)
, mSettings{ ProjectSettings::Get(project) }
{
SetName();
mBlockSizeStr = wxT("64");
mNumEditsStr = wxT("100");
mDataSizeStr = wxT("32");
mRandSeedStr = wxT("234657");
mBlockDetail = false;
mEditDetail = false;
HoldPrint(false);
MakeBenchmarkDialog();
}
// WDR: handler implementations for BenchmarkDialog
void BenchmarkDialog::OnClose(wxCommandEvent & WXUNUSED(event))
{
EndModal(0);
}
void BenchmarkDialog::MakeBenchmarkDialog()
{
ShuttleGui S(this, eIsCreating);
// Strings don't need to be translated because this class doesn't
// ever get used in a stable release.
S.StartVerticalLay(true);
{
S.SetBorder(8);
S.StartMultiColumn(4);
{
//
S.Id(BlockSizeID)
.Validator<wxTextValidator>(wxFILTER_NUMERIC, &mBlockSizeStr)
.AddTextBox(XXO("Disk Block Size (KB):"),
wxT(""),
12);
//
S.Id(NumEditsID)
.Validator<wxTextValidator>(wxFILTER_NUMERIC, &mNumEditsStr)
.AddTextBox(XXO("Number of Edits:"),
wxT(""),
12);
//
S.Id(DataSizeID)
.Validator<wxTextValidator>(wxFILTER_NUMERIC, &mDataSizeStr)
.AddTextBox(XXO("Test Data Size (MB):"),
wxT(""),
12);
///
S.Id(RandSeedID)
.Validator<wxTextValidator>(wxFILTER_NUMERIC, &mRandSeedStr)
/* i18n-hint: A "seed" is a number that initializes a
pseudorandom number generating algorithm */
.AddTextBox(XXO("Random Seed:"),
wxT(""),
12);
}
S.EndMultiColumn();
//
S.Validator<wxGenericValidator>(&mBlockDetail)
.AddCheckBox(XXO("Show detailed info about each block file"),
false);
//
S.Validator<wxGenericValidator>(&mEditDetail)
.AddCheckBox(XXO("Show detailed info about each editing operation"),
false);
//
mText = S.Id(StaticTextID)
/* i18n-hint noun */
.Name(XO("Output"))
.Style( wxTE_MULTILINE | wxTE_READONLY | wxTE_RICH )
.MinSize( { 500, 200 } )
.AddTextWindow(wxT(""));
//
S.SetBorder(10);
S.StartHorizontalLay(wxALIGN_LEFT | wxEXPAND, false);
{
S.StartHorizontalLay(wxALIGN_LEFT, false);
{
S.Id(RunID).AddButton(XXO("Run"), wxALIGN_CENTRE, true);
S.Id(BSaveID).AddButton(XXO("Save"));
/* i18n-hint verb; to empty or erase */
S.Id(ClearID).AddButton(XXO("Clear"));
}
S.EndHorizontalLay();
S.StartHorizontalLay(wxALIGN_CENTER, true);
{
// Spacer
}
S.EndHorizontalLay();
S.StartHorizontalLay(wxALIGN_NOT | wxALIGN_LEFT, false);
{
/* i18n-hint verb */
S.Id(wxID_CANCEL).AddButton(XXO("Close"));
}
S.EndHorizontalLay();
}
S.EndHorizontalLay();
}
S.EndVerticalLay();
Fit();
SetSizeHints(GetSize());
}
void BenchmarkDialog::OnSave( wxCommandEvent & WXUNUSED(event))
{
/* i18n-hint: Benchmark means a software speed test;
leave untranslated file extension .txt */
auto fName = XO("benchmark.txt").Translation();
fName = FileNames::SelectFile(FileNames::Operation::Export,
XO("Export Benchmark Data as:"),
wxEmptyString,
fName,
wxT("txt"),
{ FileNames::TextFiles },
wxFD_SAVE | wxRESIZE_BORDER,
this);
if (fName.empty())
return;
mText->SaveFile(fName);
}
void BenchmarkDialog::OnClear(wxCommandEvent & WXUNUSED(event))
{
mText->Clear();
}
void BenchmarkDialog::Printf(const TranslatableString &str)
{
auto s = str.Translation();
mToPrint += s;
if (!mHoldPrint)
FlushPrint();
}
void BenchmarkDialog::HoldPrint(bool hold)
{
mHoldPrint = hold;
if (!mHoldPrint)
FlushPrint();
}
void BenchmarkDialog::FlushPrint()
{
while(mToPrint.length() > 100) {
mText->AppendText(mToPrint.Left(100));
mToPrint = mToPrint.Right(mToPrint.length() - 100);
}
if (mToPrint.length() > 0)
mText->AppendText(mToPrint);
mToPrint = wxT("");
}
void BenchmarkDialog::OnRun( wxCommandEvent & WXUNUSED(event))
{
TransferDataFromWindow();
if (!Validate())
return;
// This code will become part of libsneedacity,
// and this class will be phased out.
long blockSize, numEdits, dataSize, randSeed;
mBlockSizeStr.ToLong(&blockSize);
mNumEditsStr.ToLong(&numEdits);
mDataSizeStr.ToLong(&dataSize);
mRandSeedStr.ToLong(&randSeed);
if (blockSize < 1 || blockSize > 1024) {
SneedacityMessageBox(
XO("Block size should be in the range 1 - 1024 KB.") );
return;
}
if (numEdits < 1 || numEdits > 10000) {
SneedacityMessageBox(
XO("Number of edits should be in the range 1 - 10000.") );
return;
}
if (dataSize < 1 || dataSize > 2000) {
SneedacityMessageBox(
XO("Test data size should be in the range 1 - 2000 MB.") );
return;
}
bool editClipCanMove = true;
gPrefs->Read(wxT("/GUI/EditClipCanMove"), &editClipCanMove);
gPrefs->Write(wxT("/GUI/EditClipCanMove"), false);
gPrefs->Flush();
// Remember the old blocksize, so that we can restore it later.
auto oldBlockSize = Sequence::GetMaxDiskBlockSize();
Sequence::SetMaxDiskBlockSize(blockSize * 1024);
const auto cleanup = finally( [&] {
Sequence::SetMaxDiskBlockSize(oldBlockSize);
gPrefs->Write(wxT("/GUI/EditClipCanMove"), editClipCanMove);
gPrefs->Flush();
} );
wxBusyCursor busy;
HoldPrint(true);
const auto t =
WaveTrackFactory{ mSettings,
SampleBlockFactory::New( mProject ) }
.NewWaveTrack(SampleFormat);
t->SetRate(1);
srand(randSeed);
uint64_t nChunks, chunkSize;
//chunkSize = 7500ull + (rand() % 1000ull);
chunkSize = 200ull + (rand() % 100ull);
nChunks = (dataSize * 1048576ull) / (chunkSize*sizeof(SampleType));
while (nChunks < 20 || chunkSize > (blockSize*1024)/4)
{
chunkSize = std::max( uint64_t(1), (chunkSize / 2) + (rand() % 100) );
nChunks = (dataSize * 1048576ull) / (chunkSize*sizeof(SampleType));
}
// The chunks are the pieces we move around in the test.
// They are (and are supposed to be) a different size to
// the blocks that make the sample blocks. That way we get to
// do some testing of when edit chunks cross sample block boundaries.
Printf( XO("Using %lld chunks of %lld samples each, for a total of %.1f MB.\n")
.Format( nChunks, chunkSize, nChunks*chunkSize*sizeof(SampleType)/1048576.0 ) );
int trials = numEdits;
using Samples = ArrayOf<SampleType>;
Samples small1{nChunks};
Samples block{chunkSize};
Printf( XO("Preparing...\n") );
wxTheApp->Yield();
FlushPrint();
int v;
int bad;
int z;
long elapsed;
wxString tempStr;
wxStopWatch timer;
for (uint64_t i = 0; i < nChunks; i++) {
v = SampleType(rand());
small1[i] = v;
for (uint64_t b = 0; b < chunkSize; b++)
block[b] = v;
t->Append((samplePtr)block.get(), SampleFormat, chunkSize);
}
t->Flush();
// This forces the WaveTrack to flush all of the appends (which is
// only necessary if you want to access the Sequence class directly,
// as we're about to do).
t->GetEndTime();
if (t->GetClipByIndex(0)->GetSequence()->GetNumSamples() != nChunks * chunkSize) {
Printf( XO("Expected len %lld, track len %lld.\n")
.Format(
nChunks * chunkSize,
t->GetClipByIndex(0)->GetSequence()->GetNumSamples()
.as_long_long() ) );
goto fail;
}
Printf( XO("Performing %d edits...\n").Format( trials ) );
wxTheApp->Yield();
FlushPrint();
timer.Start();
for (z = 0; z < trials; z++) {
// First chunk to cut
// 0 <= x0 < nChunks
const uint64_t x0 = rand() % nChunks;
// Number of chunks to cut
// 1 <= xlen <= nChunks - x0
const uint64_t xlen = 1 + (rand() % (nChunks - x0));
if (mEditDetail)
Printf( XO("Cut: %lld - %lld \n")
.Format( x0 * chunkSize, (x0 + xlen) * chunkSize) );
Track::Holder tmp;
try {
tmp = t->Cut(double (x0 * chunkSize), double ((x0 + xlen) * chunkSize));
}
catch (const SneedacityException&) {
Printf( XO("Trial %d\n").Format( z ) );
Printf( XO("Cut (%lld, %lld) failed.\n")
.Format( (x0 * chunkSize), (x0 + xlen) * chunkSize) );
Printf( XO("Expected len %lld, track len %lld.\n")
.Format(
nChunks * chunkSize,
t->GetClipByIndex(0)->GetSequence()->GetNumSamples()
.as_long_long() ) );
goto fail;
}
// Position to paste
// 0 <= y0 <= nChunks - xlen
const uint64_t y0 = rand() % (nChunks - xlen + 1);
if (mEditDetail)
Printf( XO("Paste: %lld\n").Format( y0 * chunkSize ) );
try {
t->Paste((double)(y0 * chunkSize), tmp.get());
}
catch (const SneedacityException&) {
Printf( XO("Trial %d\nFailed on Paste.\n").Format( z ) );
goto fail;
}
if (t->GetClipByIndex(0)->GetSequence()->GetNumSamples() != nChunks * chunkSize) {
Printf( XO("Trial %d\n").Format( z ) );
Printf( XO("Expected len %lld, track len %lld.\n")
.Format(
nChunks * chunkSize,
t->GetClipByIndex(0)->GetSequence()->GetNumSamples()
.as_long_long() ) );
goto fail;
}
// Permute small1 correspondingly to the cut and paste
auto first = &small1[0];
if (x0 + xlen < nChunks)
std::rotate( first + x0, first + x0 + xlen, first + nChunks );
std::rotate( first + y0, first + nChunks - xlen, first + nChunks );
}
elapsed = timer.Time();
if (mBlockDetail) {
auto seq = t->GetClipByIndex(0)->GetSequence();
seq->DebugPrintf(seq->GetBlockArray(), seq->GetNumSamples(), &tempStr);
mToPrint += tempStr;
}
Printf( XO("Time to perform %d edits: %ld ms\n").Format( trials, elapsed ) );
FlushPrint();
wxTheApp->Yield();
#if 0
Printf( XO("Checking file pointer leaks:\n") );
Printf( XO("Track # blocks: %ld\n").Format( t->GetBlockArray()->size() ) );
Printf( XO("Disk # blocks: \n") );
system("ls .sneedacity_temp/* | wc --lines");
#endif
Printf( XO("Doing correctness check...\n") );
FlushPrint();
wxTheApp->Yield();
bad = 0;
timer.Start();
for (uint64_t i = 0; i < nChunks; i++) {
v = small1[i];
t->Get((samplePtr)block.get(), SampleFormat, i * chunkSize, chunkSize);
for (uint64_t b = 0; b < chunkSize; b++)
if (block[b] != v) {
bad++;
if (bad < 10)
Printf( XO("Bad: chunk %lld sample %lld\n").Format( i, b ) );
b = chunkSize;
}
}
if (bad == 0)
Printf( XO("Passed correctness check!\n") );
else
Printf( XO("Errors in %d/%lld chunks\n").Format( bad, nChunks ) );
elapsed = timer.Time();
Printf( XO("Time to check all data: %ld ms\n").Format( elapsed ) );
Printf( XO("Reading data again...\n") );
wxTheApp->Yield();
FlushPrint();
timer.Start();
for (uint64_t i = 0; i < nChunks; i++) {
v = small1[i];
t->Get((samplePtr)block.get(), SampleFormat, i * chunkSize, chunkSize);
for (uint64_t b = 0; b < chunkSize; b++)
if (block[b] != v)
bad++;
}
elapsed = timer.Time();
Printf( XO("Time to check all data (2): %ld ms\n").Format( elapsed ) );
Printf( XO("At 44100 Hz, %d bytes per sample, the estimated number of\n simultaneous tracks that could be played at once: %.1f\n" )
.Format( SAMPLE_SIZE(SampleFormat), (nChunks*chunkSize/44100.0)/(elapsed/1000.0) ) );
goto success;
fail:
Printf( XO("TEST FAILED!!!\n") );
success:
Printf( XO("Benchmark completed successfully.\n") );
HoldPrint(false);
}