33using System . Diagnostics ;
44using System . Linq ;
55using System . Text ;
6+ using System . Text . Json . Nodes ;
67using System . Threading ;
78using System . Threading . Tasks ;
89using AustinHarris . JsonRpc ;
@@ -47,33 +48,53 @@ internal static async Task RunAsync(Action<string> print, double seconds, int wo
4748 {
4849 if ( seconds <= 0 || workers <= 0 ) throw new ArgumentOutOfRangeException ( nameof ( seconds ) ) ;
4950 var summary = new List < BenchmarkRunner . ChartRow > ( ) ;
51+ var resultRows = new JsonArray ( ) ;
5052 foreach ( var ( shape , flow ) in Rows )
5153 {
5254 string session = "async-benchmark-" + shape + "-" + flow ;
5355 try
5456 {
5557 Register ( session , shape , flow ) ;
5658 var rowInputs = Inputs ( shape ) ;
57- await ValidateAndReportAllocations ( print , session , shape , flow , rowInputs ) ;
59+ var perShape = await ValidateAndReportAllocations ( print , session , shape , flow , rowInputs ) ;
5860 bool yield = shape == "yield" ;
5961 var m = await Measure ( session , rowInputs , workers , seconds , processWide : yield ) ;
6062 string label = yield ? "one request (yieldsOnce)" : "equal-weight mix of five requests" ;
6163 print ( $ "{ shape } / { flow } : { m . Count : N0} RPCs, { workers } workers, { m . Seconds : F3} s, { m . RpcPerSec : N0} RPC/s; worker startup { m . StartupSeconds * 1000 : F1} ms; " +
6264 $ "{ ( yield ? "process-wide" : "worker-thread" ) } allocation total { m . AllocatedBytes : N0} B ({ m . BytesPerRpc : F0} B/RPC, including method and harness allocations); { label } ; { m . Cpu . TrailingText } .") ;
6365 summary . Add ( new BenchmarkRunner . ChartRow ( $ "{ shape } / { flow } ", m . RpcPerSec , $ "{ m . Count , 12 : N0} RPCs { m . BytesPerRpc , 6 : F0} B/RPC { m . Cpu . TrailingText } ") ) ;
66+ var resultRow = BenchResults . Row ( RowId ( workers == 1 ? "async1" : "asyncn" , shape , flow ) , m . RpcPerSec , m . Count , m . Cpu ) ;
67+ resultRow . Insert ( 1 , "shape" , shape ) ;
68+ resultRow . Insert ( 2 , "flow" , flow . ToString ( ) ) ;
69+ resultRow . Insert ( 5 , "bytesPerRpc" , Math . Round ( m . BytesPerRpc ) ) ;
70+ resultRow . Insert ( 6 , "startupMs" , Math . Round ( m . StartupSeconds * 1000 , 1 ) ) ;
71+ if ( perShape != null ) resultRow [ "bytesPerShape" ] = BenchResults . Numbers ( perShape ) ;
72+ resultRows . Add ( resultRow ) ;
6473 }
6574 finally { Handler . DestroySession ( session ) ; }
6675 }
6776 BenchmarkRunner . PrintBarChart ( $ "ProcessAsync(bytes), awaited workers - { Config . Serializer . Name } - { workers } worker{ ( workers == 1 ? "" : "s" ) } - RPC/s by registration", "Registration" , summary ) ;
77+
78+ if ( BenchResults . Enabled )
79+ {
80+ var doc = BenchResults . Document ( "async" , seconds ) ;
81+ doc [ "workers" ] = workers ;
82+ doc [ "rows" ] = resultRows ;
83+ BenchResults . Write ( $ "async-w{ workers } ", doc , print ) ;
84+ }
6885 }
6986
87+ /// <summary>The results id of a registration row: <c><prefix>-<shape>-<flow></c> in lower case, as benchmarks.json names the series.</summary>
88+ internal static string RowId ( string prefix , string shape , RpcContextFlow flow ) => $ "{ prefix } -{ shape . ToLowerInvariant ( ) } -{ flow . ToString ( ) . ToLowerInvariant ( ) } ";
89+
7090 /// <summary>
7191 /// The scaling gate: the inline None rows at 1, 2 and <paramref name="workers"/> workers, <paramref name="runs"/>
7292 /// paired runs, medians, and the 2/1 and N/1 ratios. Returns false when any N/1 ratio is below
7393 /// <paramref name="threshold"/>. A process-wide serialization point on the async path holds the ratio near 1.3
74- /// on any core count; the per-thread cache measured about 6.8 on 16 threads.
94+ /// on any core count; the per-thread cache measured about 6.8 on 16 threads. When <paramref name="report"/> is given,
95+ /// the gate cells (median, the paired runs, CPU), the ratios and the verdict are added to it for the results file.
7596 /// </summary>
76- internal static async Task < bool > ScaleAsync ( Action < string > print , double seconds , int workers , double threshold , int runs = 3 )
97+ internal static async Task < bool > ScaleAsync ( Action < string > print , double seconds , int workers , double threshold , int runs = 3 , JsonObject report = null )
7798 {
7899 if ( workers < 2 ) throw new ArgumentOutOfRangeException ( nameof ( workers ) ) ;
79100 var counts = new [ ] { 1 , 2 , workers } . Distinct ( ) . ToArray ( ) ;
@@ -101,6 +122,8 @@ internal static async Task<bool> ScaleAsync(Action<string> print, double seconds
101122 }
102123
103124 bool pass = true ;
125+ var cells = new JsonArray ( ) ;
126+ var gate = new JsonArray ( ) ;
104127 print ( "" ) ;
105128 print ( $ "| Registration | 1 worker | 2 workers | { workers } workers | 2/1 | { workers } /1 | gate | CPU at { workers } workers |") ;
106129 print ( "| --- | ---: | ---: | ---: | ---: | ---: | --- | --- |" ) ;
@@ -113,11 +136,32 @@ internal static async Task<bool> ScaleAsync(Action<string> print, double seconds
113136 bool ok = many / one >= threshold ;
114137 pass &= ok ;
115138 print ( $ "| { shape } / { flow } | { one : N0} | { two : N0} | { many : N0} | { two / one : F2} | { many / one : F2} | { ( ok ? "pass" : "FAIL" ) } | { cpu . TrailingText } |") ;
139+ foreach ( int w in counts )
140+ {
141+ var list = results [ ( shape , flow , w ) ] ;
142+ var cell = BenchResults . Row ( RowId ( "scale" , shape , flow ) , Median ( list . Select ( m => m . RpcPerSec ) . ToList ( ) ) , list . Sum ( m => m . Count ) , MedianCpu ( list ) ) ;
143+ cell . Insert ( 1 , "shape" , shape ) ;
144+ cell . Insert ( 2 , "flow" , flow . ToString ( ) ) ;
145+ cell . Insert ( 3 , "workers" , w ) ;
146+ cell . Insert ( 5 , "values" , BenchResults . Numbers ( list . Select ( m => Math . Round ( m . RpcPerSec ) ) ) ) ;
147+ cells . Add ( cell ) ;
148+ }
149+ gate . Add ( new JsonObject { [ "shape" ] = shape , [ "flow" ] = flow . ToString ( ) , [ "twoOverOne" ] = Math . Round ( two / one , 2 ) , [ "nOverOne" ] = Math . Round ( many / one , 2 ) , [ "pass" ] = ok } ) ;
116150 }
117151 print ( "" ) ;
118152 print ( pass ? $ "Scaling check passed: every inline row scales at least { threshold : 0.0#} x from 1 to { workers } workers."
119153 : $ "Scaling check FAILED: an inline row scales less than { threshold : 0.0#} x from 1 to { workers } workers; look for a process-wide serialization point on the ProcessAsync path.") ;
120154 print ( "The 2/1 ratio is diagnostic (the lock was already visible at two workers, about 1.3); it is not gated." ) ;
155+ if ( report != null )
156+ {
157+ report [ "workers" ] = workers ;
158+ report [ "threshold" ] = threshold ;
159+ report [ "runs" ] = runs ;
160+ report [ "counts" ] = BenchResults . Numbers ( counts . Select ( c => ( double ) c ) ) ;
161+ report [ "rows" ] = cells ;
162+ report [ "gate" ] = gate ;
163+ report [ "verdict" ] = pass ? "pass" : "fail" ;
164+ }
121165 return pass ;
122166 }
123167
@@ -128,12 +172,14 @@ internal static async Task<bool> ScaleAsync(Action<string> print, double seconds
128172 /// per request at one worker as <see cref="RunAsync"/> reports them, so a regression in one serializer's path or in the
129173 /// suspending path shows up on its own. The process-wide serializer is switched with
130174 /// <see cref="Config.SetSerializer(JsonRpcSerializer)"/> for each block and restored afterwards. A failure is printed
131- /// after the rows measured so far and is not thrown, so it cannot change the gate's exit code.
175+ /// after the rows measured so far and is not thrown, so it cannot change the gate's exit code. When
176+ /// <paramref name="report"/> is given, the rows measured and any failure are added to it for the results file.
132177 /// </summary>
133- internal static async Task ScaleDiagnosticsAsync ( Action < string > print , double seconds , int workers )
178+ internal static async Task ScaleDiagnosticsAsync ( Action < string > print , double seconds , int workers , JsonObject report = null )
134179 {
135180 var serializers = new Func < JsonRpcSerializer > [ ] { ( ) => JsmnSerializer . Instance , ( ) => new SystemTextJsonRpcSerializer ( ) , ( ) => new NewtonsoftJsonRpcSerializer ( ) } ;
136181 var rows = new List < string > ( ) ;
182+ var diagnostics = new JsonArray ( ) ;
137183 string where = "start" ;
138184 Exception failure = null ;
139185 var previous = Config . Serializer ;
@@ -164,6 +210,12 @@ internal static async Task ScaleDiagnosticsAsync(Action<string> print, double se
164210 var many = await Measure ( session , rowInputs , workers , seconds , processWide : yield ) ;
165211 print ( $ " { where } , { workers , 2 } workers: { many . RpcPerSec , 14 : N0} RPC/s ({ many . Count : N0} RPCs in { many . Seconds : F3} s); { many . Cpu . TrailingText } ") ;
166212 rows . Add ( $ "| { shape } / { flow } | { one . RpcPerSec : N0} | { many . RpcPerSec : N0} | { many . RpcPerSec / one . RpcPerSec : F2} | { one . BytesPerRpc : F0} | { many . Cpu . TrailingText } |") ;
213+ diagnostics . Add ( new JsonObject
214+ {
215+ [ "id" ] = RowId ( "scale-diag-" + name , shape , flow ) , [ "serializer" ] = name , [ "shape" ] = shape , [ "flow" ] = flow . ToString ( ) ,
216+ [ "one" ] = Math . Round ( one . RpcPerSec ) , [ "many" ] = Math . Round ( many . RpcPerSec ) , [ "ratio" ] = Math . Round ( many . RpcPerSec / one . RpcPerSec , 2 ) ,
217+ [ "bytesPerRpc" ] = Math . Round ( one . BytesPerRpc ) , [ "cpu" ] = BenchResults . Cpu ( many . Cpu ) ,
218+ } ) ;
167219 }
168220 finally { Handler . DestroySession ( session ) ; }
169221 }
@@ -179,6 +231,11 @@ internal static async Task ScaleDiagnosticsAsync(Action<string> print, double se
179231 print ( "" ) ;
180232 if ( failure != null )
181233 print ( $ "Diagnostics stopped at { where } : { failure . GetType ( ) . Name } : { failure . Message } . The gate result above stands; the diagnostics are not part of it.") ;
234+ if ( report != null )
235+ {
236+ report [ "diagnostics" ] = diagnostics ;
237+ if ( failure != null ) report [ "diagnosticsError" ] = $ "{ where } : { failure . GetType ( ) . Name } : { failure . Message } ";
238+ }
182239 }
183240
184241 private static double Median ( List < double > values )
@@ -204,8 +261,10 @@ private static ReadOnlyMemory<byte>[] Inputs(string shape) => shape == "yield"
204261
205262 /// <summary>Checks every response, warms the row, and prints the per-shape allocation of an inline document.</summary>
206263 /// <param name="report">False checks, warms and asserts inline completion without printing the per-shape lines.</param>
207- private static async Task ValidateAndReportAllocations ( Action < string > print , string session , string shape , RpcContextFlow flow , ReadOnlyMemory < byte > [ ] rowInputs , bool report = true )
264+ /// <returns>Bytes per request for each input, as printed; null for the <c>yield</c> row, which is not measured per shape.</returns>
265+ private static async Task < double [ ] > ValidateAndReportAllocations ( Action < string > print , string session , string shape , RpcContextFlow flow , ReadOnlyMemory < byte > [ ] rowInputs , bool report = true )
208266 {
267+ var perShape = shape == "yield" ? null : new double [ rowInputs . Length ] ;
209268 var expected = shape == "yield"
210269 ? new [ ] { "{\" jsonrpc\" :\" 2.0\" ,\" result\" :7,\" id\" :6}" }
211270 : BenchmarkRunner . taskInputs . Select ( t => JsonRpcProcessor . ProcessSync ( t ) ) . ToArray ( ) ;
@@ -227,7 +286,9 @@ private static async Task ValidateAndReportAllocations(Action<string> print, str
227286 }
228287 long totalBytes = GC . GetAllocatedBytesForCurrentThread ( ) - before ;
229288 if ( report ) print ( $ "{ shape } / { flow } / shape { i + 1 } : { totalBytes } B total over 2000 requests ({ totalBytes / 2000.0 : F1} B/RPC), including method allocations.") ;
289+ perShape [ i ] = Math . Round ( totalBytes / 2000.0 , 1 ) ;
230290 }
291+ return perShape ;
231292 }
232293
233294 /// <summary>
0 commit comments