Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
Benchmarker1D.cs
1using System;
3using System.Diagnostics;
4using System.Globalization;
5using System.Text;
6
8{
13 {
14 private readonly SortedDictionary<string, long> tests = new SortedDictionary<string, long>();
15 private readonly object syncObject = new object();
16 private readonly Stopwatch watch;
17
22 {
23 this.watch = new Stopwatch();
24 this.watch.Start();
25 }
26
30 public string[] Tests
31 {
32 get
33 {
34 lock (this.syncObject)
35 {
36 return this.GetTestsLocked();
37 }
38 }
39 }
40
41 private string[] GetTestsLocked()
42 {
43 string[] Result = new string[this.tests.Count];
44 this.tests.Keys.CopyTo(Result, 0);
45 return Result;
46 }
47
52 public double?[] Ticks => this.GetScaledTicks(1);
53
58 public double?[] Minutes => this.GetScaledTicks(1.0 / (Stopwatch.Frequency * 60.0));
59
64 public double?[] Seconds => this.GetScaledTicks(1.0 / Stopwatch.Frequency);
65
70 public double?[] Milliseconds => this.GetScaledTicks(1000.0 / Stopwatch.Frequency);
71
76 public double?[] Microseconds => this.GetScaledTicks(1000000.0 / Stopwatch.Frequency);
77
78 private double?[] GetScaledTicks(double Scale)
79 {
80 lock (this.syncObject)
81 {
82 return this.GetScaledTicksLocked(Scale);
83 }
84 }
85
86 private double?[] GetScaledTicksLocked(double Scale)
87 {
88 double?[] Result = new double?[this.tests.Count];
89 int i;
90
91 i = 0;
92 foreach (string Name in this.tests.Keys)
93 {
94 if (this.tests.TryGetValue(Name, out long Ticks))
95 Result[i] = Ticks * Scale;
96 else
97 Result[i] = null;
98
99 i++;
100 }
101
102 return Result;
103 }
104
109 public string GetResultScriptTicks() => this.GetResultScript(1);
110
115 public string GetResultScriptSeconds() => this.GetResultScript(1.0 / Stopwatch.Frequency);
116
121 public string GetResultScriptMilliseconds() => this.GetResultScript(1000.0 / Stopwatch.Frequency);
122
127 public string GetResultScriptMicroseconds() => this.GetResultScript(1000000.0 / Stopwatch.Frequency);
128
129 private string GetResultScript(double Scale)
130 {
131 StringBuilder sb = new StringBuilder();
132 string[] Names;
133 double?[] Values;
134 double? d;
135 int i, c;
136
137 lock (this.syncObject)
138 {
139 Names = this.GetTestsLocked();
140 Values = this.GetScaledTicksLocked(Scale);
141 }
142
143 c = Names.Length;
144
145 sb.Append('[');
146
147 for (i = 0; i < c; i++)
148 {
149 if (i > 0)
150 sb.Append(",\r\n ");
151 sb.Append("[\"");
152 sb.Append(Names[i].Replace("\"", "\\\""));
153 sb.Append("\",");
154
155 d = Values[i];
156 if (d.HasValue)
157 sb.Append(d.Value.ToString(CultureInfo.InvariantCulture));
158 else
159 sb.Append("null");
160
161 sb.Append(']');
162 }
163
164 sb.Append(']');
165
166 return sb.ToString();
167 }
168
175 public Benchmarking Start(string Name)
176 {
177 GC.GetTotalMemory(true);
178
179 lock (this.syncObject)
180 {
181 if (!this.tests.ContainsKey(Name))
182 this.tests[Name] = 0;
183 }
184
185 return new Benchmarking(this, Name, 0, 0, this.watch.ElapsedTicks);
186 }
187
195 public void Stop(string Name, long N, long M, long StartTicks)
196 {
197 long ElapsedTicks = this.watch.ElapsedTicks - StartTicks;
198
199 lock (this.syncObject)
200 {
201 this.tests[Name] = ElapsedTicks;
202 }
203 }
204
210 public bool Remove(string Name)
211 {
212 lock (this.syncObject)
213 {
214 return this.tests.Remove(Name);
215 }
216 }
217
221 public void Dispose()
222 {
223 this.watch.Stop();
224 }
225 }
226}
Class that keeps track of benchmarking results by name and timing.
double?[] Seconds
Benchmarking results, in seconds, Test names of the first index, complexities in the second.
string[] Tests
Registered tests.
string GetResultScriptTicks()
Gets benchmarking result in ticks, as script.
string GetResultScriptMilliseconds()
Gets benchmarking result in milliseconds, as script.
Benchmarker1D()
Class that keeps track of benchmarking results and timing.
Benchmarking Start(string Name)
Starts a benchmark.
string GetResultScriptSeconds()
Gets benchmarking result in seconds, as script.
double?[] Minutes
Benchmarking results, in minutes, Test names of the first index, complexities in the second.
bool Remove(string Name)
Removes a benchmark.
string GetResultScriptMicroseconds()
Gets benchmarking result in microseconds, as script.
double?[] Milliseconds
Benchmarking results, in milliseconds, Test names of the first index, complexities in the second.
double?[] Ticks
Benchmarking results, in ticks. Test names of the first index, complexities in the second.
void Dispose()
Disposes of the object.
void Stop(string Name, long N, long M, long StartTicks)
Stops a benchmark.
double?[] Microseconds
Benchmarking results, in microseconds, Test names of the first index, complexities in the second.
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