1717using System ;
1818using System . IO ;
1919using System . Linq ;
20+ using System . Security . Cryptography ;
21+ using System . Text ;
2022using System . Threading . Tasks ;
2123using Newtonsoft . Json ;
2224using Seq . Api . Model . Signals ;
2729using Serilog . Context ;
2830using Serilog . Core ;
2931
30- namespace SeqCli . Cli . Commands ;
32+ namespace SeqCli . Cli . Commands . Bench ;
3133
3234/*
3335 * Run performance benchmark tests against a Seq server.
@@ -69,6 +71,7 @@ class BenchCommand : Command
6971 string _cases = "" ;
7072 string _reportingServerUrl = "" ;
7173 string _reportingServerApiKey = "" ;
74+ string _description = "" ;
7275
7376 public BenchCommand ( SeqConnectionFactory connectionFactory )
7477 {
@@ -94,6 +97,10 @@ public BenchCommand(SeqConnectionFactory connectionFactory)
9497 "reporting-apikey=" ,
9598 "The API key to use when connecting to the reporting server" ,
9699 a => _reportingServerApiKey = a ) ;
100+ Options . Add (
101+ "description=" ,
102+ "Optional description of the bench test run" ,
103+ a => _description = a ) ;
97104 }
98105
99106 protected override async Task < int > Run ( )
@@ -104,8 +111,12 @@ protected override async Task<int> Run()
104111 using var reportingLogger = BuildReportingLogger ( ) ;
105112 var cases = ReadCases ( _cases ) ;
106113 var runId = Guid . NewGuid ( ) . ToString ( "N" ) . Substring ( 0 , 4 ) ;
107- var start = _range . Start ?? DateTime . UtcNow . AddDays ( - 7 ) ;
108- var end = _range . End ;
114+
115+ if ( _range . Start == null || _range . End == null )
116+ {
117+ Log . Error ( "Both the `start` and `end` arguments are required" ) ;
118+ return 1 ;
119+ }
109120
110121 foreach ( var c in cases . Cases )
111122 {
@@ -116,8 +127,8 @@ protected override async Task<int> Run()
116127 {
117128 var response = await connection . Data . QueryAsync (
118129 c . Query ,
119- start ,
120- end ,
130+ _range . Start ,
131+ _range . End ,
121132 SignalExpressionPart . Signal ( c . SignalExpression )
122133 ) ;
123134
@@ -137,15 +148,16 @@ protected override async Task<int> Run()
137148 using ( LogContext . PushProperty ( "MinElapsed" , timings . MinElapsed ) )
138149 using ( LogContext . PushProperty ( "MaxElapsed" , timings . MaxElapsed ) )
139150 using ( LogContext . PushProperty ( "Runs" , _runs ) )
140- using ( LogContext . PushProperty ( "SignalExpression" , c . SignalExpression ) )
141- using ( LogContext . PushProperty ( "Start" , start ) )
151+ using ( ! string . IsNullOrWhiteSpace ( c . SignalExpression ) ? LogContext . PushProperty ( "SignalExpression" , c . SignalExpression ) : null )
152+ using ( LogContext . PushProperty ( "Start" , _range . Start ) )
142153 using ( LogContext . PushProperty ( "StandardDeviationElapsed" , timings . StandardDeviationElapsed ) )
143- using ( end != null ? LogContext . PushProperty ( "End" , end ) : null )
154+ using ( LogContext . PushProperty ( "End" , _range . End ) )
144155 using ( LogContext . PushProperty ( "Query" , c . Query ) )
156+ using ( ! string . IsNullOrWhiteSpace ( _description ) ? LogContext . PushProperty ( "Description" , _description ) : null )
145157 {
146158 reportingLogger . Information (
147159 "Bench run {Cases}/{RunId} against {Server} for query {Id}: mean {MeanElapsed:N0} ms with relative dispersion {RelativeStandardDeviationElapsed:N2}" ,
148- cases . CasesHash , runId , _reportingServerUrl , c . Id , timings . MeanElapsed , timings . RelativeStandardDeviationElapsed ) ;
160+ cases . CasesHash , runId , _connection . Url , c . Id , timings . MeanElapsed , timings . RelativeStandardDeviationElapsed ) ;
149161 }
150162 }
151163
@@ -188,7 +200,7 @@ static BenchCasesCollection ReadCases(string filename)
188200 var casesFile = JsonConvert . DeserializeObject < BenchCasesCollection > ( casesString )
189201 ?? new BenchCasesCollection ( ) ;
190202
191- casesFile . CasesHash = casesString . GetHashCode ( ) ; // not consistent across framework versions, but that's OK
203+ casesFile . CasesHash = HashString ( casesString ) ;
192204
193205 if ( casesFile . Cases . Select ( c => c . Id ) . Distinct ( ) . Count ( ) != casesFile . Cases . Count )
194206 {
@@ -202,4 +214,11 @@ static BenchCasesCollection ReadCases(string filename)
202214
203215 return casesFile ;
204216 }
217+
218+ static string HashString ( string input )
219+ {
220+ using var md5 = MD5 . Create ( ) ;
221+ var bytes = Encoding . ASCII . GetBytes ( input ) ;
222+ return Convert . ToHexString ( md5 . ComputeHash ( bytes ) ) ;
223+ }
205224}
0 commit comments