Datei: StressTests/StressTest/Statistics.cs

Last Commit (9de6a8c)
1 -- File didn't exist --
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
New Commit (bbf1d57)
1 using System;
2 using System.Collections.Concurrent;
3 using System.Diagnostics;
4 using System.Linq;
5 using System.Text;
6 using System.Threading;
7 using System.Threading.Tasks;
8
9 namespace StressTest
10 {
11 ····/// <summary>
12 ····/// Collects the durations of the database operations and the errors of all workers. Thread safe.
13 ····/// </summary>
14 ····public class Statistics
15 ····{
16 ········class OperationStats
17 ········{
18 ············public long Count;
19 ············public long TotalTicks;
20 ············public long MaxTicks;
21
22 ············public void Add( long ticks )
23 ············{
24 ················Interlocked.Increment( ref Count );
25 ················Interlocked.Add( ref TotalTicks, ticks );
26 ················long max;
27 ················while (ticks > (max = Interlocked.Read( ref MaxTicks )))
28 ················{
29 ····················if (Interlocked.CompareExchange( ref MaxTicks, ticks, max ) == max)
30 ························break;
31 ················}
32 ············}
33 ········}
34
35 ········public class ErrorInfo
36 ········{
37 ············public int Worker { get; init; }
38 ············public int Iteration { get; init; }
39 ············public string Step { get; init; }
40 ············public Exception Exception { get; init; }
41 ········}
42
43 ········readonly ConcurrentDictionary<string, OperationStats> operations = new ConcurrentDictionary<string, OperationStats>();
44 ········readonly ConcurrentQueue<ErrorInfo> errors = new ConcurrentQueue<ErrorInfo>();
45 ········long successfulIterations;
46 ········long failedIterations;
47
48 ········public long SuccessfulIterations => Interlocked.Read( ref this.successfulIterations );
49 ········public long FailedIterations => Interlocked.Read( ref this.failedIterations );
50 ········public int ErrorCount => this.errors.Count;
51
52 ········public void IterationSucceeded() => Interlocked.Increment( ref this.successfulIterations );
53
54 ········public void IterationFailed( ErrorInfo error )
55 ········{
56 ············Interlocked.Increment( ref this.failedIterations );
57 ············this.errors.Enqueue( error );
58 ········}
59
60 ········/// <summary>
61 ········/// Executes an operation and measures its duration.
62 ········/// </summary>
63 ········public async Task<T> MeasureAsync<T>( string operation, Func<Task<T>> func )
64 ········{
65 ············long start = Stopwatch.GetTimestamp();
66 ············T result = await func();
67 ············Add( operation, Stopwatch.GetTimestamp() - start );
68 ············return result;
69 ········}
70
71 ········/// <summary>
72 ········/// Executes an operation and measures its duration.
73 ········/// </summary>
74 ········public async Task MeasureAsync( string operation, Func<Task> func )
75 ········{
76 ············long start = Stopwatch.GetTimestamp();
77 ············await func();
78 ············Add( operation, Stopwatch.GetTimestamp() - start );
79 ········}
80
81 ········void Add( string operation, long ticks )
82 ········{
83 ············this.operations.GetOrAdd( operation, _ => new OperationStats() ).Add( ticks );
84 ········}
85
86 ········static double Ms( long ticks ) => ticks * 1000.0 / Stopwatch.Frequency;
87
88 ········public string GetOperationReport( TimeSpan elapsed )
89 ········{
90 ············var sb = new StringBuilder();
91 ············sb.AppendLine( $"{"Operation",-34} {"Count",9} {"Avg ms",9} {"Max ms",9} {"Ops/s",9}" );
92 ············foreach (var kvp in this.operations.OrderBy( k => k.Key ))
93 ············{
94 ················var s = kvp.Value;
95 ················double avg = s.Count == 0 ? 0 : Ms( s.TotalTicks ) / s.Count;
96 ················double perSecond = elapsed.TotalSeconds == 0 ? 0 : s.Count / elapsed.TotalSeconds;
97 ················sb.AppendLine( $"{kvp.Key,-34} {s.Count,9} {avg,9:F1} {Ms( s.MaxTicks ),9:F1} {perSecond,9:F1}" );
98 ············}
99 ············return sb.ToString();
100 ········}
101
102 ········public string GetErrorReport( int maxErrors )
103 ········{
104 ············var sb = new StringBuilder();
105 ············var all = this.errors.ToArray();
106 ············sb.AppendLine( "Errors grouped by step and exception type:" );
107 ············foreach (var g in all.GroupBy( e => (e.Step, Type: e.Exception.GetType().FullName) ).OrderByDescending( g => g.Count() ))
108 ················sb.AppendLine( $"··{g.Count(),6} x {g.Key.Step}: {g.Key.Type}" );
109
110 ············sb.AppendLine();
111 ············sb.AppendLine( $"The first {Math.Min( maxErrors, all.Length )} errors:" );
112 ············foreach (var e in all.Take( maxErrors ))
113 ············{
114 ················sb.AppendLine( $"--- Worker {e.Worker}, iteration {e.Iteration}, step '{e.Step}':" );
115 ················sb.AppendLine( e.Exception.ToString() );
116 ············}
117 ············return sb.ToString();
118 ········}
119 ····}
120 }
121