From cd8ea5fe45f05bf1d2084b3092aca0e67de1adee Mon Sep 17 00:00:00 2001 From: Jan Tattermusch Date: Fri, 15 May 2020 11:03:30 +0200 Subject: [PATCH] hit target qps with more precision --- .../Grpc.IntegrationTesting/XdsInteropClient.cs | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/csharp/Grpc.IntegrationTesting/XdsInteropClient.cs b/src/csharp/Grpc.IntegrationTesting/XdsInteropClient.cs index b988a41b25d..b9d7799fba6 100644 --- a/src/csharp/Grpc.IntegrationTesting/XdsInteropClient.cs +++ b/src/csharp/Grpc.IntegrationTesting/XdsInteropClient.cs @@ -18,6 +18,7 @@ using System; using System.Collections.Generic; +using System.Diagnostics; using System.Threading; using System.Threading.Tasks; @@ -37,6 +38,8 @@ namespace Grpc.IntegrationTesting public int NumChannels { get; set; } [Option("qps", Default = 1)] + + // The desired QPS per channel. public int Qps { get; set; } [Option("server", Default = "localhost:8080")] @@ -117,16 +120,26 @@ namespace Grpc.IntegrationTesting var client = new TestService.TestServiceClient(channel); var inflightTasks = new List(); - int millisPerQuery = (int)(1000.0 / options.Qps); // qps value is per-channel + long rpcsStarted = 0; + var stopwatch = Stopwatch.StartNew(); while (!cancellationToken.IsCancellationRequested) { inflightTasks.Add(RunSingleRpcAsync(client, cancellationToken)); + rpcsStarted++; + // only cleanup calls that have already completed, calls that are still inflight will be cleaned up later. await CleanupCompletedTasksAsync(inflightTasks); Console.WriteLine($"Currently {inflightTasks.Count} in-flight RPCs"); - await Task.Delay(millisPerQuery); // not accurate, but good enough for low QPS. + + // if needed, wait a bit before we start the next RPC. + int nextDueInMillis = (int) Math.Max(0, (1000 * rpcsStarted / options.Qps) - stopwatch.ElapsedMilliseconds); + if (nextDueInMillis > 0) + { + await Task.Delay(nextDueInMillis); + } } + stopwatch.Stop(); Console.WriteLine($"Shutting down channel {channelId}"); await channel.ShutdownAsync();