1 module minijson.benchmark;
2 
3 import minijson.lib : minifyFiles, minifyString;
4 
5 import std : dirEntries, array, SpanMode, writeln, map, readText, parallel, iota, getopt;
6 import std.datetime.stopwatch : benchmark, StopWatch, AutoStart, Duration;
7 
8 void main(string[] args)
9 {
10   bool benchmarkMinifyFiles = false;
11   bool benchmarkMinifyString = true;
12   bool benchmarkParallelMinifyString = false;
13 
14   getopt(args, "benchmark-minifyFiles", &benchmarkMinifyFiles, "benchmark-minifyString",
15       &benchmarkMinifyString, "benchmark-parallel-minifyString", &benchmarkParallelMinifyString);
16 
17   const string[] files = dirEntries("./test/fixtures/standard", SpanMode.shallow).map!(entry => entry.name).array();
18 
19   string[] filesContent;
20   foreach (file; files.parallel())
21   {
22     filesContent ~= readText(file);
23   }
24 
25   Duration result;
26   auto sw = StopWatch(AutoStart.yes);
27 
28   if (benchmarkMinifyFiles)
29   {
30     writeln("Benchmark minifyFiles");
31     sw.reset();
32 
33     minifyFiles(files);
34 
35     result = sw.peek();
36     writeln(result);
37   }
38 
39   if (benchmarkMinifyString)
40   {
41     writeln("Benchmark minifyString single-threaded");
42     const repeat = 120;
43     auto repeater = iota(repeat);
44     string tmp;
45 
46     sw.reset();
47     foreach (_; repeater)
48     {
49       foreach (fileContent; filesContent)
50       {
51         tmp = minifyString(fileContent);
52       }
53     }
54     result = sw.peek();
55 
56     writeln(result / repeat);
57 
58     if (benchmarkParallelMinifyString)
59     {
60       writeln("Benchmark minifyString multi-threaded");
61       auto repeater2 = iota(repeat);
62 
63       sw.reset();
64       foreach (_; repeater2)
65       {
66         foreach (fileContent; filesContent.parallel())
67         {
68           tmp = minifyString(fileContent);
69         }
70       }
71       result = sw.peek();
72 
73       writeln(result / repeat);
74     }
75   }
76 }