1 module unit_threaded.randomized.benchmark;
2 
3 import unit_threaded.from;
4 
5 /* This function used $(D MonoTimeImpl!(ClockType.precise).currTime) to time
6 how long $(D MonoTimeImpl!(ClockType.precise).currTime) takes to return
7 the current time.
8 */
9 private auto medianStopWatchTime()
10 {
11     import core.time;
12     import std.algorithm : sort;
13     import std.datetime: Duration, MonoTimeImpl;
14 
15     enum numRounds = 51;
16     Duration[numRounds] times;
17 
18     MonoTimeImpl!(ClockType.precise) dummy;
19     for (size_t i = 0; i < numRounds; ++i)
20     {
21         auto sw = MonoTimeImpl!(ClockType.precise).currTime;
22         dummy = MonoTimeImpl!(ClockType.precise).currTime;
23         dummy = MonoTimeImpl!(ClockType.precise).currTime;
24         doNotOptimizeAway(dummy);
25         times[i] = MonoTimeImpl!(ClockType.precise).currTime - sw;
26     }
27 
28     sort(times[]);
29 
30     return times[$ / 2].total!"hnsecs";
31 }
32 
33 private from!"std.datetime".Duration getQuantilTick
34     (double q)
35     (from!"std.datetime".Duration[] ticks)
36     pure @safe
37 {
38     size_t idx = cast(size_t)(ticks.length * q);
39 
40     if (ticks.length % 2 == 1)
41     {
42         return ticks[idx];
43     }
44     else
45     {
46         return (ticks[idx] + ticks[idx - 1]) / 2;
47     }
48 }
49 
50 // @Name("Quantil calculations")
51 // unittest
52 // {
53 //     static import std.conv;
54 //     import std.algorithm.iteration : map;
55 
56 //     auto ticks = [1, 2, 3, 4, 5].map!(a => dur!"seconds"(a)).array;
57 
58 //     Duration q25 = getQuantilTick!0.25(ticks);
59 //     assert(q25 == dur!"seconds"(2), q25.toString());
60 
61 //     Duration q50 = getQuantilTick!0.50(ticks);
62 //     assert(q50 == dur!"seconds"(3), q25.toString());
63 
64 //     Duration q75 = getQuantilTick!0.75(ticks);
65 //     assert(q75 == dur!"seconds"(4), q25.toString());
66 
67 //     q25 = getQuantilTick!0.25(ticks[0 .. 4]);
68 //     assert(q25 == dur!"seconds"(1) + dur!"msecs"(500), q25.toString());
69 
70 //     q50 = getQuantilTick!0.50(ticks[0 .. 4]);
71 //     assert(q50 == dur!"seconds"(2) + dur!"msecs"(500), q25.toString());
72 
73 //     q75 = getQuantilTick!0.75(ticks[0 .. 4]);
74 //     assert(q75 == dur!"seconds"(3) + dur!"msecs"(500), q25.toString());
75 // }
76 
77 /** The options  controlling the behaviour of benchmark. */
78 struct BenchmarkOptions
79 {
80     import std.datetime: Duration, seconds;
81 
82     string funcname; // the name of the function to benchmark
83     string filename; // the name of the file the results will be appended to
84     Duration duration = 1.seconds; // the time after which the function to
85                                    // benchmark is not executed anymore
86     size_t maxRounds = 10000; // the maximum number of times the function
87                               // to benchmark is called
88     int seed = 1337; // the seed to the random number generator
89 
90     this(string funcname)
91     {
92         this.funcname = funcname;
93     }
94 }
95 
96 /** This $(D struct) takes care of the time taking and outputting of the
97 statistics.
98 */
99 struct Benchmark
100 {
101     import std.array : Appender;
102     import std.datetime: Duration, MonoTimeImpl, ClockType;
103 
104     string filename; // where to write the benchmark result to
105     string funcname; // the name of the benchmark
106     size_t rounds; // the number of times the functions is supposed to be
107     //executed
108     string timeScale; // the unit the benchmark is measuring in
109     real medianStopWatch; // the median time it takes to get the clocktime twice
110     bool dontWrite; // if set, no data is written to the the file name "filename"
111     // true if, RndValueGen opApply was interrupt unexpectitally
112     Appender!(Duration[]) ticks; // the stopped times, there will be rounds ticks
113     size_t ticksIndex = 0; // the index into ticks
114     size_t curRound = 0; // the number of rounds run
115     MonoTimeImpl!(ClockType.precise) startTime;
116     Duration timeSpend; // overall time spend running the benchmark function
117 
118     /** The constructor for the $(D Benchmark).
119     Params:
120         funcname = The name of the $(D benchmark) instance. The $(D funcname)
121             will be used to associate the results with the function
122         rounds = How many rounds.
123         filename = The $(D filename) will be used as a filename to store the
124             results.
125     */
126     this(in string funcname, in size_t rounds, in string filename)
127     {
128         import std.array : appender;
129         this.filename = filename;
130         this.funcname = funcname;
131         this.rounds = rounds;
132         this.timeScale = "hnsecs";
133         this.ticks = appender!(Duration[])();
134         this.medianStopWatch = medianStopWatchTime();
135     }
136 
137     /** A call to this method will start the time taking process */
138     void start()
139     {
140         this.startTime = MonoTimeImpl!(ClockType.precise).currTime;
141     }
142 
143     /** A call to this method will stop the time taking process, and
144     appends the execution time to the $(D ticks) member.
145     */
146     void stop()
147     {
148         auto end = MonoTimeImpl!(ClockType.precise).currTime;
149         Duration dur = end - this.startTime;
150         this.timeSpend += dur;
151         this.ticks.put(dur);
152         ++this.curRound;
153     }
154 
155     ~this()
156     {
157         import std.stdio : File;
158         import std.datetime: Clock;
159 
160         if (!this.dontWrite && this.ticks.data.length)
161         {
162             import std.algorithm : sort;
163 
164             auto sortedTicks = this.ticks.data;
165             sortedTicks.sort();
166 
167             auto f = File(filename ~ "_bechmark.csv", "a");
168             scope (exit)
169                 f.close();
170 
171             auto q0 = sortedTicks[0].total!("hnsecs")() /
172                 cast(double) this.rounds;
173             auto q25 = getQuantilTick!0.25(sortedTicks).total!("hnsecs")() /
174                 cast(double) this.rounds;
175             auto q50 = getQuantilTick!0.50(sortedTicks).total!("hnsecs")() /
176                    cast(double) this.rounds;
177             auto q75 = getQuantilTick!0.75(sortedTicks).total!("hnsecs")() /
178                 cast(double) this.rounds;
179             auto q100 = sortedTicks[$ - 1].total!("hnsecs")() /
180                 cast(double) this.rounds;
181 
182             // funcname, the data when the benchmark was created, unit of time,
183             // rounds, medianStopWatch, low, 0.25 quantil, median,
184             // 0.75 quantil, high
185             f.writefln(
186                 "\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\""
187                 ~ ",\"%s\"",
188                 this.funcname, Clock.currTime.toISOExtString(),
189                 this.timeScale, this.curRound, this.medianStopWatch,
190                 q0 > this.medianStopWatch ? q0 - this.medianStopWatch : 0,
191                 q25 > this.medianStopWatch ? q25 - this.medianStopWatch : 0,
192                 q50 > this.medianStopWatch ? q50 - this.medianStopWatch : 0,
193                 q75 > this.medianStopWatch ? q75 - this.medianStopWatch : 0,
194                 q100 > this.medianStopWatch ? q100 - this.medianStopWatch : 0);
195         }
196     }
197 }
198 
199 void doNotOptimizeAway(T...)(ref T t)
200 {
201     foreach (ref it; t)
202     {
203         doNotOptimizeAwayImpl(&it);
204     }
205 }
206 
207 private void doNotOptimizeAwayImpl(void* p) {
208         import core.thread : getpid;
209         import std.stdio : writeln;
210         if(getpid() == 1) {
211                 writeln(*cast(char*)p);
212         }
213 }
214 
215 // unittest
216 // {
217 //     static void funToBenchmark(int a, float b, Gen!(int, -5, 5) c, string d,
218 //         GenASCIIString!(1, 10) e)
219 //     {
220 //         import core.thread;
221 
222 //         Thread.sleep(1.seconds / 100000);
223 //         doNotOptimizeAway(a, b, c, d, e);
224 //     }
225 
226 //     benchmark!funToBenchmark();
227 //     benchmark!funToBenchmark("Another Name");
228 //     benchmark!funToBenchmark("Another Name", 2.seconds);
229 //     benchmark!funToBenchmark(2.seconds);
230 // }
231 
232 /** This function runs the passed callable $(D T) for the duration of
233 $(D maxRuntime). It will count how often $(D T) is run in the duration and
234 how long each run took to complete.
235 
236 Unless compiled in release mode, statistics will be printed to $(D stderr).
237 If compiled in release mode the statistics are appended to a file called
238 $(D name).
239 
240 Params:
241     opts = A $(D BenchmarkOptions) instance that encompasses all possible
242         parameters of benchmark.
243     name = The name of the benchmark. The name is also used as filename to
244         save the benchmark results.
245     maxRuntime = The maximum time the benchmark is executed. The last run will
246         not be interrupted.
247 */
248 void benchmark(alias T)(const ref BenchmarkOptions opts)
249 {
250     import std.random : Random;
251     import std.traits: ParameterIdentifierTuple, Parameters;
252     import unit_threaded.randomized.random;
253 
254     auto bench = Benchmark(opts.funcname, opts.maxRounds, opts.filename);
255     auto rnd = Random(opts.seed);
256     enum string[] parameterNames = [ParameterIdentifierTuple!T];
257     auto valueGenerator = RndValueGen!(parameterNames, Parameters!T)(&rnd);
258 
259     while (bench.timeSpend <= opts.duration && bench.curRound < opts.maxRounds)
260     {
261         valueGenerator.genValues();
262 
263         bench.start();
264         try
265         {
266             T(valueGenerator.values);
267         }
268         catch (Throwable t)
269         {
270             import std.experimental.logger : logf;
271 
272             logf("unittest with name %s failed when parameter %s where passed",
273                 opts.funcname, valueGenerator);
274             break;
275         }
276         finally
277         {
278             bench.stop();
279             ++bench.curRound;
280         }
281     }
282 }
283 
284 /// Ditto
285 void benchmark(alias T)(string funcname = "", string filename = __FILE__)
286 {
287     import std.string : empty;
288     import std.traits: fullyQualifiedName;
289 
290     auto opt = BenchmarkOptions(
291         funcname.empty ? fullyQualifiedName!T : funcname
292     );
293     opt.filename = filename;
294     benchmark!(T)(opt);
295 }
296 
297 /// Ditto
298 void benchmark(alias T)(from!"std.datetime".Duration maxRuntime, string filename = __FILE__)
299 {
300     import std.traits: fullyQualifiedName;
301     auto opt = BenchmarkOptions(fullyQualifiedName!T);
302     opt.filename = filename;
303     opt.duration = maxRuntime;
304     benchmark!(T)(opt);
305 }
306 
307 /// Ditto
308 void benchmark(alias T)(string name, from!"std.datetime".Duration maxRuntime,
309     string filename = __FILE__)
310 {
311     auto opt = BenchmarkOptions(name);
312     opt.filename = filename;
313     opt.duration = maxRuntime;
314     benchmark!(T)(opt);
315 }
316 
317 /// Ditto
318 /*void benchmark(alias T)(string name, string filename = __FILE__)
319 {
320     auto opt = BenchmarkOptions(name);
321     opt.filename = filename;
322     benchmark!(T)(opt);
323 }*/