-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathBenchmarker2D.cs
More file actions
296 lines (254 loc) · 7.33 KB
/
Benchmarker2D.cs
File metadata and controls
296 lines (254 loc) · 7.33 KB
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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
namespace Waher.Runtime.Profiling
{
/// <summary>
/// Class that keeps track of benchmarking results by name, complexity and timing.
/// </summary>
public class Benchmarker2D : IBenchmarker
{
private readonly SortedDictionary<string, bool> tests = new SortedDictionary<string, bool>();
private readonly SortedDictionary<long, bool> complexities = new SortedDictionary<long, bool>();
private readonly Dictionary<string, Dictionary<long, long>> ticks = new Dictionary<string, Dictionary<long, long>>();
private readonly object syncObject = new object();
private readonly Stopwatch watch;
/// <summary>
/// Class that keeps track of benchmarking results by name, complexity and timing.
/// </summary>
public Benchmarker2D()
{
this.watch = new Stopwatch();
this.watch.Start();
}
/// <summary>
/// Registered tests.
/// </summary>
public string[] Tests
{
get
{
lock (this.syncObject)
{
return this.GetTestsLocked();
}
}
}
private string[] GetTestsLocked()
{
string[] Result = new string[this.tests.Count];
this.tests.Keys.CopyTo(Result, 0);
return Result;
}
/// <summary>
/// Registered complexities.
/// </summary>
public long[] Complexities
{
get
{
lock (this.syncObject)
{
return this.GetComplexitiesLocked();
}
}
}
private long[] GetComplexitiesLocked()
{
long[] Result = new long[this.complexities.Count];
this.complexities.Keys.CopyTo(Result, 0);
return Result;
}
/// <summary>
/// Benchmarking results, in ticks. Test names of the first index, complexities in
/// the second.
/// </summary>
public double?[,] Ticks => this.GetScaledTicks(1);
/// <summary>
/// Benchmarking results, in minutes, Test names of the first index,
/// complexities in the second.
/// </summary>
public double?[,] Minutes => this.GetScaledTicks(1.0 / (Stopwatch.Frequency * 60.0));
/// <summary>
/// Benchmarking results, in seconds, Test names of the first index,
/// complexities in the second.
/// </summary>
public double?[,] Seconds => this.GetScaledTicks(1.0 / Stopwatch.Frequency);
/// <summary>
/// Benchmarking results, in milliseconds, Test names of the first index,
/// complexities in the second.
/// </summary>
public double?[,] Milliseconds => this.GetScaledTicks(1000.0 / Stopwatch.Frequency);
/// <summary>
/// Benchmarking results, in microseconds, Test names of the first index,
/// complexities in the second.
/// </summary>
public double?[,] Microseconds => this.GetScaledTicks(1000000.0 / Stopwatch.Frequency);
private double?[,] GetScaledTicks(double Scale)
{
lock (this.syncObject)
{
return this.GetScaledTicksLocked(Scale);
}
}
private double?[,] GetScaledTicksLocked(double Scale)
{
double?[,] Result = new double?[this.tests.Count, this.complexities.Count];
int i, j;
i = 0;
foreach (string Name in this.tests.Keys)
{
j = 0;
foreach (long Complexity in this.complexities.Keys)
{
if (this.ticks.TryGetValue(Name, out Dictionary<long, long> Complexities) &&
Complexities.TryGetValue(Complexity, out long Ticks))
{
Result[i, j] = Ticks * Scale;
}
else
Result[i, j] = null;
j++;
}
i++;
}
return Result;
}
/// <summary>
/// Gets benchmarking result in ticks, as script.
/// </summary>
/// <returns>Script</returns>
public string GetResultScriptTicks() => this.GetResultScript(1);
/// <summary>
/// Gets benchmarking result in seconds, as script.
/// </summary>
/// <returns>Script</returns>
public string GetResultScriptSeconds() => this.GetResultScript(1.0 / Stopwatch.Frequency);
/// <summary>
/// Gets benchmarking result in milliseconds, as script.
/// </summary>
/// <returns>Script</returns>
public string GetResultScriptMilliseconds() => this.GetResultScript(1000.0 / Stopwatch.Frequency);
/// <summary>
/// Gets benchmarking result in microseconds, as script.
/// </summary>
/// <returns>Script</returns>
public string GetResultScriptMicroseconds() => this.GetResultScript(1000000.0 / Stopwatch.Frequency);
private string GetResultScript(double Scale)
{
StringBuilder sb = new StringBuilder();
string[] Names;
long[] ComplexitiesN;
double?[,] Values;
double? d;
int i, j, c, N;
lock (this.syncObject)
{
Names = this.GetTestsLocked();
ComplexitiesN = this.GetComplexitiesLocked();
Values = this.GetScaledTicksLocked(Scale);
}
c = Names.Length;
N = ComplexitiesN.Length;
sb.Append("[[\"N\\\\Test\"");
foreach (string Name in Names)
{
sb.Append(",\"");
sb.Append(Name.Replace("\"", "\\\""));
sb.Append('"');
}
sb.Append(']');
for (j = 0; j < N; j++)
{
sb.Append(",\r\n [");
sb.Append(ComplexitiesN[j].ToString());
for (i = 0; i < c; i++)
{
sb.Append(',');
d = Values[i, j];
if (d.HasValue)
sb.Append(d.Value.ToString().Replace(System.Globalization.NumberFormatInfo.CurrentInfo.NumberDecimalSeparator, "."));
else
sb.Append("null");
}
sb.Append(']');
}
sb.Append(']');
return sb.ToString();
}
/// <summary>
/// Starts a benchmark.
/// </summary>
/// <param name="Name">Name of benchmark.</param>
/// <param name="Complexity">Complexity of benchmark (N).</param>
/// <returns>Benchmarking object. Dispose this object when benchmarking is
/// completed.</returns>
public Benchmarking Start(string Name, long Complexity)
{
GC.GetTotalMemory(true);
lock (this.syncObject)
{
if (!this.tests.ContainsKey(Name))
this.tests[Name] = true;
if (!this.complexities.ContainsKey(Complexity))
this.complexities[Complexity] = true;
}
return new Benchmarking(this, Name, Complexity, 0, this.watch.ElapsedTicks);
}
/// <summary>
/// Stops a benchmark.
/// </summary>
/// <param name="Name">Name of benchmark.</param>
/// <param name="N">Complexity of benchmark (N).</param>
/// <param name="M">Complexity of benchmark (M).</param>
/// <param name="StartTicks">Elapsed ticks at the start of benchmark.</param>
public void Stop(string Name, long N, long M, long StartTicks)
{
long ElapsedTicks = this.watch.ElapsedTicks - StartTicks;
lock (this.syncObject)
{
if (!this.ticks.TryGetValue(Name, out Dictionary<long, long> Complexities))
{
Complexities = new Dictionary<long, long>();
this.ticks[Name] = Complexities;
}
Complexities[N] = ElapsedTicks;
}
}
/// <summary>
/// Removes a benchmark.
/// </summary>
/// <param name="Name">Name of benchmark.</param>
/// <returns>If the benchmark was found, and removed.</returns>
public bool Remove(string Name)
{
lock (this.syncObject)
{
this.ticks.Remove(Name);
return this.tests.Remove(Name);
}
}
/// <summary>
/// Removes a complexity.
/// </summary>
/// <param name="N">Complexity N</param>
/// <returns>If the benchmark was found, and removed.</returns>
public bool Remove(long N)
{
lock (this.syncObject)
{
foreach (Dictionary<long, long> Complexities in this.ticks.Values)
Complexities.Remove(N);
return this.complexities.Remove(N);
}
}
/// <summary>
/// Disposes of the object.
/// </summary>
public void Dispose()
{
this.watch.Stop();
}
}
}