Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
Benchmarker2D.cs
1using System;
3using System.Diagnostics;
4using System.Globalization;
5using System.Text;
6
8{
13 {
14 private readonly SortedDictionary<string, bool> tests = new SortedDictionary<string, bool>();
15 private readonly SortedDictionary<long, bool> complexities = new SortedDictionary<long, bool>();
16 private readonly Dictionary<string, Dictionary<long, long>> ticks = new Dictionary<string, Dictionary<long, long>>();
17 private readonly object syncObject = new object();
18 private readonly Stopwatch watch;
19
24 {
25 this.watch = new Stopwatch();
26 this.watch.Start();
27 }
28
32 public string[] Tests
33 {
34 get
35 {
36 lock (this.syncObject)
37 {
38 return this.GetTestsLocked();
39 }
40 }
41 }
42
43 private string[] GetTestsLocked()
44 {
45 string[] Result = new string[this.tests.Count];
46 this.tests.Keys.CopyTo(Result, 0);
47 return Result;
48 }
49
53 public long[] Complexities
54 {
55 get
56 {
57 lock (this.syncObject)
58 {
59 return this.GetComplexitiesLocked();
60 }
61 }
62 }
63
64 private long[] GetComplexitiesLocked()
65 {
66 long[] Result = new long[this.complexities.Count];
67 this.complexities.Keys.CopyTo(Result, 0);
68 return Result;
69 }
70
75 public double?[,] Ticks => this.GetScaledTicks(1);
76
81 public double?[,] Minutes => this.GetScaledTicks(1.0 / (Stopwatch.Frequency * 60.0));
82
87 public double?[,] Seconds => this.GetScaledTicks(1.0 / Stopwatch.Frequency);
88
93 public double?[,] Milliseconds => this.GetScaledTicks(1000.0 / Stopwatch.Frequency);
94
99 public double?[,] Microseconds => this.GetScaledTicks(1000000.0 / Stopwatch.Frequency);
100
101 private double?[,] GetScaledTicks(double Scale)
102 {
103 lock (this.syncObject)
104 {
105 return this.GetScaledTicksLocked(Scale);
106 }
107 }
108
109 private double?[,] GetScaledTicksLocked(double Scale)
110 {
111 double?[,] Result = new double?[this.tests.Count, this.complexities.Count];
112 int i, j;
113
114 i = 0;
115 foreach (string Name in this.tests.Keys)
116 {
117 j = 0;
118 foreach (long Complexity in this.complexities.Keys)
119 {
120 if (this.ticks.TryGetValue(Name, out Dictionary<long, long> Complexities) &&
121 Complexities.TryGetValue(Complexity, out long Ticks))
122 {
123 Result[i, j] = Ticks * Scale;
124 }
125 else
126 Result[i, j] = null;
127
128 j++;
129 }
130
131 i++;
132 }
133
134 return Result;
135 }
136
141 public string GetResultScriptTicks() => this.GetResultScript(1);
142
147 public string GetResultScriptSeconds() => this.GetResultScript(1.0 / Stopwatch.Frequency);
148
153 public string GetResultScriptMilliseconds() => this.GetResultScript(1000.0 / Stopwatch.Frequency);
154
159 public string GetResultScriptMicroseconds() => this.GetResultScript(1000000.0 / Stopwatch.Frequency);
160
161 private string GetResultScript(double Scale)
162 {
163 StringBuilder sb = new StringBuilder();
164 string[] Names;
165 long[] ComplexitiesN;
166 double?[,] Values;
167 double? d;
168 int i, j, c, N;
169
170 lock (this.syncObject)
171 {
172 Names = this.GetTestsLocked();
173 ComplexitiesN = this.GetComplexitiesLocked();
174 Values = this.GetScaledTicksLocked(Scale);
175 }
176
177 c = Names.Length;
178 N = ComplexitiesN.Length;
179
180 sb.Append("[[\"N\\\\Test\"");
181 foreach (string Name in Names)
182 {
183 sb.Append(",\"");
184 sb.Append(Name.Replace("\"", "\\\""));
185 sb.Append('"');
186 }
187 sb.Append(']');
188
189 for (j = 0; j < N; j++)
190 {
191 sb.Append(",\r\n [");
192 sb.Append(ComplexitiesN[j].ToString());
193
194 for (i = 0; i < c; i++)
195 {
196 sb.Append(',');
197
198 d = Values[i, j];
199 if (d.HasValue)
200 sb.Append(d.Value.ToString(CultureInfo.InvariantCulture));
201 else
202 sb.Append("null");
203 }
204
205 sb.Append(']');
206 }
207
208 sb.Append(']');
209
210 return sb.ToString();
211 }
212
220 public Benchmarking Start(string Name, long Complexity)
221 {
222 GC.GetTotalMemory(true);
223
224 lock (this.syncObject)
225 {
226 if (!this.tests.ContainsKey(Name))
227 this.tests[Name] = true;
228
229 if (!this.complexities.ContainsKey(Complexity))
230 this.complexities[Complexity] = true;
231 }
232
233 return new Benchmarking(this, Name, Complexity, 0, this.watch.ElapsedTicks);
234 }
235
243 public void Stop(string Name, long N, long M, long StartTicks)
244 {
245 long ElapsedTicks = this.watch.ElapsedTicks - StartTicks;
246
247 lock (this.syncObject)
248 {
249 if (!this.ticks.TryGetValue(Name, out Dictionary<long, long> Complexities))
250 {
251 Complexities = new Dictionary<long, long>();
252 this.ticks[Name] = Complexities;
253 }
254
255 Complexities[N] = ElapsedTicks;
256 }
257 }
258
264 public bool Remove(string Name)
265 {
266 lock (this.syncObject)
267 {
268 this.ticks.Remove(Name);
269 return this.tests.Remove(Name);
270 }
271 }
272
278 public bool Remove(long N)
279 {
280 lock (this.syncObject)
281 {
282 foreach (Dictionary<long, long> Complexities in this.ticks.Values)
283 Complexities.Remove(N);
284
285 return this.complexities.Remove(N);
286 }
287 }
288
292 public void Dispose()
293 {
294 this.watch.Stop();
295 }
296 }
297}
Class that keeps track of benchmarking results by name, complexity and timing.
bool Remove(string Name)
Removes a benchmark.
string GetResultScriptTicks()
Gets benchmarking result in ticks, as script.
void Stop(string Name, long N, long M, long StartTicks)
Stops a benchmark.
void Dispose()
Disposes of the object.
bool Remove(long N)
Removes a complexity.
string GetResultScriptMicroseconds()
Gets benchmarking result in microseconds, as script.
long[] Complexities
Registered complexities.
double?[,] Seconds
Benchmarking results, in seconds, Test names of the first index, complexities in the second.
double?[,] Minutes
Benchmarking results, in minutes, Test names of the first index, complexities in the second.
string GetResultScriptSeconds()
Gets benchmarking result in seconds, as script.
string[] Tests
Registered tests.
double?[,] Microseconds
Benchmarking results, in microseconds, Test names of the first index, complexities in the second.
Benchmarking Start(string Name, long Complexity)
Starts a benchmark.
double?[,] Ticks
Benchmarking results, in ticks. Test names of the first index, complexities in the second.
string GetResultScriptMilliseconds()
Gets benchmarking result in milliseconds, as script.
double?[,] Milliseconds
Benchmarking results, in milliseconds, Test names of the first index, complexities in the second.
Benchmarker2D()
Class that keeps track of benchmarking results by name, complexity and timing.
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