Skip to content

Commit eec5f5e

Browse files
authored
Merge pull request #264 from liammclennan/bench-upgrades
Bench upgrades
2 parents 8780d38 + a17b450 commit eec5f5e

File tree

3 files changed

+31
-12
lines changed

3 files changed

+31
-12
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,6 @@ namespace SeqCli.Cli.Commands;
2323
class BenchCasesCollection
2424
{
2525
// An identifier for the particular cases file
26-
public int CasesHash = 0;
26+
public string CasesHash = "";
2727
public IList<BenchCase> Cases = new List<BenchCase>();
2828
}

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

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
using System;
1818
using System.IO;
1919
using System.Linq;
20+
using System.Security.Cryptography;
21+
using System.Text;
2022
using System.Threading.Tasks;
2123
using Newtonsoft.Json;
2224
using Seq.Api.Model.Signals;
@@ -27,7 +29,7 @@
2729
using Serilog.Context;
2830
using 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
}

test/SeqCli.EndToEnd/Bench/BenchTestCase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public Task ExecuteAsync(
1313
ILogger logger,
1414
CliCommandRunner runner)
1515
{
16-
var exit = runner.Exec("bench", "");
16+
var exit = runner.Exec("bench", "--start=2022-01-01 --end=2022-01-02");
1717
Assert.Equal(0, exit);
1818

1919
return Task.CompletedTask;

0 commit comments

Comments
 (0)