Skip to content

Commit 63e7184

Browse files
committed
refactor
1 parent 1b757d0 commit 63e7184

File tree

3 files changed

+73
-58
lines changed

3 files changed

+73
-58
lines changed

src/SeqCli/Cli/Commands/Bench/BenchCommand.cs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,12 @@ public BenchCommand(SeqConnectionFactory connectionFactory)
113113

114114
protected override async Task<int> Run()
115115
{
116+
if (!_withIngestion && !_withQueries)
117+
{
118+
Log.Error("Use at least one of --with-ingestion and --with-queries");
119+
return 1;
120+
}
121+
116122
try
117123
{
118124
var (_, apiKey) = _connectionFactory.GetConnectionDetails(_connection);
@@ -138,12 +144,14 @@ protected override async Task<int> Run()
138144
{
139145
var t = IngestionBenchmark(reportingLogger, runId, connection, apiKey, seqVersion,
140146
isQueryBench: _withQueries, cancellationToken)
141-
.ContinueWith(async t =>
147+
.ContinueWith(t =>
142148
{
143149
if (t.Exception is not null)
144150
{
145-
await Console.Error.WriteLineAsync(t.Exception.Message);
151+
return Console.Error.WriteLineAsync(t.Exception.Message);
146152
}
153+
154+
return Task.CompletedTask;
147155
});
148156

149157
if (!_withQueries)
@@ -223,20 +231,20 @@ async Task IngestionBenchmark(Logger reportingLogger, string runId, SeqConnectio
223231
}
224232
}
225233

226-
async Task<CollectedTimings> QueryBenchmark(Logger reportingLogger, string runId, SeqConnection connection, string seqVersion)
234+
async Task<QueryBenchRunResults> QueryBenchmark(Logger reportingLogger, string runId, SeqConnection connection, string seqVersion)
227235
{
228236
var cases = ReadCases(_cases);
229-
CollectedTimings collectedTimings = new(reportingLogger);
237+
QueryBenchRunResults queryBenchRunResults = new(reportingLogger);
230238
reportingLogger.Information(
231239
"Query benchmark run {RunId} against {ServerUrl} ({SeqVersion}); {CaseCount} cases, {Runs} runs, from {Start} to {End}",
232240
runId, connection.Client.ServerUrl, seqVersion, cases.Cases.Count, _runs, _range.Start, _range.End);
233241

234242

235243
foreach (var c in cases.Cases.OrderBy(c => c.Id)
236-
.Concat(new [] { CollectedTimings.FINAL_COUNT_CASE }))
244+
.Concat(new [] { QueryBenchRunResults.FINAL_COUNT_CASE }))
237245
{
238246
var timings = new QueryBenchCaseTimings(c);
239-
collectedTimings.Add(timings);
247+
queryBenchRunResults.Add(timings);
240248

241249
foreach (var i in Enumerable.Range(1, _runs))
242250
{
@@ -274,7 +282,7 @@ async Task<CollectedTimings> QueryBenchmark(Logger reportingLogger, string runId
274282
}
275283
}
276284

277-
return collectedTimings;
285+
return queryBenchRunResults;
278286
}
279287

280288
/// <summary>

src/SeqCli/Cli/Commands/Bench/QueryBenchCaseTimings.cs

Lines changed: 3 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -15,61 +15,13 @@
1515
using System;
1616
using System.Collections.Generic;
1717
using System.Linq;
18-
using Serilog.Core;
1918

2019
namespace SeqCli.Cli.Commands.Bench;
2120

22-
class CollectedTimings
23-
{
24-
readonly Logger _reportingLogger;
25-
List<QueryBenchCaseTimings> _collectedTimings = new();
26-
27-
public static QueryBenchCase FINAL_COUNT_CASE = new QueryBenchCase()
28-
{
29-
Id = "final-count-star",
30-
Query = "select count(*) from stream",
31-
};
32-
33-
public CollectedTimings(Logger reportingLogger)
34-
{
35-
_reportingLogger = reportingLogger;
36-
}
37-
38-
public void Add(QueryBenchCaseTimings caseTimings)
39-
{
40-
_collectedTimings.Add(caseTimings);
41-
}
42-
43-
public void LogSummary(string description)
44-
{
45-
_reportingLogger.Information(
46-
"Query benchmark {Description} complete in {TotalMeanElapsed:N0} ms with {MeanRelativeStandardDeviationPercentage:N1}% deviation, processed {FinalEventCount:N0} events at {EventsPerMs:N0} events/ms",
47-
description,
48-
TotalMeanElapsed(),
49-
MeanRelativeStandardDeviationPercentage(),
50-
FinalEventCount(),
51-
FinalEventCount() * _collectedTimings.Count / TotalMeanElapsed());
52-
}
53-
54-
private double TotalMeanElapsed()
55-
{
56-
return _collectedTimings.Sum(c => c.MeanElapsed);
57-
}
58-
59-
private double MeanRelativeStandardDeviationPercentage()
60-
{
61-
return _collectedTimings.Average(c => c.RelativeStandardDeviationElapsed) * 100;
62-
}
63-
64-
private int FinalEventCount()
65-
{
66-
var benchCase = _collectedTimings.Single(c => c.Id == FINAL_COUNT_CASE.Id);
67-
return Convert.ToInt32(benchCase.LastResult);
68-
}
69-
}
70-
7121
/*
72-
* Collects benchmarking elapsed time measurements and calculates statistics.
22+
* Collects benchmarking elapsed time measurements and calculates statistics.
23+
*
24+
* The results for one bench case.
7325
*/
7426
class QueryBenchCaseTimings
7527
{
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using Serilog.Core;
5+
6+
namespace SeqCli.Cli.Commands.Bench;
7+
8+
class QueryBenchRunResults
9+
{
10+
readonly Logger _reportingLogger;
11+
List<QueryBenchCaseTimings> _collectedTimings = new();
12+
13+
public static QueryBenchCase FINAL_COUNT_CASE = new()
14+
{
15+
Id = "final-count-star",
16+
Query = "select count(*) from stream",
17+
};
18+
19+
public QueryBenchRunResults(Logger reportingLogger)
20+
{
21+
_reportingLogger = reportingLogger;
22+
}
23+
24+
public void Add(QueryBenchCaseTimings caseTimings)
25+
{
26+
_collectedTimings.Add(caseTimings);
27+
}
28+
29+
public void LogSummary(string description)
30+
{
31+
_reportingLogger.Information(
32+
"Query benchmark {Description} complete in {TotalMeanElapsed:N0} ms with {MeanRelativeStandardDeviationPercentage:N1}% deviation, processed {FinalEventCount:N0} events at {EventsPerMs:N0} events/ms",
33+
description,
34+
TotalMeanElapsed(),
35+
MeanRelativeStandardDeviationPercentage(),
36+
FinalEventCount(),
37+
FinalEventCount() * _collectedTimings.Count / TotalMeanElapsed());
38+
}
39+
40+
private double TotalMeanElapsed()
41+
{
42+
return _collectedTimings.Sum(c => c.MeanElapsed);
43+
}
44+
45+
private double MeanRelativeStandardDeviationPercentage()
46+
{
47+
return _collectedTimings.Average(c => c.RelativeStandardDeviationElapsed) * 100;
48+
}
49+
50+
private int FinalEventCount()
51+
{
52+
var benchCase = _collectedTimings.Single(c => c.Id == FINAL_COUNT_CASE.Id);
53+
return Convert.ToInt32(benchCase.LastResult);
54+
}
55+
}

0 commit comments

Comments
 (0)