|
| 1 | +// Copyright Datalust Pty Ltd and Contributors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#nullable enable |
| 16 | + |
| 17 | +using System; |
| 18 | +using System.IO; |
| 19 | +using System.Linq; |
| 20 | +using System.Threading.Tasks; |
| 21 | +using Newtonsoft.Json; |
| 22 | +using Seq.Api.Model.Signals; |
| 23 | +using SeqCli.Cli.Features; |
| 24 | +using SeqCli.Connection; |
| 25 | +using SeqCli.Util; |
| 26 | +using Serilog; |
| 27 | +using Serilog.Context; |
| 28 | +using Serilog.Core; |
| 29 | + |
| 30 | +namespace SeqCli.Cli.Commands; |
| 31 | + |
| 32 | +/* |
| 33 | + * Run performance benchmark tests against a Seq server. |
| 34 | + * |
| 35 | + * Requires test cases in a JSON file matching the format of `BenchCases.json`. |
| 36 | + * |
| 37 | + * If a Seq reporting server is configured this command logs data such as: |
| 38 | + * |
| 39 | + * { |
| 40 | + "@t": "2022-11-09T01:12:06.0293545Z", |
| 41 | + "@mt": "Bench run {BenchRunId} for query {Id}. Mean {MeanElapsed:N0} ms with relative dispersion {RelativeStandardDeviationElapsed:N2}", |
| 42 | + "@m": "Bench run \"2907\" for query \"with-signal\". Mean 4 ms with relative dispersion 0.06", |
| 43 | + "@i": "bb0c84a5", |
| 44 | + "@r": [ |
| 45 | + "4", |
| 46 | + "0.06" |
| 47 | + ], |
| 48 | + "BenchRunId": "2907", |
| 49 | + "End": "2022-08-15T00:00:00.0000000", |
| 50 | + "Id": "with-signal", |
| 51 | + "LastResult": 606, |
| 52 | + "MaxElapsed": 4.082, |
| 53 | + "MeanElapsed": 3.6676, |
| 54 | + "MinElapsed": 3.4334, |
| 55 | + "Query": "select count(*) from stream where @Level = 'Warning'", |
| 56 | + "RelativeStandardDeviationElapsed": 0.05619408341421253, |
| 57 | + "Runs": 10, |
| 58 | + "SignalExpression": "signal-m33302", |
| 59 | + "Start": "2022-08-14T16:00:00.0000000" |
| 60 | +} |
| 61 | + */ |
| 62 | +[Command("bench", @"Measure query performance. |
| 63 | +
|
| 64 | +Example cases file format: |
| 65 | + |
| 66 | +{ |
| 67 | + ""cases"": [ |
| 68 | + { |
| 69 | + ""id"": ""count-star"", |
| 70 | + ""query"": ""select count(*) from stream"", |
| 71 | + ""signalExpression"": ""signal-expression-here""] |
| 72 | + } |
| 73 | + ] |
| 74 | +} |
| 75 | +")] |
| 76 | +class BenchCommand : Command |
| 77 | +{ |
| 78 | + readonly SeqConnectionFactory _connectionFactory; |
| 79 | + int _runs = 3; |
| 80 | + readonly ConnectionFeature _connection; |
| 81 | + readonly DateRangeFeature _range; |
| 82 | + string _cases = ""; |
| 83 | + string _reportingServerUrl = ""; |
| 84 | + string _reportingServerApiKey = ""; |
| 85 | + |
| 86 | + public BenchCommand(SeqConnectionFactory connectionFactory) |
| 87 | + { |
| 88 | + _connectionFactory = connectionFactory; |
| 89 | + Options.Add("r|runs=", "The number of runs to execute", r => |
| 90 | + { |
| 91 | + int.TryParse(r, out _runs); |
| 92 | + }); |
| 93 | + |
| 94 | + Options.Add( |
| 95 | + "c|cases=", |
| 96 | + @"A JSON file containing the set of cases to run. Defaults to a standard set of cases.", |
| 97 | + c => _cases = c); |
| 98 | + |
| 99 | + _connection = Enable<ConnectionFeature>(); |
| 100 | + _range = Enable<DateRangeFeature>(); |
| 101 | + |
| 102 | + Options.Add( |
| 103 | + "reporting-server=", |
| 104 | + "The address of a Seq server to send bench results to", |
| 105 | + s => _reportingServerUrl = s); |
| 106 | + Options.Add( |
| 107 | + "reporting-apikey=", |
| 108 | + "The API key to use when connecting to the reporting server", |
| 109 | + a => _reportingServerApiKey = a); |
| 110 | + } |
| 111 | + |
| 112 | + protected override async Task<int> Run() |
| 113 | + { |
| 114 | + try |
| 115 | + { |
| 116 | + var connection = _connectionFactory.Connect(_connection); |
| 117 | + using var reportingLogger = BuildReportingLogger(); |
| 118 | + var cases = ReadCases(_cases); |
| 119 | + var runId = Guid.NewGuid().ToString("N").Substring(0, 4); |
| 120 | + var start = _range.Start ?? DateTime.UtcNow.AddDays(-7); |
| 121 | + var end = _range.End; |
| 122 | + |
| 123 | + foreach (var c in cases.Cases) |
| 124 | + { |
| 125 | + var timings = new BenchCaseTimings(); |
| 126 | + object? lastResult = null; |
| 127 | + |
| 128 | + foreach (var i in Enumerable.Range(1, _runs)) |
| 129 | + { |
| 130 | + var response = await connection.Data.QueryAsync( |
| 131 | + c.Query, |
| 132 | + start, |
| 133 | + end, |
| 134 | + SignalExpressionPart.Signal(c.SignalExpression) |
| 135 | + ); |
| 136 | + |
| 137 | + timings.PushElapsed(response.Statistics.ElapsedMilliseconds); |
| 138 | + |
| 139 | + if (response.Rows != null) |
| 140 | + { |
| 141 | + var isScalarResult = response.Rows.Length == 1 && response.Rows[0].Length == 1; |
| 142 | + if (isScalarResult && i == _runs) |
| 143 | + { |
| 144 | + lastResult = response.Rows[0][0]; |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + using (lastResult != null ? LogContext.PushProperty("LastResult", lastResult) : null) |
| 150 | + using (LogContext.PushProperty("MinElapsed", timings.MinElapsed)) |
| 151 | + using (LogContext.PushProperty("MaxElapsed", timings.MaxElapsed)) |
| 152 | + using (LogContext.PushProperty("Runs", _runs)) |
| 153 | + using (LogContext.PushProperty("SignalExpression", c.SignalExpression)) |
| 154 | + using (LogContext.PushProperty("Start", start)) |
| 155 | + using (LogContext.PushProperty("StandardDeviationElapsed", timings.StandardDeviationElapsed)) |
| 156 | + using (end != null ? LogContext.PushProperty("End", end) : null) |
| 157 | + using (LogContext.PushProperty("Query", c.Query)) |
| 158 | + { |
| 159 | + reportingLogger.Information( |
| 160 | + "Bench run {Cases}/{RunId} against {Server} for query {Id}: mean {MeanElapsed:N0} ms with relative dispersion {RelativeStandardDeviationElapsed:N2}", |
| 161 | + cases.CasesHash, runId, _reportingServerUrl, c.Id, timings.MeanElapsed, timings.RelativeStandardDeviationElapsed); |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + return 0; |
| 166 | + } |
| 167 | + catch (Exception ex) |
| 168 | + { |
| 169 | + Log.Error(ex, "Benchmarking failed: {ErrorMessage}", ex.Message); |
| 170 | + return 1; |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + /// <summary> |
| 175 | + /// Build a second Serilog logger for logging benchmark results. |
| 176 | + /// </summary> |
| 177 | + Logger BuildReportingLogger() |
| 178 | + { |
| 179 | + var loggerConfiguration = new LoggerConfiguration() |
| 180 | + .Enrich.FromLogContext() |
| 181 | + .WriteTo.Console(); |
| 182 | + |
| 183 | + if (!string.IsNullOrWhiteSpace(_reportingServerUrl)) |
| 184 | + loggerConfiguration.WriteTo.Seq( |
| 185 | + _reportingServerUrl, |
| 186 | + apiKey: string.IsNullOrWhiteSpace(_reportingServerApiKey) ? null : _reportingServerApiKey, |
| 187 | + period: TimeSpan.FromMilliseconds(1)); |
| 188 | + |
| 189 | + return loggerConfiguration.CreateLogger(); |
| 190 | + } |
| 191 | + |
| 192 | + /// <summary> |
| 193 | + /// Read and parse the bench test cases from the file supplied or else from a default file. |
| 194 | + /// </summary> |
| 195 | + static BenchCasesCollection ReadCases(string filename) |
| 196 | + { |
| 197 | + var defaultCasesPath = Content.GetPath("Cli/Commands/Bench/BenchCases.json"); |
| 198 | + var casesString = File.ReadAllText(string.IsNullOrWhiteSpace(filename) |
| 199 | + ? defaultCasesPath |
| 200 | + : filename); |
| 201 | + var casesFile = JsonConvert.DeserializeObject<BenchCasesCollection>(casesString) |
| 202 | + ?? new BenchCasesCollection(); |
| 203 | + |
| 204 | + casesFile.CasesHash = casesString.GetHashCode(); // not consistent across framework versions, but that's OK |
| 205 | + |
| 206 | + if (casesFile.Cases.Select(c => c.Id).Distinct().Count() != casesFile.Cases.Count) |
| 207 | + { |
| 208 | + throw new Exception($"Cases file {filename} contains a duplicate id"); |
| 209 | + } |
| 210 | + |
| 211 | + if (!casesFile.Cases.Any()) |
| 212 | + { |
| 213 | + throw new Exception($"Cases file {filename} contains no cases"); |
| 214 | + } |
| 215 | + |
| 216 | + return casesFile; |
| 217 | + } |
| 218 | +} |
0 commit comments