-
Notifications
You must be signed in to change notification settings - Fork 262
/
main.cpp
1262 lines (1063 loc) · 42.2 KB
/
main.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
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
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <algorithm>
#include <cfloat>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#if defined(_MSC_VER) || defined(__CYGWIN__)
#include <process.h>
#else
#include <spawn.h>
#include <sys/wait.h>
#endif
#include "test.h"
#include "timer.h"
#include "resultfilename.h"
#include "rapidjson/internal/ieee754.h"
static const unsigned cTrialCount = 10;
static const char* gProgramName;
struct TestJson {
TestJson() : filename(), json(), length(), stat(), statUTF16() {}
char* fullpath;
char* filename;
char* json;
size_t length;
Stat stat; // Reference statistics
Stat statUTF16; // Reference statistics
};
typedef std::vector<TestJson> TestJsonList;
static void PrintStat(const Stat& stat) {
printf("objectCount: %10u\n", (unsigned)stat.objectCount);
printf("arrayCount: %10u\n", (unsigned)stat.arrayCount);
printf("numberCount: %10u\n", (unsigned)stat.numberCount);
printf("stringCount: %10u\n", (unsigned)stat.stringCount);
printf("trueCount: %10u\n", (unsigned)stat.trueCount);
printf("falseCount: %10u\n", (unsigned)stat.falseCount);
printf("nullCount: %10u\n", (unsigned)stat.nullCount);
printf("memberCount: %10u\n", (unsigned)stat.memberCount);
printf("elementCount: %10u\n", (unsigned)stat.elementCount);
printf("stringLength: %10u\n", (unsigned)stat.stringLength);
}
#if USE_MEMORYSTAT
static void PrintMemoryStat() {
const MemoryStat& stat = Memory::Instance().GetStat();
printf(
"Memory stats:\n"
" mallocCount = %u\n"
"reallocCount = %u\n"
" freeCount = %u\n"
" currentSize = %u\n"
" peakSize = %u\n",
(unsigned)stat.mallocCount,
(unsigned)stat.reallocCount,
(unsigned)stat.freeCount,
(unsigned)stat.currentSize,
(unsigned)stat.peakSize);
}
#endif
static char* ReadJSON(FILE *fp, size_t* length) {
fseek(fp, 0, SEEK_END);
*length = (size_t)ftell(fp);
fseek(fp, 0, SEEK_SET);
char* json = (char*)malloc(*length + 1);
fread(json, 1, *length, fp);
json[*length] = '\0';
fclose(fp);
return json;
}
static char* ReadJSON(const char* filename, size_t* length) {
FILE *fp = fopen(filename, "rb");
if (!fp) {
char buffer[FILENAME_MAX];
sprintf(buffer, "../%s", filename);
fp = fopen(buffer, "rb");
if (!fp)
return 0;
}
return ReadJSON(fp, length);
}
static void makeValidFilename(char *filename) {
while (*filename) {
switch (*filename) {
case '/':
*filename = '_';
break;
}
filename++;
}
}
static void EscapeString(FILE* fp, const char *s, size_t length) {
fputc('"', fp);
for (size_t j = 0; j < length; j++)
if (s[j] != 0)
fputc(s[j], fp);
else
fputs("\\0", fp);
fputc('"', fp);
}
static bool ReadFiles(const char* path, TestJsonList& testJsons) {
char fullpath[FILENAME_MAX];
sprintf(fullpath, path, "data.txt");
FILE* fp = fopen(fullpath, "r");
if (!fp)
return false;
// Currently use RapidJSON to generate reference statistics
TestList& tests = TestManager::Instance().GetTests();
const TestBase* referenceTest = 0;
for (TestList::iterator itr = tests.begin(); itr != tests.end(); ++itr) {
if (strcmp((*itr)->GetName(), "RapidJSON (C++)") == 0) {
referenceTest = *itr;
break;
}
}
if (!referenceTest) {
printf("Error: Cannot find RapidJSON as refernce test. Not reading any files.\n");
fclose(fp);
return false;
}
while (!feof(fp)) {
char filename[FILENAME_MAX];
if (fscanf(fp, "%s", filename) == 1) {
sprintf(fullpath, path, filename);
FILE *fp2 = fopen(fullpath, "rb");
if (!fp2) {
printf("Error: Cannot read '%s'\n", filename);
continue;
}
TestJson t = TestJson();
t.fullpath = StrDup(fullpath);
t.filename = StrDup(filename);
t.json = ReadJSON(fp2, &t.length);
// Generate reference statistics
#if TEST_SAXSTATISTICS
if (!referenceTest->SaxStatistics(t.json, t.length, &t.stat))
printf("Failed to generate reference statistics\n");
#endif
#if TEST_SAXSTATISTICSUTF16
if (!referenceTest->SaxStatisticsUTF16(t.json, t.length, &t.statUTF16))
printf("Failed to generate reference UTF16 statistics\n");
#endif
printf("Read '%s' (%u bytes)\n", t.filename, (unsigned)t.length);
PrintStat(t.stat);
printf("\n");
testJsons.push_back(t);
}
}
fclose(fp);
printf("\n");
return true;
}
static void FreeFiles(TestJsonList& testJsons) {
for (TestJsonList::iterator itr = testJsons.begin(); itr != testJsons.end(); ++itr) {
free(itr->fullpath);
free(itr->filename);
free(itr->json);
itr->filename = 0;
itr->json = 0;
}
}
// Normally use this at the end of MEMORYSTAT_SCOPE()
#if USE_MEMORYSTAT
void CheckMemoryLeak() {
const MemoryStat& stat = Memory::Instance().GetStat();
if (stat.currentSize != 0) {
printf("\nWarning: potential memory leak (%d allocations for %d bytes)\n",
(int)stat.mallocCount + (int)stat.reallocCount - (int)stat.freeCount,
(int)stat.currentSize);
PrintMemoryStat();
printf("\n");
}
}
#define MEMORYSTAT_CHECKMEMORYLEAK() CheckMemoryLeak()
#else
#define MEMORYSTAT_CHECKMEMORYLEAK()
#endif
static void Verify(const TestBase& test, const TestJsonList& testJsons) {
(void)testJsons;
printf("Verifying %s ... ", test.GetName());
fflush(stdout);
bool failed = false;
#if TEST_PARSE && TEST_STATISTICS
for (TestJsonList::const_iterator itr = testJsons.begin(); itr != testJsons.end(); ++itr) {
MEMORYSTAT_SCOPE();
test.SetUp();
ParseResultBase* dom1 = test.Parse(itr->json, itr->length);
if (!dom1) {
printf("\nFailed to parse '%s'\n", itr->filename);
failed = true;
test.TearDown();
continue;
}
Stat stat1;
if (!test.Statistics(dom1, &stat1)) {
printf("Not support Statistics\n");
delete dom1;
test.TearDown();
continue;
}
StringResultBase* json1 = test.Stringify(dom1);
delete dom1;
if (!json1) {
// Some libraries may not support stringify, but still check statistics
if (memcmp(&stat1, &itr->stat, sizeof(Stat)) != 0 &&
memcmp(&stat1, &itr->statUTF16, sizeof(Stat)) != 0)
{
printf("\nStatistics of '%s' is different from reference.\n\n", itr->filename);
printf("Reference\n---------\n");
PrintStat(itr->stat);
printf("\nStat 1\n--------\n");
PrintStat(stat1);
printf("\n");
failed = true;
}
test.TearDown();
continue;
}
ParseResultBase* dom2 = test.Parse(json1->c_str(), strlen(json1->c_str()));
if (!dom2) {
printf("\nFailed to parse '%s' 2nd time\n", itr->filename);
failed = true;
// Write out json1 for diagnosis
char filename[FILENAME_MAX];
sprintf(filename, "%s_%s", test.GetName(), itr->filename);
makeValidFilename(filename);
FILE* fp = fopen(filename, "wb");
fwrite(json1->c_str(), strlen(json1->c_str()), 1, fp);
fclose(fp);
delete json1;
test.TearDown();
continue;
}
Stat stat2;
test.Statistics(dom2, &stat2);
StringResultBase* json2 = test.Stringify(dom2);
delete dom2;
Stat* statProblem = 0;
int statProblemWhich = 0;
if (memcmp(&stat1, &itr->stat, sizeof(Stat)) != 0 &&
memcmp(&stat1, &itr->statUTF16, sizeof(Stat)) != 0)
{
statProblem = &stat1;
statProblemWhich = 1;
}
else if (memcmp(&stat2, &itr->stat, sizeof(Stat)) != 0 &&
memcmp(&stat2, &itr->statUTF16, sizeof(Stat)) != 0)
{
statProblem = &stat2;
statProblemWhich = 2;
}
if (statProblem != 0) {
printf("\nStatistics of '%s' is different from reference.\n\n", itr->filename);
printf("Reference\n---------\n");
PrintStat(itr->stat);
printf("\nStat #%d\n--------\n", statProblemWhich);
PrintStat(*statProblem);
printf("\n");
// Write out json1 for diagnosis
char filename[FILENAME_MAX];
sprintf(filename, "%s_%s", test.GetName(), itr->filename);
makeValidFilename(filename);
FILE* fp = fopen(filename, "wb");
fwrite(json1->c_str(), strlen(json1->c_str()), 1, fp);
fclose(fp);
failed = true;
}
delete json1;
delete json2;
test.TearDown();
MEMORYSTAT_CHECKMEMORYLEAK();
}
#endif
#if TEST_SAXSTATISTICS
// Verify SaxStatistics()
for (TestJsonList::const_iterator itr = testJsons.begin(); itr != testJsons.end(); ++itr) {
MEMORYSTAT_SCOPE();
Stat stat1;
if (test.SaxStatistics(itr->json, itr->length, &stat1)) {
if (memcmp(&stat1, &itr->stat, sizeof(Stat)) != 0 &&
memcmp(&stat1, &itr->statUTF16, sizeof(Stat)) != 0)
{
printf("\nSaxStatistics of '%s' is different from reference.\n\n", itr->filename);
printf("Reference\n---------\n");
PrintStat(itr->stat);
printf("\nStat #%d\n--------\n", 1);
PrintStat(stat1);
printf("\n");
}
}
}
#endif
printf(failed ? "Failed\n" : "OK\n");
}
static void VerifyAll(const TestJsonList& testJsons) {
TestList& tests = TestManager::Instance().GetTests();
for (TestList::iterator itr = tests.begin(); itr != tests.end(); ++itr)
Verify(**itr, testJsons);
printf("\n");
}
#if USE_MEMORYSTAT
#define BENCH_MEMORYSTAT_INIT() MemoryStat memoryStat = MemoryStat()
#define BENCH_MEMORYSTAT_ITERATION(trial) if (trial == 0) memoryStat = Memory::Instance().GetStat()
#ifdef _MSC_VER
#define BENCH_MEMORYSTAT_OUTPUT(fp) fprintf(fp, ",%Iu,%Iu,%Iu", memoryStat.currentSize, memoryStat.peakSize, memoryStat.mallocCount + memoryStat.reallocCount)
#else
#define BENCH_MEMORYSTAT_OUTPUT(fp) fprintf(fp, ",%zu,%zu,%zu", memoryStat.currentSize, memoryStat.peakSize, memoryStat.mallocCount + memoryStat.reallocCount)
#endif
#else
#define BENCH_MEMORYSTAT_INIT()
#define BENCH_MEMORYSTAT_ITERATION(trial)
#define BENCH_MEMORYSTAT_OUTPUT(fp)
#endif
#if TEST_PARSE
static void BenchParse(const TestBase& test, const TestJsonList& testJsons, FILE *fp) {
MEMORYSTAT_SCOPE();
for (TestJsonList::const_iterator itr = testJsons.begin(); itr != testJsons.end(); ++itr) {
printf("%15s %-20s ... ", "Parse", itr->filename);
fflush(stdout);
double minDuration = DBL_MAX;
BENCH_MEMORYSTAT_INIT();
bool supported = true;
for (unsigned trial = 0; trial < cTrialCount; trial++) {
test.SetUp();
Timer timer;
ParseResultBase* dom;
{
MEMORYSTAT_SCOPE();
timer.Start();
dom = test.Parse(itr->json, itr->length);
timer.Stop();
BENCH_MEMORYSTAT_ITERATION(trial);
}
delete dom;
if (!dom) {
supported = false;
test.TearDown();
break;
}
double duration = timer.GetElapsedMilliseconds();
minDuration = std::min(minDuration, duration);
test.TearDown();
}
if (!supported)
printf("Not support\n");
else {
double throughput = itr->length / (1024.0 * 1024.0) / (minDuration * 0.001);
printf("%6.3f ms %3.3f MB/s\n", minDuration, throughput);
fprintf(fp, "1. Parse,%s,%s,%f", test.GetName(), itr->filename, minDuration);
BENCH_MEMORYSTAT_OUTPUT(fp);
fprintf(fp, ",0"); // Code size
fputc('\n', fp);
}
}
MEMORYSTAT_CHECKMEMORYLEAK();
}
#else
static void BenchParse(const TestBase&, const TestJsonList&, FILE *) {
}
#endif
#if TEST_PARSE && TEST_STRINGIFY
static void BenchStringify(const TestBase& test, const TestJsonList& testJsons, FILE *fp) {
MEMORYSTAT_SCOPE();
for (TestJsonList::const_iterator itr = testJsons.begin(); itr != testJsons.end(); ++itr) {
printf("%15s %-20s ... ", "Stringify", itr->filename);
fflush(stdout);
test.SetUp();
double minDuration = DBL_MAX;
ParseResultBase* dom = test.Parse(itr->json, itr->length);
BENCH_MEMORYSTAT_INIT();
bool supported = true;
for (unsigned trial = 0; trial < cTrialCount; trial++) {
Timer timer;
StringResultBase* json;
{
MEMORYSTAT_SCOPE();
timer.Start();
json = test.Stringify(dom);
timer.Stop();
BENCH_MEMORYSTAT_ITERATION(trial);
}
delete json;
if (!json) {
supported = false;
break;
}
double duration = timer.GetElapsedMilliseconds();
minDuration = std::min(minDuration, duration);
}
delete dom;
test.TearDown();
if (!supported)
printf("Not support\n");
else {
double throughput = itr->length / (1024.0 * 1024.0) / (minDuration * 0.001);
printf("%6.3f ms %3.3f MB/s\n", minDuration, throughput);
fprintf(fp, "2. Stringify,%s,%s,%f", test.GetName(), itr->filename, minDuration);
BENCH_MEMORYSTAT_OUTPUT(fp);
fprintf(fp, ",0"); // Code size
fputc('\n', fp);
}
}
MEMORYSTAT_CHECKMEMORYLEAK();
}
#else
static void BenchStringify(const TestBase&, const TestJsonList&, FILE *) {
}
#endif
#if TEST_PARSE && TEST_PRETTIFY
static void BenchPrettify(const TestBase& test, const TestJsonList& testJsons, FILE *fp) {
MEMORYSTAT_SCOPE();
for (TestJsonList::const_iterator itr = testJsons.begin(); itr != testJsons.end(); ++itr) {
printf("%15s %-20s ... ", "Prettify", itr->filename);
fflush(stdout);
test.SetUp();
double minDuration = DBL_MAX;
ParseResultBase* dom = test.Parse(itr->json, itr->length);
BENCH_MEMORYSTAT_INIT();
bool supported = true;
for (unsigned trial = 0; trial < cTrialCount; trial++) {
Timer timer;
StringResultBase* json;
{
MEMORYSTAT_SCOPE();
timer.Start();
json = test.Prettify(dom);
timer.Stop();
BENCH_MEMORYSTAT_ITERATION(trial);
}
delete json;
if (!json) {
supported = false;
break;
}
double duration = timer.GetElapsedMilliseconds();
minDuration = std::min(minDuration, duration);
}
delete dom;
test.TearDown();
if (!supported)
printf("Not support\n");
else {
double throughput = itr->length / (1024.0 * 1024.0) / (minDuration * 0.001);
printf("%6.3f ms %3.3f MB/s\n", minDuration, throughput);
fprintf(fp, "3. Prettify,%s,%s,%f", test.GetName(), itr->filename, minDuration);
BENCH_MEMORYSTAT_OUTPUT(fp);
fprintf(fp, ",0"); // Code size
fputc('\n', fp);
}
}
MEMORYSTAT_CHECKMEMORYLEAK();
}
#else
static void BenchPrettify(const TestBase&, const TestJsonList&, FILE *) {
}
#endif
#if TEST_PARSE && TEST_STATISTICS
static void BenchStatistics(const TestBase& test, const TestJsonList& testJsons, FILE *fp) {
MEMORYSTAT_SCOPE();
for (TestJsonList::const_iterator itr = testJsons.begin(); itr != testJsons.end(); ++itr) {
printf("%15s %-20s ... ", "Statistics", itr->filename);
fflush(stdout);
test.SetUp();
double minDuration = DBL_MAX;
ParseResultBase* dom = test.Parse(itr->json, itr->length);
BENCH_MEMORYSTAT_INIT();
bool supported = true;
for (unsigned trial = 0; trial < cTrialCount; trial++) {
Timer timer;
{
MEMORYSTAT_SCOPE();
timer.Start();
Stat stat;
supported = test.Statistics(dom, &stat);
timer.Stop();
BENCH_MEMORYSTAT_ITERATION(trial);
}
if (!supported)
break;
double duration = timer.GetElapsedMilliseconds();
minDuration = std::min(minDuration, duration);
}
delete dom;
test.TearDown();
if (!supported)
printf("Not support\n");
else {
double throughput = itr->length / (1024.0 * 1024.0) / (minDuration * 0.001);
printf("%6.3f ms %3.3f MB/s\n", minDuration, throughput);
fprintf(fp, "4. Statistics,%s,%s,%f", test.GetName(), itr->filename, minDuration);
BENCH_MEMORYSTAT_OUTPUT(fp);
fprintf(fp, ",0"); // Code size
fputc('\n', fp);
}
}
MEMORYSTAT_CHECKMEMORYLEAK();
}
#else
static void BenchStatistics(const TestBase&, const TestJsonList&, FILE *) {
}
#endif
#if TEST_SAXROUNDTRIP
static void BenchSaxRoundtrip(const TestBase& test, const TestJsonList& testJsons, FILE *fp) {
MEMORYSTAT_SCOPE();
for (TestJsonList::const_iterator itr = testJsons.begin(); itr != testJsons.end(); ++itr) {
printf("%15s %-20s ... ", "SaxRoundtrip", itr->filename);
fflush(stdout);
double minDuration = DBL_MAX;
BENCH_MEMORYSTAT_INIT();
bool supported = true;
for (unsigned trial = 0; trial < cTrialCount; trial++) {
test.SetUp();
Timer timer;
StringResultBase* json;
{
MEMORYSTAT_SCOPE();
timer.Start();
json = test.SaxRoundtrip(itr->json, itr->length);
timer.Stop();
BENCH_MEMORYSTAT_ITERATION(trial);
}
delete json;
if (!json) {
supported = false;
test.TearDown();
break;
}
double duration = timer.GetElapsedMilliseconds();
minDuration = std::min(minDuration, duration);
test.TearDown();
}
if (!supported)
printf("Not support\n");
else {
double throughput = itr->length / (1024.0 * 1024.0) / (minDuration * 0.001);
printf("%6.3f ms %3.3f MB/s\n", minDuration, throughput);
fprintf(fp, "5. Sax Round-trip,%s,%s,%f", test.GetName(), itr->filename, minDuration);
BENCH_MEMORYSTAT_OUTPUT(fp);
fprintf(fp, ",0"); // Code size
fputc('\n', fp);
}
}
MEMORYSTAT_CHECKMEMORYLEAK();
}
#else
static void BenchSaxRoundtrip(const TestBase&, const TestJsonList&, FILE*) {
}
#endif
#if TEST_SAXSTATISTICS
static void BenchSaxStatistics(const TestBase& test, const TestJsonList& testJsons, FILE *fp) {
MEMORYSTAT_SCOPE();
for (TestJsonList::const_iterator itr = testJsons.begin(); itr != testJsons.end(); ++itr) {
printf("%15s %-20s ... ", "Sax Statistics", itr->filename);
fflush(stdout);
double minDuration = DBL_MAX;
BENCH_MEMORYSTAT_INIT();
bool supported = true;
for (unsigned trial = 0; trial < cTrialCount; trial++) {
test.SetUp();
Timer timer;
{
MEMORYSTAT_SCOPE();
timer.Start();
Stat stat;
supported = test.SaxStatistics(itr->json, itr->length, &stat);
timer.Stop();
BENCH_MEMORYSTAT_ITERATION(trial);
}
if (!supported) {
test.TearDown();
break;
}
double duration = timer.GetElapsedMilliseconds();
minDuration = std::min(minDuration, duration);
test.TearDown();
}
if (!supported)
printf("Not support\n");
else {
double throughput = itr->length / (1024.0 * 1024.0) / (minDuration * 0.001);
printf("%6.3f ms %3.3f MB/s\n", minDuration, throughput);
fprintf(fp, "6. SaxStatistics,%s,%s,%f", test.GetName(), itr->filename, minDuration);
BENCH_MEMORYSTAT_OUTPUT(fp);
fprintf(fp, ",0"); // Code size
fputc('\n', fp);
}
}
MEMORYSTAT_CHECKMEMORYLEAK();
}
#else
static void BenchSaxStatistics(const TestBase&, const TestJsonList&, FILE*) {
}
#endif
static void BenchCodeSize(const TestBase& test, const TestJsonList& testJsons, FILE *fp) {
(void)testJsons;
#if _MSC_VER
const char cSeparator = '\\';
#else
const char cSeparator = '/';
#endif
// Compute path of bin
char path[FILENAME_MAX];
strcpy(path, gProgramName);
char* lastSep = strrchr(path, cSeparator);
*lastSep = '\0';
// Compute filename suffix (e.g. "_release_x32_vs2010.exe"
const char* filename_suffix = strchr(lastSep + 1, '_');
// Compute test base name (e.g. "rapidjsontest")
char testFilename[FILENAME_MAX];
strcpy(testFilename, test.GetFilename());
*strrchr(testFilename, '.') = '\0';
const char* testBaseName = strrchr(testFilename, cSeparator) + 1;
// Assemble a full path
char fullpath[FILENAME_MAX];
sprintf(fullpath, "%s%cjsonstat%cjsonstat_%s%s", path, cSeparator, cSeparator, testBaseName, filename_suffix);
char * const argv[] = { fullpath, testJsons.front().fullpath, NULL };
#ifdef _MSC_VER
int ret = _spawnv(_P_WAIT, fullpath, argv);
#elif defined(__CYGWIN__)
int ret = spawnv(_P_WAIT, fullpath, argv);
#else
pid_t pid;
int ret = posix_spawn(&pid, fullpath, NULL, NULL, argv, NULL);
if (ret == 0) {
int status;
waitpid(pid, &status, 0);
}
#endif
if (ret != 0) {
printf("Execute '%s' failed (ret=%d)\n", fullpath, ret);
return;
}
// Get file size
FILE *fp2 = fopen(fullpath, "rb");
if (fp2) {
fseek(fp2, 0, SEEK_END);
unsigned fileSize = (unsigned)ftell(fp2);
printf("jsonstat file size = %u\n", fileSize);
fprintf(fp, "7. Code size,%s,jsonstat,0", test.GetName());
#if USE_MEMORYSTAT
fprintf(fp, ",0,0,0");
#endif
fprintf(fp, ",%u\n", fileSize);
fclose(fp2);
}
else
printf("File '%s' not found\n", fullpath);
}
static void BenchPerformance(const TestBase& test, const TestJsonList& testJsons, FILE *fp) {
printf("Benchmarking Performance of %s\n", test.GetName());
BenchParse(test, testJsons, fp);
BenchStringify(test, testJsons, fp);
BenchPrettify(test, testJsons, fp);
BenchStatistics(test, testJsons, fp);
BenchSaxRoundtrip(test, testJsons, fp);
BenchSaxStatistics(test, testJsons, fp);
BenchCodeSize(test, testJsons, fp);
printf("\n");
}
static void BenchAllPerformance(const TestJsonList& testJsons) {
// Try to write to /result path, where template.php exists
FILE *fp;
if ((fp = fopen("../../result/performance.php", "r")) != NULL) {
fclose(fp);
fp = fopen("../../result/performance_" RESULT_FILENAME, "w");
}
else if ((fp = fopen("../result/performance.php", "r")) != NULL) {
fclose(fp);
fp = fopen("../result/performance_" RESULT_FILENAME, "w");
}
else
fp = fopen("performance_" RESULT_FILENAME, "w");
fputs("Type,Library,Filename,Time (ms)", fp);
#if USE_MEMORYSTAT
fputs(",Memory (byte),MemoryPeak (byte),AllocCount", fp);
#endif
fputs(",FileSize (byte)\n", fp);
TestList& tests = TestManager::Instance().GetTests();
for (TestList::iterator itr = tests.begin(); itr != tests.end(); ++itr)
BenchPerformance(**itr, testJsons, fp);
fclose(fp);
}
#if TEST_CONFORMANCE
static void BenchConformance(const TestBase& test, FILE* fp) {
printf("Benchmarking Conformance of %s\n", test.GetName());
// Output markdown
FILE* md;
char testname[FILENAME_MAX];
strcpy(testname, test.GetName());
makeValidFilename(testname);
char mdFilename[FILENAME_MAX];
sprintf(mdFilename, "../../result/conformance_%s.md", testname);
if (!(md = fopen(mdFilename, "w"))) {
sprintf(mdFilename, "../result/conformance_%s.md", testname);
md = fopen(mdFilename, "w");
}
if (md)
fprintf(md, "# Conformance of %s\n\n", test.GetName());
#if TEST_PARSE
// Parse Validation, JsonChecker pass
if (md)
fprintf(md, "## 1. Parse Validation\n\n");
int parseValidationCorrect = 0, parseValidationTotal = 0;
for (int i = 1; i <= 3; i++) {
MEMORYSTAT_SCOPE();
char path[FILENAME_MAX];
sprintf(path, "../data/jsonchecker/pass%02d.json", i);
size_t length;
char* json = ReadJSON(path, &length);
if (!json)
continue;
test.SetUp();
ParseResultBase* pr = test.Parse(json, length);
bool result = pr != 0;
fprintf(fp, "1. Parse Validation,%s,pass%02d,%s\n", test.GetName(), i, result ? "true" : "false");
// printf("pass%02d: %s\n", i, result ? "true" : "false");
delete pr;
test.TearDown();
if (!result) {
if (md)
fprintf(md, "* `%s` is valid but was mistakenly deemed invalid.\n~~~js\n%s\n~~~\n\n", path, json);
}
else
parseValidationCorrect++;
parseValidationTotal++;
free(json);
MEMORYSTAT_CHECKMEMORYLEAK();
}
// Parse Validation, JsonChecker fail
for (int i = 1; i <= 33; i++) {
MEMORYSTAT_SCOPE();
char path[FILENAME_MAX];
sprintf(path, "../data/jsonchecker/fail%02d.json", i);
size_t length;
char* json = ReadJSON(path, &length);
if (!json)
continue;
test.SetUp();
ParseResultBase* pr = test.Parse(json, length);
bool result = pr == 0;
fprintf(fp, "1. Parse Validation,%s,fail%02d,%s\n", test.GetName(), i, result ? "true" : "false");
// printf("fail%02d: %s\n", i, result ? "true" : "false");
delete pr;
test.TearDown();
if (!result) {
if (md)
fprintf(md, "* `%s` is invalid but was mistakenly deemed valid.\n~~~js\n%s\n~~~\n\n", path, json);
}
else
parseValidationCorrect++;
parseValidationTotal++;
free(json);
MEMORYSTAT_CHECKMEMORYLEAK();
}
if (md)
fprintf(md, "\nSummary: %d of %d are correct.\n\n", parseValidationCorrect, parseValidationTotal);
#endif // TEST_PARSE
parseValidationTotal = 0;
parseValidationCorrect = 0;
if (md)
fprintf(md, "## 2. Parse Double\n\n");
// Parse Double
{
using rapidjson::internal::Double;
int i = 1;
#define TEST_DOUBLE(json, expect)\
{\
bool result = false;\
double actual = 0.0;\
test.SetUp();\
if (test.ParseDouble(json, &actual)) \
result = Double(expect).Uint64Value() == Double(actual).Uint64Value();\
if (!result) {\
if (md)\
fprintf(md, "* `%s`\n * expect: `%.17g (0x016%" PRIX64 ")`\n * actual: `%.17g (0x016%" PRIX64 ")`\n\n",\
json, expect, Double(expect).Uint64Value(), actual, Double(actual).Uint64Value());\
}\
else\
parseValidationCorrect++;\
parseValidationTotal++;\
/*printf("double%02d: %s\n", i, result ? "true" : "false");*/\
/*if (!result)*/\
/* printf("JSON: %s\nExpect: %17g\nActual: %17g\n\n", json, expect, actual);*/\
fprintf(fp, "2. Parse Double,%s,double%02d,%s\n", test.GetName(), i, result ? "true" : "false");\
test.TearDown();\
i++;\
}
TEST_DOUBLE("[0.0]", 0.0);
TEST_DOUBLE("[-0.0]", -0.0);
TEST_DOUBLE("[1.0]", 1.0);
TEST_DOUBLE("[-1.0]", -1.0);
TEST_DOUBLE("[1.5]", 1.5);
TEST_DOUBLE("[-1.5]", -1.5);
TEST_DOUBLE("[3.1416]", 3.1416);
TEST_DOUBLE("[1E10]", 1E10);
TEST_DOUBLE("[1e10]", 1e10);
TEST_DOUBLE("[1E+10]", 1E+10);
TEST_DOUBLE("[1E-10]", 1E-10);
TEST_DOUBLE("[-1E10]", -1E10);
TEST_DOUBLE("[-1e10]", -1e10);
TEST_DOUBLE("[-1E+10]", -1E+10);
TEST_DOUBLE("[-1E-10]", -1E-10);
TEST_DOUBLE("[1.234E+10]", 1.234E+10);
TEST_DOUBLE("[1.234E-10]", 1.234E-10);
TEST_DOUBLE("[1.79769e+308]", 1.79769e+308);
TEST_DOUBLE("[2.22507e-308]", 2.22507e-308);
TEST_DOUBLE("[-1.79769e+308]", -1.79769e+308);
TEST_DOUBLE("[-2.22507e-308]", -2.22507e-308);
TEST_DOUBLE("[4.9406564584124654e-324]", 4.9406564584124654e-324); // minimum denormal
TEST_DOUBLE("[2.2250738585072009e-308]", 2.2250738585072009e-308); // Max subnormal double
TEST_DOUBLE("[2.2250738585072014e-308]", 2.2250738585072014e-308); // Min normal positive double
TEST_DOUBLE("[1.7976931348623157e+308]", 1.7976931348623157e+308); // Max double
TEST_DOUBLE("[1e-10000]", 0.0); // must underflow
TEST_DOUBLE("[18446744073709551616]", 18446744073709551616.0); // 2^64 (max of uint64_t + 1, force to use double)
TEST_DOUBLE("[-9223372036854775809]", -9223372036854775809.0); // -2^63 - 1(min of int64_t + 1, force to use double)
TEST_DOUBLE("[0.9868011474609375]", 0.9868011474609375); // https://github.com/miloyip/rapidjson/issues/120
TEST_DOUBLE("[123e34]", 123e34); // Fast Path Cases In Disguise
TEST_DOUBLE("[45913141877270640000.0]", 45913141877270640000.0);
TEST_DOUBLE("[2.2250738585072011e-308]", 2.2250738585072011e-308); // http://www.exploringbinary.com/php-hangs-on-numeric-value-2-2250738585072011e-308/
//TEST_DOUBLE("[1e-00011111111111]", 0.0);
//TEST_DOUBLE("[-1e-00011111111111]", -0.0);
TEST_DOUBLE("[1e-214748363]", 0.0);
TEST_DOUBLE("[1e-214748364]", 0.0);
//TEST_DOUBLE("[1e-21474836311]", 0.0);
TEST_DOUBLE("[0.017976931348623157e+310]", 1.7976931348623157e+308); // Max double in another form
// Since
// abs((2^-1022 - 2^-1074) - 2.2250738585072012e-308) = 3.109754131239141401123495768877590405345064751974375599... ¡Á 10^-324
// abs((2^-1022) - 2.2250738585072012e-308) = 1.830902327173324040642192159804623318305533274168872044... ¡Á 10 ^ -324
// So 2.2250738585072012e-308 should round to 2^-1022 = 2.2250738585072014e-308
TEST_DOUBLE("[2.2250738585072012e-308]", 2.2250738585072014e-308); // http://www.exploringbinary.com/java-hangs-when-converting-2-2250738585072012e-308/
// More closer to normal/subnormal boundary
// boundary = 2^-1022 - 2^-1075 = 2.225073858507201136057409796709131975934819546351645648... ¡Á 10^-308
TEST_DOUBLE("[2.22507385850720113605740979670913197593481954635164564e-308]", 2.2250738585072009e-308);
TEST_DOUBLE("[2.22507385850720113605740979670913197593481954635164565e-308]", 2.2250738585072014e-308);
// 1.0 is in (1.0 - 2^-54, 1.0 + 2^-53)
// 1.0 - 2^-54 = 0.999999999999999944488848768742172978818416595458984375
TEST_DOUBLE("[0.999999999999999944488848768742172978818416595458984375]", 1.0); // round to even
TEST_DOUBLE("[0.999999999999999944488848768742172978818416595458984374]", 0.99999999999999989); // previous double
TEST_DOUBLE("[0.999999999999999944488848768742172978818416595458984376]", 1.0); // next double
// 1.0 + 2^-53 = 1.00000000000000011102230246251565404236316680908203125
TEST_DOUBLE("[1.00000000000000011102230246251565404236316680908203125]", 1.0); // round to even
TEST_DOUBLE("[1.00000000000000011102230246251565404236316680908203124]", 1.0); // previous double
TEST_DOUBLE("[1.00000000000000011102230246251565404236316680908203126]", 1.00000000000000022); // next double
// Numbers from https://github.com/floitsch/double-conversion/blob/master/test/cctest/test-strtod.cc
TEST_DOUBLE("[72057594037927928.0]", 72057594037927928.0);
TEST_DOUBLE("[72057594037927936.0]", 72057594037927936.0);
TEST_DOUBLE("[72057594037927932.0]", 72057594037927936.0);
TEST_DOUBLE("[7205759403792793199999e-5]", 72057594037927928.0);
TEST_DOUBLE("[7205759403792793200001e-5]", 72057594037927936.0);
TEST_DOUBLE("[9223372036854774784.0]", 9223372036854774784.0);