@@ -25,8 +25,8 @@ public class PerfTestRunner
2525 private readonly int _timeSeconds ;
2626 private readonly Func < PerfScenarioConfig , PerfScenario > _scenarioFactory ;
2727
28- private PerfScenario [ ] _tests ;
29- private Stopwatch _sw = new Stopwatch ( ) ;
28+ private readonly PerfScenario [ ] _tests ;
29+ private readonly Stopwatch _sw = new Stopwatch ( ) ;
3030
3131 public PerfTestRunner (
3232 ResultWriter writer ,
@@ -80,8 +80,9 @@ private void FilterTcpStatistics()
8080 }
8181 }
8282
83- public async Task RunTestAsync ( )
83+ public async Task < int > RunTestAsync ( )
8484 {
85+ int ret = 0 ;
8586 _sw . Restart ( ) ;
8687
8788 try
@@ -92,14 +93,16 @@ public async Task RunTestAsync()
9293 catch ( OperationCanceledException )
9394 {
9495 Console . WriteLine ( $ "Setup FAILED (timeout:{ _sw . Elapsed } )") ;
96+ ret = 1 ;
97+ return ret ;
9598 }
9699
97100 _sw . Restart ( ) ;
98101 Console . WriteLine ( ) ;
99-
102+
100103 try
101104 {
102- await LoopAsync ( ) . ConfigureAwait ( false ) ;
105+ ret = await LoopAsync ( ) . ConfigureAwait ( false ) ;
103106 }
104107 catch ( OperationCanceledException )
105108 {
@@ -111,16 +114,22 @@ public async Task RunTestAsync()
111114
112115 await TeardownAllAsync ( ) . ConfigureAwait ( false ) ;
113116 Console . WriteLine ( "Done. " ) ;
117+
118+ return ret ;
114119 }
115120
116- private async Task LoopAsync ( )
121+ private async Task < int > LoopAsync ( )
117122 {
118123 using ( var cts = new CancellationTokenSource ( TimeSpan . FromSeconds ( _timeSeconds ) ) )
119124 {
120125 ulong statTotalCompleted = 0 ;
121126 ulong statTotalFaulted = 0 ;
122127 ulong statTotalCancelled = 0 ;
123128 double statTotalSeconds = 0.0 ;
129+ int cpuLoad = 0 ;
130+ long memoryBytes = 0 , gcBytes = 0 , tcpConn = 0 , devConn = 0 ;
131+ double avgRps = 0.0 , stdDevRps = 0.0 ;
132+
124133 List < double > statRps = new List < double > ( ) ;
125134
126135 var runner = new ParallelRun (
@@ -145,23 +154,61 @@ private async Task LoopAsync()
145154 double totalRequestsPerSec = statTotalCompleted / statTotalSeconds ;
146155 double totalTransferPerSec = totalRequestsPerSec * _messageSizeBytes ;
147156
148- ( double avgRps , double stdDevRps ) = CalculateAvgAndStDev ( statRps ) ;
157+ ( avgRps , stdDevRps ) = CalculateAvgAndStDev ( statRps ) ;
149158 double avgBps = avgRps * _messageSizeBytes ;
150159 double stdDevBps = stdDevRps * _messageSizeBytes ;
151- SystemMetrics . GetMetrics ( out int cpuPercent , out long memoryBytes , out long gcBytes , out long tcpConn , out long devConn ) ;
152-
160+ SystemMetrics . GetMetrics ( out cpuLoad , out memoryBytes , out gcBytes , out tcpConn , out devConn ) ;
161+
153162 Console . WriteLine ( $ "[{ _sw . Elapsed } ] Loop Statistics:") ;
154163 Console . WriteLine ( $ "RPS : { requestsPerSec , 10 : N2} R/s Avg: { avgRps , 10 : N2} R/s +/-StdDev: { stdDevRps , 10 : N2} R/s") ;
155- Console . WriteLine ( $ "Throughput: { GetHumanReadableBytes ( transferPerSec ) } /s Avg: { GetHumanReadableBytes ( avgBps ) } /s +/-StdDev: { GetHumanReadableBytes ( avgRps ) } /s ") ;
164+ Console . WriteLine ( $ "Throughput: { GetHumanReadableBytes ( transferPerSec ) } /s Avg: { GetHumanReadableBytes ( avgBps ) } /s +/-StdDev: { GetHumanReadableBytes ( stdDevBps ) } /s ") ;
156165 Console . WriteLine ( $ "Connected : { devConn , 10 : N0} ") ;
157- Console . WriteLine ( $ "CPU : { cpuPercent , 10 : N2} % Mem: { GetHumanReadableBytes ( memoryBytes ) } GC_Mem: { GetHumanReadableBytes ( gcBytes ) } TCP: { tcpConn , 4 : N0} ") ;
166+ Console . WriteLine ( $ "CPU Load : { ( float ) cpuLoad / 100 , 10 : N2} Mem: { GetHumanReadableBytes ( memoryBytes ) } GC_Mem: { GetHumanReadableBytes ( gcBytes ) } TCP: { tcpConn , 4 : N0} ") ;
158167 Console . WriteLine ( "----" ) ;
159168 Console . WriteLine ( $ "TOTALs: ") ;
160169 Console . WriteLine ( $ "Requests : Completed: { statTotalCompleted , 10 : N0} Faulted: { statTotalFaulted , 10 : N0} Cancelled: { statTotalCancelled , 10 : N0} ") ;
161170 Console . WriteLine ( $ "Data : { GetHumanReadableBytes ( statTotalCompleted * ( ulong ) _messageSizeBytes ) } ") ;
162171 } ) ;
163172
164173 await runner . RunAsync ( runOnce : false , ct : cts . Token ) . ConfigureAwait ( false ) ;
174+
175+ Console . WriteLine ( ) ;
176+ int ret = 0 ;
177+ float ? expectedDeviceConn = ( float ) _n * Configuration . Stress . ConnectedDevicesPercentage / 100 ;
178+ float ? expectedTcpConn = ( float ) _poolSize * Configuration . Stress . TcpConnectionsPercentage / 100 ;
179+
180+ if ( expectedDeviceConn . HasValue && ( devConn < expectedDeviceConn ) )
181+ {
182+ Console . Error . WriteLine ( $ "FAILED KPI: Connected Devices. Expected: >{ expectedDeviceConn } ; Actual: { devConn } .") ;
183+ ret = 1 ;
184+ }
185+
186+ if ( expectedTcpConn . HasValue && ( tcpConn != expectedTcpConn ) ) // Ensure all are connected and no connection leaks exist.
187+ {
188+ Console . Error . WriteLine ( $ "FAILED KPI: TCP Connections. Expected: ={ expectedTcpConn } ; Actual: { tcpConn } .") ;
189+ ret = 2 ;
190+ }
191+
192+ if ( Configuration . Stress . RequestsPerSecondMinAvg . HasValue && ( avgRps < Configuration . Stress . RequestsPerSecondMinAvg ) )
193+ {
194+ Console . Error . WriteLine ( $ "FAILED KPI: RPS Average. Expected: >{ Configuration . Stress . RequestsPerSecondMinAvg } ; Actual: { avgRps } .") ;
195+ ret = 3 ;
196+ }
197+
198+ if ( Configuration . Stress . RequestsPerSecondMaxStd . HasValue && ( stdDevRps > Configuration . Stress . RequestsPerSecondMaxStd ) )
199+ {
200+ Console . Error . WriteLine ( $ "FAILED KPI: RPS StdDev. Expected: <{ Configuration . Stress . RequestsPerSecondMaxStd } ; Actual: { stdDevRps } .") ;
201+ ret = 4 ;
202+ }
203+
204+ if ( Configuration . Stress . GCMemoryBytes . HasValue && ( gcBytes > Configuration . Stress . GCMemoryBytes ) )
205+ {
206+ Console . Error . WriteLine ( $ "FAILED KPI: GC Memory. Expected: <{ GetHumanReadableBytes ( Configuration . Stress . GCMemoryBytes . Value ) } ; Actual: { GetHumanReadableBytes ( gcBytes ) } .") ;
207+ ret = 5 ;
208+ }
209+
210+ if ( ret != 0 ) Console . WriteLine ( "^^^^^^^^^^^^^^^^^^^\n " ) ;
211+ return ret ;
165212 }
166213 }
167214
@@ -211,12 +258,12 @@ private async Task SetupAllAsync()
211258 double totalRequestsPerSec = statTotalCompleted / statTotalSeconds ;
212259
213260 ( double avgRps , double stdDevRps ) = CalculateAvgAndStDev ( statRps ) ;
214- SystemMetrics . GetMetrics ( out int cpuPercent , out long memoryBytes , out long gcBytes , out long tcpConn , out long devConn ) ;
261+ SystemMetrics . GetMetrics ( out int cpuLoad , out long memoryBytes , out long gcBytes , out long tcpConn , out long devConn ) ;
215262
216263 Console . WriteLine ( $ "[{ _sw . Elapsed } ] Setup Statistics:") ;
217264 Console . WriteLine ( $ "RPS : { requestsPerSec , 10 : N2} R/s Avg: { avgRps , 10 : N2} R/s +/-StdDev: { stdDevRps , 10 : N2} R/s") ;
218265 Console . WriteLine ( $ "Connected : { devConn , 10 : N0} ") ;
219- Console . WriteLine ( $ "CPU : { cpuPercent , 10 : N2} % Mem: { GetHumanReadableBytes ( memoryBytes ) } GC_Mem: { GetHumanReadableBytes ( gcBytes ) } TCP: { tcpConn , 4 : N0} ") ;
266+ Console . WriteLine ( $ "CPU Load : { ( float ) cpuLoad / 100 , 10 : N2} Mem: { GetHumanReadableBytes ( memoryBytes ) } GC_Mem: { GetHumanReadableBytes ( gcBytes ) } TCP: { tcpConn , 4 : N0} ") ;
220267 Console . WriteLine ( "----" ) ;
221268 Console . WriteLine ( $ "TOTALs: ") ;
222269 Console . WriteLine ( $ "Requests : Completed: { statTotalCompleted , 10 : N0} Faulted: { statTotalFaulted , 10 : N0} Cancelled: { statTotalCancelled , 10 : N0} ") ;
@@ -257,13 +304,13 @@ private async Task TeardownAllAsync()
257304 double totalRequestsPerSec = statTotalCompleted / statTotalSeconds ;
258305
259306 ( double avgRps , double stdDevRps ) = CalculateAvgAndStDev ( statRps ) ;
260- SystemMetrics . GetMetrics ( out int cpuPercent , out long memoryBytes , out long gcBytes , out long tcpConn , out long devConn ) ;
307+ SystemMetrics . GetMetrics ( out int cpuLoad , out long memoryBytes , out long gcBytes , out long tcpConn , out long devConn ) ;
261308
262309
263310 Console . WriteLine ( $ "[{ _sw . Elapsed } ] Teardown Statistics:") ;
264311 Console . WriteLine ( $ "RPS : { requestsPerSec , 10 : N2} R/s Avg: { avgRps , 10 : N2} R/s +/-StdDev: { stdDevRps , 10 : N2} R/s") ;
265312 Console . WriteLine ( $ "Connected : { devConn , 10 : N0} ") ;
266- Console . WriteLine ( $ "CPU : { cpuPercent , 10 : N2} % Mem: { GetHumanReadableBytes ( memoryBytes ) } GC_Mem: { GetHumanReadableBytes ( gcBytes ) } TCP: { tcpConn , 4 : N0} ") ;
313+ Console . WriteLine ( $ "CPU Load : { ( float ) cpuLoad / 100 , 10 : N2} Mem: { GetHumanReadableBytes ( memoryBytes ) } GC_Mem: { GetHumanReadableBytes ( gcBytes ) } TCP: { tcpConn , 4 : N0} ") ;
267314 Console . WriteLine ( "----" ) ;
268315 Console . WriteLine ( $ "TOTALs: ") ;
269316 Console . WriteLine ( $ "Requests : Completed: { statTotalCompleted , 10 : N0} Faulted: { statTotalFaulted , 10 : N0} Cancelled: { statTotalCancelled , 10 : N0} ") ;
0 commit comments