Skip to content

Commit

Permalink
chore: benchmark StreamingHub latency was not correct, send metrics e…
Browse files Browse the repository at this point in the history
…ach round
  • Loading branch information
guitarrapc committed Aug 13, 2024
1 parent 856fddf commit ec52b2b
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ public class PerformanceTestRunningContext
int count;
bool isRunning;
Stopwatch stopwatch;
List<List<double>> latencyPerConnection = [];
List<object> locks = [];
List<List<double>> latencyPerConnection;
List<object> locks;

public PerformanceTestRunningContext(int connectionCount)
{
stopwatch = new Stopwatch();
latencyPerConnection = new (connectionCount);
locks = new (connectionCount);
for (var i = 0; i < connectionCount; i++)
{
latencyPerConnection.Add([]);
Expand Down Expand Up @@ -49,6 +51,8 @@ public void Complete()
public PerformanceResult GetResult()
{
var latency = MeasureLatency();
latencyPerConnection.Clear();
locks.Clear();
return new PerformanceResult(count, count / (double)stopwatch.Elapsed.TotalSeconds, stopwatch.Elapsed, latency);

Latency MeasureLatency()
Expand All @@ -66,7 +70,7 @@ Latency MeasureLatency()
latencyPerConnection[i].Sort();
}
var latencyMean = (totalCount != 0) ? totalSum / totalCount : totalSum;
var latencyAllConnection = new List<double>();
var latencyAllConnection = new List<double>(totalCount);
foreach (var connections in latencyPerConnection) latencyAllConnection.AddRange(connections);
var latency50p = GetPercentile(50, latencyAllConnection);
var latency75p = GetPercentile(75, latencyAllConnection);
Expand Down
9 changes: 5 additions & 4 deletions perf/BenchmarkApp/PerformanceTest.Client/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ async Task Main(
}
var result = await RunScenarioAsync(scenario2, config, controlServiceClient);
results.Add(result);
datadog.PutClientBenchmarkMetrics(scenario.ToString(), ApplicationInformation.Current, serialization.ToString(), result);
await datadog.PutClientBenchmarkMetricsAsync(scenario.ToString(), ApplicationInformation.Current, serialization.ToString(), result);
}
}

Expand Down Expand Up @@ -136,8 +136,6 @@ async Task Main(
writer.WriteLine($"{s}\t{string.Join("\t", results.Select(x => x.RequestsPerSecond.ToString("0.000")))}\t{results.Average(x => x.RequestsPerSecond):0.000}");
}
}

await datadog.WaitSaveAsync();
}

async Task<PerformanceResult> RunScenarioAsync(ScenarioType scenario, ScenarioConfiguration config, IPerfTestControlService controlService)
Expand Down Expand Up @@ -265,7 +263,7 @@ public static class DatadogMetricsRecorderExtensions
/// <param name="applicationInfo"></param>
/// <param name="serialization"></param>
/// <param name="result"></param>
public static void PutClientBenchmarkMetrics(this DatadogMetricsRecorder recorder, string scenario, ApplicationInformation applicationInfo, string serialization, PerformanceResult result)
public static async Task PutClientBenchmarkMetricsAsync(this DatadogMetricsRecorder recorder, string scenario, ApplicationInformation applicationInfo, string serialization, PerformanceResult result)
{
var tags = MetricsTagCache.Get((scenario, applicationInfo, serialization), static x => [$"app:MagicOnion", $"magiconion_version:{x.applicationInfo.MagicOnionVersion}", $"grpcdotnet_version:{x.applicationInfo.GrpcNetVersion}", $"messagepack_version:{x.applicationInfo.MessagePackVersion}", $"memorypack_version:{x.applicationInfo.MemoryPackVersion}", $"process_arch:{x.applicationInfo.ProcessArchitecture}", $"process_count:{x.applicationInfo.ProcessorCount}", $"scenario:{x.scenario}", $"serialization:{x.serialization}"]);

Expand All @@ -279,6 +277,9 @@ public static void PutClientBenchmarkMetrics(this DatadogMetricsRecorder recorde
recorder.Record(recorder.SendAsync("benchmark.client.latency_p75", result.Latency.P75, DatadogMetricsType.Gauge, tags, "millisecond"));
recorder.Record(recorder.SendAsync("benchmark.client.latency_p90", result.Latency.P90, DatadogMetricsType.Gauge, tags, "millisecond"));
recorder.Record(recorder.SendAsync("benchmark.client.latency_p99", result.Latency.P99, DatadogMetricsType.Gauge, tags, "millisecond"));

// wait until send complete
await recorder.WaitSaveAsync();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,20 @@ public class StreamingHubScenario : IScenario, IPerfTestHubReceiver
{
IPerfTestHub client = default!;
readonly TimeProvider timeProvider = TimeProvider.System;
long beginTimeStamp = default;

public async ValueTask PrepareAsync(GrpcChannel channel)
{
this.client = await StreamingHubClient.ConnectAsync<IPerfTestHub, IPerfTestHubReceiver>(channel, this);
this.beginTimeStamp = timeProvider.GetTimestamp();
}

public async ValueTask RunAsync(int connectionId, PerformanceTestRunningContext ctx, CancellationToken cancellationToken)
{
while (!cancellationToken.IsCancellationRequested)
{
var begin = timeProvider.GetTimestamp();
await client.CallMethodAsync("FooBarBaz🚀こんにちは世界", 123, 4567, 891011);
ctx.Increment();
ctx.Latency(connectionId, timeProvider.GetElapsedTime(this.beginTimeStamp));
ctx.Latency(connectionId, timeProvider.GetElapsedTime(begin));
}
}
}
Expand Down

0 comments on commit ec52b2b

Please sign in to comment.