Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
Benchmarker3D.cs
1using System;
3using System.Diagnostics;
4using System.Globalization;
5using System.Text;
6
8{
14 {
15 private readonly SortedDictionary<string, bool> tests = new SortedDictionary<string, bool>();
16 private readonly SortedDictionary<long, bool> complexitiesN = new SortedDictionary<long, bool>();
17 private readonly SortedDictionary<long, bool> complexitiesM = new SortedDictionary<long, bool>();
18 private readonly Dictionary<string, Dictionary<long, Dictionary<long, long>>> ticks = new Dictionary<string, Dictionary<long, Dictionary<long, long>>>();
19 private readonly object syncObject = new object();
20 private readonly Stopwatch watch;
21
27 {
28 this.watch = new Stopwatch();
29 this.watch.Start();
30 }
31
35 public string[] Tests
36 {
37 get
38 {
39 lock (this.syncObject)
40 {
41 return this.GetTestsLocked();
42 }
43 }
44 }
45
46 private string[] GetTestsLocked()
47 {
48 string[] Result = new string[this.tests.Count];
49 this.tests.Keys.CopyTo(Result, 0);
50 return Result;
51 }
52
56 public long[] ComplexitiesN
57 {
58 get
59 {
60 lock (this.syncObject)
61 {
62 return this.GetComplexitiesNLocked();
63 }
64 }
65 }
66
67 private long[] GetComplexitiesNLocked()
68 {
69 long[] Result = new long[this.complexitiesN.Count];
70 this.complexitiesN.Keys.CopyTo(Result, 0);
71 return Result;
72 }
73
77 public long[] ComplexitiesM
78 {
79 get
80 {
81 lock (this.syncObject)
82 {
83 return this.GetComplexitiesMLocked();
84 }
85 }
86 }
87
88 private long[] GetComplexitiesMLocked()
89 {
90 long[] Result = new long[this.complexitiesM.Count];
91 this.complexitiesM.Keys.CopyTo(Result, 0);
92 return Result;
93 }
94
99 public double?[,,] Ticks => this.GetScaledTicks(1);
100
105 public double?[,,] Minutes => this.GetScaledTicks(1.0 / (Stopwatch.Frequency * 60.0));
106
111 public double?[,,] Seconds => this.GetScaledTicks(1.0 / Stopwatch.Frequency);
112
117 public double?[,,] Milliseconds => this.GetScaledTicks(1000.0 / Stopwatch.Frequency);
118
123 public double?[,,] Microseconds => this.GetScaledTicks(1000000.0 / Stopwatch.Frequency);
124
125 private double?[,,] GetScaledTicks(double Scale)
126 {
127 lock (this.syncObject)
128 {
129 return this.GetScaledTicksLocked(Scale);
130 }
131 }
132
133 private double?[,,] GetScaledTicksLocked(double Scale)
134 {
135 lock (this.syncObject)
136 {
137 double?[,,] Result = new double?[this.tests.Count, this.complexitiesN.Count,
138 this.complexitiesM.Count];
139 int i, j, k;
140
141 i = 0;
142 foreach (string Name in this.tests.Keys)
143 {
144 j = 0;
145 foreach (long ComplexityN in this.complexitiesN.Keys)
146 {
147 k = 0;
148 foreach (long ComplexityM in this.complexitiesM.Keys)
149 {
150 if (this.ticks.TryGetValue(Name, out Dictionary<long, Dictionary<long, long>> ComplexitiesN) &&
151 ComplexitiesN.TryGetValue(ComplexityN, out Dictionary<long, long> ComplexitiesM) &&
152 ComplexitiesM.TryGetValue(ComplexityM, out long Ticks))
153 {
154 Result[i, j, k] = Ticks * Scale;
155 }
156 else
157 Result[i, j, k] = null;
158
159 k++;
160 }
161
162 j++;
163 }
164
165 i++;
166 }
167
168 return Result;
169 }
170 }
171
176 public string GetResultScriptTicks() => this.GetResultScript(1);
177
182 public string GetResultScriptSeconds() => this.GetResultScript(1.0 / Stopwatch.Frequency);
183
188 public string GetResultScriptMilliseconds() => this.GetResultScript(1000.0 / Stopwatch.Frequency);
189
194 public string GetResultScriptMicroseconds() => this.GetResultScript(1000000.0 / Stopwatch.Frequency);
195
196 private string GetResultScript(double Scale)
197 {
198 StringBuilder sb = new StringBuilder();
199 string[] Names;
200 long[] ComplexitiesN;
201 long[] ComplexitiesM;
202 double?[,,] Values;
203 double? d;
204 int i, j, k, c, N, M;
205
206 lock (this.syncObject)
207 {
208 Names = this.GetTestsLocked();
209 ComplexitiesN = this.GetComplexitiesNLocked();
210 ComplexitiesM = this.GetComplexitiesMLocked();
211 Values = this.GetScaledTicksLocked(Scale);
212 }
213
214 c = Names.Length;
215 N = ComplexitiesN.Length;
216 M = ComplexitiesM.Length;
217
218 sb.Append('{');
219 for (i = 0; i < c; i++)
220 {
221 if (i > 0)
222 sb.Append(',');
223
224 sb.Append("\r\n\t\"");
225 sb.Append(Names[i].Replace("\"", "\\\""));
226 sb.Append("\":\r\n\t\t[[\"N\\\\M\"");
227
228 foreach (long Complexity in ComplexitiesM)
229 {
230 sb.Append(',');
231 sb.Append(Complexity.ToString());
232 }
233
234 sb.Append(']');
235
236 for (j = 0; j < N; j++)
237 {
238 sb.Append(",\r\n\t\t [");
239 sb.Append(ComplexitiesN[j].ToString());
240
241 for (k = 0; k < M; k++)
242 {
243 sb.Append(',');
244
245 d = Values[i, j, k];
246 if (d.HasValue)
247 sb.Append(d.Value.ToString(CultureInfo.InvariantCulture));
248 else
249 sb.Append("null");
250 }
251
252 sb.Append(']');
253 }
254
255 sb.Append(']');
256 }
257
258 if (c > 0)
259 sb.AppendLine();
260
261 sb.Append('}');
262
263 return sb.ToString();
264 }
265
274 public Benchmarking Start(string Name, long N, long M)
275 {
276 GC.GetTotalMemory(true);
277
278 lock (this.syncObject)
279 {
280 if (!this.tests.ContainsKey(Name))
281 this.tests[Name] = true;
282
283 if (!this.complexitiesN.ContainsKey(N))
284 this.complexitiesN[N] = true;
285
286 if (!this.complexitiesM.ContainsKey(M))
287 this.complexitiesM[M] = true;
288 }
289
290 return new Benchmarking(this, Name, N, M, this.watch.ElapsedTicks);
291 }
292
300 public void Stop(string Name, long N, long M, long StartTicks)
301 {
302 long ElapsedTicks = this.watch.ElapsedTicks - StartTicks;
303
304 lock (this.syncObject)
305 {
306 if (!this.ticks.TryGetValue(Name, out Dictionary<long, Dictionary<long, long>> ComplexitiesN))
307 {
308 ComplexitiesN = new Dictionary<long, Dictionary<long, long>>();
309 this.ticks[Name] = ComplexitiesN;
310 }
311
312 if (!ComplexitiesN.TryGetValue(N, out Dictionary<long, long> ComplexitiesM))
313 {
314 ComplexitiesM = new Dictionary<long, long>();
316 }
317
318 ComplexitiesM[M] = ElapsedTicks;
319 }
320 }
321
327 public bool Remove(string Name)
328 {
329 lock (this.syncObject)
330 {
331 this.ticks.Remove(Name);
332 return this.tests.Remove(Name);
333 }
334 }
335
341 public bool Remove(long? N)
342 {
343 return this.Remove(N, null);
344 }
345
352 public bool Remove(long? N, long? M)
353 {
354 bool Removed = false;
355
356 lock (this.syncObject)
357 {
358 foreach (Dictionary<long, Dictionary<long, long>> ComplexitiesN in this.ticks.Values)
359 {
360 if (N.HasValue)
361 ComplexitiesN.Remove(N.Value);
362
363 if (M.HasValue)
364 {
365 foreach (Dictionary<long, long> ComplexitiesM in ComplexitiesN.Values)
366 ComplexitiesM.Remove(M.Value);
367 }
368 }
369
370 if (N.HasValue && this.complexitiesN.Remove(N.Value))
371 Removed = true;
372
373 if (M.HasValue && this.complexitiesM.Remove(M.Value))
374 Removed = true;
375 }
376
377 return Removed;
378 }
379
383 public void Dispose()
384 {
385 this.watch.Stop();
386 }
387 }
388}
Class that keeps track of benchmarking results by name, two complexities N and M, and timing.
double?[,,] Microseconds
Benchmarking results, in microseconds, Test names of the first index, complexities N in the second an...
string GetResultScriptMilliseconds()
Gets benchmarking result in milliseconds, as script.
void Dispose()
Disposes of the object.
long[] ComplexitiesM
Registered complexities (M).
double?[,,] Milliseconds
Benchmarking results, in milliseconds, Test names of the first index, complexities N in the second an...
string GetResultScriptSeconds()
Gets benchmarking result in seconds, as script.
string[] Tests
Registered tests.
Benchmarking Start(string Name, long N, long M)
Starts a benchmark.
string GetResultScriptMicroseconds()
Gets benchmarking result in microseconds, as script.
double?[,,] Minutes
Benchmarking results, in minutes, Test names of the first index, complexities N in the second and com...
bool Remove(string Name)
Removes a benchmark.
long[] ComplexitiesN
Registered complexities (N).
double?[,,] Ticks
Benchmarking results, in ticks. Test names of the first index, complexities N in the second and compl...
string GetResultScriptTicks()
Gets benchmarking result in ticks, as script.
void Stop(string Name, long N, long M, long StartTicks)
Stops a benchmark.
bool Remove(long? N)
Removes a complexity.
Benchmarker3D()
Class that keeps track of benchmarking results by name, two complexities N and M, and timing.
double?[,,] Seconds
Benchmarking results, in seconds, Test names of the first index, complexities N in the second and com...
bool Remove(long? N, long? M)
Removes a complexity.
Class that keeps track of benchmarking results and timing.
Definition: Benchmarking.cs:9
Class that keeps track of benchmarking results and timing.
Definition: IBenchmarker.cs:9