Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
ProfilerThread.cs
1using System;
2using System.Collections.Generic;
3using System.Globalization;
4using System.Text;
5using System.Xml;
8
10{
15 {
19 Sequential,
20
24 StateMachine,
25
29 Accumulating,
30
34 Binary,
35
39 Analog
40 }
41
45 public class ProfilerThread
46 {
47 private readonly List<ProfilerThread> subThreads = new List<ProfilerThread>();
48 private readonly List<ProfilerEvent> events = new List<ProfilerEvent>();
49 private readonly string name;
50 private readonly int order;
51 private readonly ProfilerThreadType type;
52 private readonly Profiler profiler;
53 private readonly ProfilerThread parent;
54 private long? startedAt = null;
55 private long? stoppedAt = null;
56
65 {
66 this.name = Name;
67 this.order = Order;
68 this.type = Type;
69 this.profiler = Profiler;
70 this.parent = null;
71 }
72
81 {
82 this.name = Name;
83 this.order = Order;
84 this.type = Type;
85 this.parent = Parent;
86 this.profiler = this.parent.Profiler;
87 }
88
92 public string Name => this.name;
93
97 public int Order => this.order;
98
102 public ProfilerThreadType Type => this.type;
103
107 public Profiler Profiler => this.profiler;
108
112 public ProfilerThread Parent => this.parent;
113
117 public long? StartedAt => this.startedAt;
118
122 public long? StoppedAt => this.stoppedAt;
123
131 {
132 ProfilerThread Result = this.profiler.CreateThread(Name, Type, this);
133 this.subThreads.Add(Result);
134 return Result;
135 }
136
141 public void NewState(string State)
142 {
143 this.events.Add(new NewState(this.profiler.ElapsedTicks, State, this));
144 }
145
150 public void NewSample(double Sample)
151 {
152 this.events.Add(new NewSample(this.profiler.ElapsedTicks, Sample, this));
153 }
154
158 public void Idle()
159 {
160 this.events.Add(new Idle(this.profiler.ElapsedTicks, this));
161 }
162
166 public void High()
167 {
168 this.NewState("high");
169 }
170
174 public void Low()
175 {
176 this.NewState("low");
177 }
178
185 public void Interval(DateTime From, DateTime To, string Label)
186 {
187 this.events.Add(new Interval(this.profiler.GetTicks(From), this.profiler.GetTicks(To), Label, this));
188 }
189
196 public void Interval(long From, long To, string Label)
197 {
198 this.events.Add(new Interval(From, To, Label, this));
199 }
200
205 public void Event(string Name)
206 {
207 this.events.Add(new Event(this.profiler.ElapsedTicks, Name, this));
208 }
209
215 public void Event(string Name, string Label)
216 {
217 this.events.Add(new Event(this.profiler.ElapsedTicks, Name, Label, this));
218 }
219
224 public void Exception(System.Exception Exception)
225 {
226 this.events.Add(new Events.Exception(this.profiler.ElapsedTicks, Exception, this));
227 }
228
234 public void Exception(System.Exception Exception, string Label)
235 {
236 this.events.Add(new Events.Exception(this.profiler.ElapsedTicks, Exception, Label, this));
237 }
238
242 public void Start()
243 {
244 long Ticks = this.profiler.ElapsedTicks;
245 this.startedAt = Ticks;
246 this.events.Add(new Start(Ticks, this));
247 }
248
252 public void Stop()
253 {
254 long Ticks = this.profiler.ElapsedTicks;
255 this.stoppedAt = Ticks;
256 this.events.Add(new Stop(Ticks, this));
257 }
258
264 public void ExportXml(XmlWriter Output, TimeUnit TimeUnit)
265 {
266 Output.WriteStartElement("Thread");
267 Output.WriteAttributeString("name", this.name);
268 Output.WriteAttributeString("type", this.type.ToString());
269
270 Output.WriteStartElement("Events");
271
272 ProfilerEvent Prev = null;
273
274 foreach (ProfilerEvent Event in this.events)
275 {
276 Event.ExportXml(Output, Prev, TimeUnit);
277 Prev = Event;
278 }
279
280 Output.WriteEndElement();
281
282 foreach (ProfilerThread Thread in this.subThreads)
283 Thread.ExportXml(Output, TimeUnit);
284
285 Output.WriteEndElement();
286 }
287
293 public void ExportPlantUmlDescription(StringBuilder Output, TimeUnit TimeUnit)
294 {
295 switch (this.type)
296 {
297 case ProfilerThreadType.StateMachine:
298 Output.Append("robust \"");
299 break;
300
301 case ProfilerThreadType.Binary:
302 Output.Append("binary \"");
303 break;
304
305 case ProfilerThreadType.Analog:
306 Output.Append("analog \"");
307 break;
308
309 case ProfilerThreadType.Sequential:
310 case ProfilerThreadType.Accumulating:
311 default:
312 Output.Append("concise \"");
313 break;
314 }
315
316 Output.Append(this.name);
317 Output.Append("\" as T");
318 Output.AppendLine(this.order.ToString());
319
320 if (this.type == ProfilerThreadType.Accumulating)
321 {
323 ProfilerEvent[] Events = this.events.ToArray();
324 this.events.Clear();
325
326 foreach (ProfilerEvent Event in Events)
328
329 Accumulator.Report(this.events);
330 }
331
332 foreach (ProfilerEvent Event in this.events)
334
335 foreach (ProfilerThread Thread in this.subThreads)
336 Thread.ExportPlantUmlDescription(Output, TimeUnit);
337 }
338
342 public string Key => "T" + this.order.ToString();
343
349 {
350 foreach (ProfilerEvent Event in this.events)
351 Event.ExportPlantUml(States);
352
353 foreach (ProfilerThread Thread in this.subThreads)
354 Thread.ExportPlantUmlEvents(States);
355
356 if (this.startedAt.HasValue && this.stoppedAt.HasValue)
357 {
358 StringBuilder Output = States.Summary;
359 long Ticks = this.stoppedAt.Value - this.startedAt.Value;
360 string ElapsedStr = this.profiler.ToTimeStr(Ticks, this, States.TimeUnit, 3);
361 KeyValuePair<double, string> Time;
362
363 Output.Append(this.Key);
364 Output.Append('@');
365
366 Time = this.profiler.ToTime(this.startedAt.Value, this, States.TimeUnit);
367 Output.Append(Time.Key.ToString("F0").Replace(CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator, "."));
368
369 Output.Append(" <-> @");
370
371 Time = this.profiler.ToTime(this.stoppedAt.Value, this, States.TimeUnit);
372 Output.Append(Time.Key.ToString("F0").Replace(CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator, "."));
373
374 Output.Append(" : ");
375 Output.AppendLine(ElapsedStr);
376 }
377 }
378
385 public KeyValuePair<double, string> ToTime(long Ticks, TimeUnit TimeUnit)
386 {
387 return this.profiler.ToTime(Ticks, this, TimeUnit);
388 }
389
396 public string ToTimeStr(long Ticks, TimeUnit TimeUnit)
397 {
398 return this.profiler.ToTimeStr(Ticks, this, TimeUnit, 7);
399 }
400
401 }
402}
override void ExportPlantUml(PlantUmlStates States)
Exports events to PlantUML.
Definition: Event.cs:66
override void ExportPlantUmlPreparation()
Prepares the event for export to PlantUML
Definition: Event.cs:89
Contains a new sample value.
Definition: NewSample.cs:10
Abstract base class for profiler events.
virtual void Accumulate(Accumulator Accumulator)
Accumulates the event.
virtual void ExportXml(XmlWriter Output, ProfilerEvent Previous, TimeUnit TimeUnit)
Exports the event to XML.
void Report(List< ProfilerEvent > Result)
Reports accumulated events to a list of events.
Definition: Accumulator.cs:119
Contains internal states used during generation of PlantUML diagram.s
Class that keeps track of events and timing.
Definition: Profiler.cs:67
Class that keeps track of events and timing for one thread.
ProfilerThread CreateSubThread(string Name, ProfilerThreadType Type)
Creates a new profiler thread.
long? StoppedAt
Ticks when thread was stopped.
void NewSample(double Sample)
A new sample value has been recored
void Exception(System.Exception Exception)
Exception occurred
void Low()
Sets the (binary) state to "low".
void ExportXml(XmlWriter Output, TimeUnit TimeUnit)
Exports events to XML.
string ToTimeStr(long Ticks, TimeUnit TimeUnit)
String representation of time, corresponding to a measured number of high-frequency clock ticks.
void Event(string Name, string Label)
Event occurred
void Interval(DateTime From, DateTime To, string Label)
Records an interval in the profiler thread.
ProfilerThreadType Type
Type of profiler thread.
void ExportPlantUmlEvents(PlantUmlStates States)
Exports events to PlantUML.
void High()
Sets the (binary) state to "high".
KeyValuePair< double, string > ToTime(long Ticks, TimeUnit TimeUnit)
Time (amount, unit), corresponding to a measured number of high-frequency clock ticks.
void Event(string Name)
Event occurred
long? StartedAt
Ticks when thread was started.
ProfilerThread(string Name, int Order, ProfilerThreadType Type, Profiler Profiler)
Class that keeps track of events and timing for one thread.
Profiler Profiler
Profiler reference.
string Key
Thread key in diagrams.
ProfilerThread Parent
Parent profiler thread, if any.
void Exception(System.Exception Exception, string Label)
Exception occurred
ProfilerThread(string Name, int Order, ProfilerThreadType Type, ProfilerThread Parent)
Class that keeps track of events and timing for one thread.
void ExportPlantUmlDescription(StringBuilder Output, TimeUnit TimeUnit)
Exports events to PlantUML.
void NewState(string State)
Thread changes state.
void Interval(long From, long To, string Label)
Records an interval in the profiler thread.
TimeUnit
Options for presenting time in reports.
Definition: Profiler.cs:16
ProfilerThreadType
Type of profiler thread.