Datei: StressTests/StressTest/Statistics.cs

Last 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
New Commit (4a40b5b)
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 ········readonly ConcurrentDictionary<string, long> deadlockRetries = new ConcurrentDictionary<string, long>();
46 ········long successfulIterations;
47 ········long failedIterations;
48
49 ········public long SuccessfulIterations => Interlocked.Read( ref this.successfulIterations );
50 ········public long FailedIterations => Interlocked.Read( ref this.failedIterations );
51 ········public int ErrorCount => this.errors.Count;
52
53 ········public void IterationSucceeded() => Interlocked.Increment( ref this.successfulIterations );
54
55 ········public void IterationFailed( ErrorInfo error )
56 ········{
57 ············Interlocked.Increment( ref this.failedIterations );
58 ············this.errors.Enqueue( error );
59 ········}
60
61 ········/// <summary>
62 ········/// Counts a retry of a step after a deadlock.
63 ········/// </summary>
64 ········public void DeadlockRetried( string step ) => this.deadlockRetries.AddOrUpdate( step, 1, ( _, n ) => n + 1 );
65
66 ········public string GetDeadlockReport()
67 ········{
68 ············var sb = new StringBuilder();
69 ············sb.AppendLine( $"Deadlock retries: {this.deadlockRetries.Values.Sum()}" );
70 ············foreach (var kvp in this.deadlockRetries.OrderBy( k => k.Key ))
71 ················sb.AppendLine( $"··{kvp.Value,6} x {kvp.Key}" );
72 ············return sb.ToString();
73 ········}
74
75 ········/// <summary>
76 ········/// Executes an operation and measures its duration.
77 ········/// </summary>
78 ········public async Task<T> MeasureAsync<T>( string operation, Func<Task<T>> func )
79 ········{
80 ············long start = Stopwatch.GetTimestamp();
81 ············T result = await func();
82 ············Add( operation, Stopwatch.GetTimestamp() - start );
83 ············return result;
84 ········}
85
86 ········/// <summary>
87 ········/// Executes an operation and measures its duration.
88 ········/// </summary>
89 ········public async Task MeasureAsync( string operation, Func<Task> func )
90 ········{
91 ············long start = Stopwatch.GetTimestamp();
92 ············await func();
93 ············Add( operation, Stopwatch.GetTimestamp() - start );
94 ········}
95
96 ········void Add( string operation, long ticks )
97 ········{
98 ············this.operations.GetOrAdd( operation, _ => new OperationStats() ).Add( ticks );
99 ········}
100
101 ········static double Ms( long ticks ) => ticks * 1000.0 / Stopwatch.Frequency;
102
103 ········public string GetOperationReport( TimeSpan elapsed )
104 ········{
105 ············var sb = new StringBuilder();
106 ············sb.AppendLine( $"{"Operation",-34} {"Count",9} {"Avg ms",9} {"Max ms",9} {"Ops/s",9}" );
107 ············foreach (var kvp in this.operations.OrderBy( k => k.Key ))
108 ············{
109 ················var s = kvp.Value;
110 ················double avg = s.Count == 0 ? 0 : Ms( s.TotalTicks ) / s.Count;
111 ················double perSecond = elapsed.TotalSeconds == 0 ? 0 : s.Count / elapsed.TotalSeconds;
112 ················sb.AppendLine( $"{kvp.Key,-34} {s.Count,9} {avg,9:F1} {Ms( s.MaxTicks ),9:F1} {perSecond,9:F1}" );
113 ············}
114 ············return sb.ToString();
115 ········}
116
117 ········public string GetErrorReport( int maxErrors )
118 ········{
119 ············var sb = new StringBuilder();
120 ············var all = this.errors.ToArray();
121 ············sb.AppendLine( "Errors grouped by step and exception type:" );
122 ············foreach (var g in all.GroupBy( e => (e.Step, Type: e.Exception.GetType().FullName) ).OrderByDescending( g => g.Count() ))
123 ················sb.AppendLine( $"··{g.Count(),6} x {g.Key.Step}: {g.Key.Type}" );
124
125 ············sb.AppendLine();
126 ············sb.AppendLine( $"The first {Math.Min( maxErrors, all.Length )} errors:" );
127 ············foreach (var e in all.Take( maxErrors ))
128 ············{
129 ················sb.AppendLine( $"--- Worker {e.Worker}, iteration {e.Iteration}, step '{e.Step}':" );
130 ················sb.AppendLine( e.Exception.ToString() );
131 ············}
132 ············return sb.ToString();
133 ········}
134 ····}
135 }
136