1 module performance.compare;
2 
3 import std.stdio;
4 import std.file;
5 import std.path;
6 import std.csv;
7 import std.typecons;
8 import std.algorithm;
9 import std.array;
10 import std.exception : enforce;
11 import std.conv;
12 
13 import performance.common;
14 
15 ulong[string] loadProfileData(string sha = "")
16 {
17     string fpath;
18     
19     if (sha.empty)
20         fpath = chainPath(exeDir, "profile.csv").array.to!string;
21     else
22         fpath = chainPath(getCachePath(), sha, "tests/performance-tests/profile.csv").array.to!string;
23 
24     enforce(fpath.exists, "SHA profile data does not exists at path " ~ fpath);
25 
26     ulong[string] data;
27 
28     auto file = File(fpath, "r");
29 
30     try
31     {
32         file
33             .byLine
34             .joiner("\n")
35             .csvReader!(Tuple!(string, ulong))
36             .each!(record => data[record[0]] = record[1]);
37     }
38     catch 
39     {
40         writeln("Failed reading profile data.");
41         destroy(data);
42     }
43     finally
44     {
45         file.close();
46     }
47 
48     return data;
49 }
50 
51 void compare(string sha)
52 {
53     auto prevData = loadProfileData(sha);
54     auto currentData = loadProfileData();
55 
56     auto file = File(chainPath(exeDir, "benchmark.csv"), "w");
57     file.writeln("Function Name,Previous Runtime[μs],Current Runtime[μs],Speedup[%]");
58 
59     auto keys = sort(prevData.keys);
60 
61     foreach(key; keys)
62     {
63         auto prevTime = prevData[key];
64 
65         if (key in currentData)
66         {
67             auto currentTime = currentData[key];
68             long speedup = cast(long)((float(prevTime) / float(currentTime) - 1.0f) * 100.0f);
69             file.writefln("%s,%d,%d,%d", key, prevTime, currentTime, speedup, "%");
70         }
71         else
72         {
73             file.writefln("%s:not found", key);
74         }
75     }
76     file.close();
77 }