Jan Tattermusch | d0c1bfa | 2015-10-22 19:14:57 -0700 | [diff] [blame] | 1 | #region Copyright notice and license |
| 2 | |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 3 | // Copyright 2015 gRPC authors. |
Jan Tattermusch | d0c1bfa | 2015-10-22 19:14:57 -0700 | [diff] [blame] | 4 | // |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 5 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | // you may not use this file except in compliance with the License. |
| 7 | // You may obtain a copy of the License at |
Jan Tattermusch | d0c1bfa | 2015-10-22 19:14:57 -0700 | [diff] [blame] | 8 | // |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 9 | // http://www.apache.org/licenses/LICENSE-2.0 |
Jan Tattermusch | d0c1bfa | 2015-10-22 19:14:57 -0700 | [diff] [blame] | 10 | // |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 11 | // Unless required by applicable law or agreed to in writing, software |
| 12 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | // See the License for the specific language governing permissions and |
| 15 | // limitations under the License. |
Jan Tattermusch | d0c1bfa | 2015-10-22 19:14:57 -0700 | [diff] [blame] | 16 | |
| 17 | #endregion |
| 18 | |
| 19 | using System; |
Jan Tattermusch | 09c5e05 | 2016-05-18 22:24:27 -0700 | [diff] [blame] | 20 | using System.Collections.Concurrent; |
Jan Tattermusch | d0c1bfa | 2015-10-22 19:14:57 -0700 | [diff] [blame] | 21 | using System.Collections.Generic; |
| 22 | using System.Diagnostics; |
| 23 | using System.IO; |
| 24 | using System.Linq; |
| 25 | using System.Text.RegularExpressions; |
| 26 | using System.Threading; |
| 27 | using System.Threading.Tasks; |
| 28 | using Google.Protobuf; |
| 29 | using Grpc.Core; |
Jan Tattermusch | 09c5e05 | 2016-05-18 22:24:27 -0700 | [diff] [blame] | 30 | using Grpc.Core.Internal; |
Jan Tattermusch | 3d6644a | 2016-03-21 09:46:15 -0700 | [diff] [blame] | 31 | using Grpc.Core.Logging; |
Jan Tattermusch | 09c5e05 | 2016-05-18 22:24:27 -0700 | [diff] [blame] | 32 | using Grpc.Core.Profiling; |
Jan Tattermusch | d0c1bfa | 2015-10-22 19:14:57 -0700 | [diff] [blame] | 33 | using Grpc.Core.Utils; |
| 34 | using NUnit.Framework; |
| 35 | using Grpc.Testing; |
| 36 | |
| 37 | namespace Grpc.IntegrationTesting |
| 38 | { |
| 39 | /// <summary> |
| 40 | /// Helper methods to start client runners for performance testing. |
| 41 | /// </summary> |
Jan Tattermusch | 3d6644a | 2016-03-21 09:46:15 -0700 | [diff] [blame] | 42 | public class ClientRunners |
Jan Tattermusch | d0c1bfa | 2015-10-22 19:14:57 -0700 | [diff] [blame] | 43 | { |
Jan Tattermusch | 3d6644a | 2016-03-21 09:46:15 -0700 | [diff] [blame] | 44 | static readonly ILogger Logger = GrpcEnvironment.Logger.ForType<ClientRunners>(); |
| 45 | |
Jan Tattermusch | 09c5e05 | 2016-05-18 22:24:27 -0700 | [diff] [blame] | 46 | // Profilers to use for clients. |
| 47 | static readonly BlockingCollection<BasicProfiler> profilers = new BlockingCollection<BasicProfiler>(); |
| 48 | |
| 49 | internal static void AddProfiler(BasicProfiler profiler) |
| 50 | { |
| 51 | GrpcPreconditions.CheckNotNull(profiler); |
| 52 | profilers.Add(profiler); |
| 53 | } |
| 54 | |
Jan Tattermusch | d0c1bfa | 2015-10-22 19:14:57 -0700 | [diff] [blame] | 55 | /// <summary> |
| 56 | /// Creates a started client runner. |
| 57 | /// </summary> |
| 58 | public static IClientRunner CreateStarted(ClientConfig config) |
| 59 | { |
Jan Tattermusch | 3d6644a | 2016-03-21 09:46:15 -0700 | [diff] [blame] | 60 | Logger.Debug("ClientConfig: {0}", config); |
Jan Tattermusch | 3d6644a | 2016-03-21 09:46:15 -0700 | [diff] [blame] | 61 | |
Jan Tattermusch | 3d6644a | 2016-03-21 09:46:15 -0700 | [diff] [blame] | 62 | if (config.AsyncClientThreads != 0) |
| 63 | { |
| 64 | Logger.Warning("ClientConfig.AsyncClientThreads is not supported for C#. Ignoring the value"); |
| 65 | } |
| 66 | if (config.CoreLimit != 0) |
| 67 | { |
| 68 | Logger.Warning("ClientConfig.CoreLimit is not supported for C#. Ignoring the value"); |
| 69 | } |
| 70 | if (config.CoreList.Count > 0) |
| 71 | { |
| 72 | Logger.Warning("ClientConfig.CoreList is not supported for C#. Ignoring the value"); |
| 73 | } |
Jan Tattermusch | d0c1bfa | 2015-10-22 19:14:57 -0700 | [diff] [blame] | 74 | |
Jan Tattermusch | ea6cc57 | 2017-11-16 18:04:37 +0100 | [diff] [blame] | 75 | var channels = CreateChannels(config.ClientChannels, config.ServerTargets, config.SecurityParams, config.ChannelArgs); |
Jan Tattermusch | e81adf3 | 2016-04-04 15:59:50 -0700 | [diff] [blame] | 76 | |
| 77 | return new ClientRunnerImpl(channels, |
| 78 | config.ClientType, |
| 79 | config.RpcType, |
| 80 | config.OutstandingRpcsPerChannel, |
| 81 | config.LoadParams, |
| 82 | config.PayloadConfig, |
Jan Tattermusch | 09c5e05 | 2016-05-18 22:24:27 -0700 | [diff] [blame] | 83 | config.HistogramParams, |
| 84 | () => GetNextProfiler()); |
Jan Tattermusch | e81adf3 | 2016-04-04 15:59:50 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Jan Tattermusch | ea6cc57 | 2017-11-16 18:04:37 +0100 | [diff] [blame] | 87 | private static List<Channel> CreateChannels(int clientChannels, IEnumerable<string> serverTargets, SecurityParams securityParams, IEnumerable<ChannelArg> channelArguments) |
Jan Tattermusch | e81adf3 | 2016-04-04 15:59:50 -0700 | [diff] [blame] | 88 | { |
| 89 | GrpcPreconditions.CheckArgument(clientChannels > 0, "clientChannels needs to be at least 1."); |
| 90 | GrpcPreconditions.CheckArgument(serverTargets.Count() > 0, "at least one serverTarget needs to be specified."); |
| 91 | |
| 92 | var credentials = securityParams != null ? TestCredentials.CreateSslCredentials() : ChannelCredentials.Insecure; |
Jan Tattermusch | ea6cc57 | 2017-11-16 18:04:37 +0100 | [diff] [blame] | 93 | var channelOptions = new List<ChannelOption>(); |
Jan Tattermusch | e81adf3 | 2016-04-04 15:59:50 -0700 | [diff] [blame] | 94 | if (securityParams != null && securityParams.ServerHostOverride != "") |
Jan Tattermusch | 3d6644a | 2016-03-21 09:46:15 -0700 | [diff] [blame] | 95 | { |
Jan Tattermusch | ea6cc57 | 2017-11-16 18:04:37 +0100 | [diff] [blame] | 96 | channelOptions.Add(new ChannelOption(ChannelOptions.SslTargetNameOverride, securityParams.ServerHostOverride)); |
| 97 | } |
| 98 | foreach (var channelArgument in channelArguments) |
| 99 | { |
| 100 | channelOptions.Add(channelArgument.ToChannelOption()); |
Jan Tattermusch | 3d6644a | 2016-03-21 09:46:15 -0700 | [diff] [blame] | 101 | } |
Jan Tattermusch | d0c1bfa | 2015-10-22 19:14:57 -0700 | [diff] [blame] | 102 | |
Jan Tattermusch | e81adf3 | 2016-04-04 15:59:50 -0700 | [diff] [blame] | 103 | var result = new List<Channel>(); |
| 104 | for (int i = 0; i < clientChannels; i++) |
| 105 | { |
| 106 | var target = serverTargets.ElementAt(i % serverTargets.Count()); |
| 107 | var channel = new Channel(target, credentials, channelOptions); |
| 108 | result.Add(channel); |
| 109 | } |
| 110 | return result; |
Jan Tattermusch | d0c1bfa | 2015-10-22 19:14:57 -0700 | [diff] [blame] | 111 | } |
Jan Tattermusch | 09c5e05 | 2016-05-18 22:24:27 -0700 | [diff] [blame] | 112 | |
| 113 | private static BasicProfiler GetNextProfiler() |
| 114 | { |
| 115 | BasicProfiler result = null; |
| 116 | profilers.TryTake(out result); |
| 117 | return result; |
| 118 | } |
Jan Tattermusch | d0c1bfa | 2015-10-22 19:14:57 -0700 | [diff] [blame] | 119 | } |
| 120 | |
Jan Tattermusch | 09c5e05 | 2016-05-18 22:24:27 -0700 | [diff] [blame] | 121 | internal class ClientRunnerImpl : IClientRunner |
Jan Tattermusch | d0c1bfa | 2015-10-22 19:14:57 -0700 | [diff] [blame] | 122 | { |
Jan Tattermusch | 18ce9d6 | 2015-11-18 15:41:10 -0800 | [diff] [blame] | 123 | const double SecondsToNanos = 1e9; |
| 124 | |
Jan Tattermusch | e81adf3 | 2016-04-04 15:59:50 -0700 | [diff] [blame] | 125 | readonly List<Channel> channels; |
Jan Tattermusch | e26e2e5 | 2016-03-21 13:52:50 -0700 | [diff] [blame] | 126 | readonly ClientType clientType; |
| 127 | readonly RpcType rpcType; |
Jan Tattermusch | e45ca5f | 2016-03-21 14:59:08 -0700 | [diff] [blame] | 128 | readonly PayloadConfig payloadConfig; |
Jan Tattermusch | d7079b2 | 2017-05-16 19:55:57 +0200 | [diff] [blame] | 129 | readonly Lazy<byte[]> cachedByteBufferRequest; |
Jan Tattermusch | 19e3d3b | 2017-05-17 11:05:06 +0200 | [diff] [blame] | 130 | readonly ThreadLocal<Histogram> threadLocalHistogram; |
Jan Tattermusch | d0c1bfa | 2015-10-22 19:14:57 -0700 | [diff] [blame] | 131 | |
Jan Tattermusch | e81adf3 | 2016-04-04 15:59:50 -0700 | [diff] [blame] | 132 | readonly List<Task> runnerTasks; |
| 133 | readonly CancellationTokenSource stoppedCts = new CancellationTokenSource(); |
Jan Tattermusch | d0c1bfa | 2015-10-22 19:14:57 -0700 | [diff] [blame] | 134 | readonly WallClockStopwatch wallClockStopwatch = new WallClockStopwatch(); |
Jan Tattermusch | 09c5e05 | 2016-05-18 22:24:27 -0700 | [diff] [blame] | 135 | readonly AtomicCounter statsResetCount = new AtomicCounter(); |
Jan Tattermusch | d0c1bfa | 2015-10-22 19:14:57 -0700 | [diff] [blame] | 136 | |
Jan Tattermusch | 09c5e05 | 2016-05-18 22:24:27 -0700 | [diff] [blame] | 137 | public ClientRunnerImpl(List<Channel> channels, ClientType clientType, RpcType rpcType, int outstandingRpcsPerChannel, LoadParams loadParams, PayloadConfig payloadConfig, HistogramParams histogramParams, Func<BasicProfiler> profilerFactory) |
Jan Tattermusch | d0c1bfa | 2015-10-22 19:14:57 -0700 | [diff] [blame] | 138 | { |
Jan Tattermusch | e81adf3 | 2016-04-04 15:59:50 -0700 | [diff] [blame] | 139 | GrpcPreconditions.CheckArgument(outstandingRpcsPerChannel > 0, "outstandingRpcsPerChannel"); |
Jan Tattermusch | 8c839e8 | 2016-04-12 15:12:08 -0700 | [diff] [blame] | 140 | GrpcPreconditions.CheckNotNull(histogramParams, "histogramParams"); |
Jan Tattermusch | e81adf3 | 2016-04-04 15:59:50 -0700 | [diff] [blame] | 141 | this.channels = new List<Channel>(channels); |
Jan Tattermusch | e26e2e5 | 2016-03-21 13:52:50 -0700 | [diff] [blame] | 142 | this.clientType = clientType; |
| 143 | this.rpcType = rpcType; |
Jan Tattermusch | e45ca5f | 2016-03-21 14:59:08 -0700 | [diff] [blame] | 144 | this.payloadConfig = payloadConfig; |
Jan Tattermusch | d7079b2 | 2017-05-16 19:55:57 +0200 | [diff] [blame] | 145 | this.cachedByteBufferRequest = new Lazy<byte[]>(() => new byte[payloadConfig.BytebufParams.ReqSize]); |
Jan Tattermusch | 19e3d3b | 2017-05-17 11:05:06 +0200 | [diff] [blame] | 146 | this.threadLocalHistogram = new ThreadLocal<Histogram>(() => new Histogram(histogramParams.Resolution, histogramParams.MaxPossible), true); |
Jan Tattermusch | d0c1bfa | 2015-10-22 19:14:57 -0700 | [diff] [blame] | 147 | |
Jan Tattermusch | e81adf3 | 2016-04-04 15:59:50 -0700 | [diff] [blame] | 148 | this.runnerTasks = new List<Task>(); |
| 149 | foreach (var channel in this.channels) |
| 150 | { |
| 151 | for (int i = 0; i < outstandingRpcsPerChannel; i++) |
| 152 | { |
| 153 | var timer = CreateTimer(loadParams, 1.0 / this.channels.Count / outstandingRpcsPerChannel); |
Jan Tattermusch | 09c5e05 | 2016-05-18 22:24:27 -0700 | [diff] [blame] | 154 | var optionalProfiler = profilerFactory(); |
| 155 | this.runnerTasks.Add(RunClientAsync(channel, timer, optionalProfiler)); |
Jan Tattermusch | e81adf3 | 2016-04-04 15:59:50 -0700 | [diff] [blame] | 156 | } |
| 157 | } |
Jan Tattermusch | d0c1bfa | 2015-10-22 19:14:57 -0700 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | public ClientStats GetStats(bool reset) |
| 161 | { |
Jan Tattermusch | 19e3d3b | 2017-05-17 11:05:06 +0200 | [diff] [blame] | 162 | var histogramData = new HistogramData(); |
| 163 | foreach (var hist in threadLocalHistogram.Values) |
| 164 | { |
| 165 | hist.GetSnapshot(histogramData, reset); |
| 166 | } |
| 167 | |
Jan Tattermusch | d0c1bfa | 2015-10-22 19:14:57 -0700 | [diff] [blame] | 168 | var secondsElapsed = wallClockStopwatch.GetElapsedSnapshot(reset).TotalSeconds; |
| 169 | |
Jan Tattermusch | 09c5e05 | 2016-05-18 22:24:27 -0700 | [diff] [blame] | 170 | if (reset) |
| 171 | { |
| 172 | statsResetCount.Increment(); |
| 173 | } |
| 174 | |
Jan Tattermusch | 8251fdd | 2017-06-01 19:26:12 +0200 | [diff] [blame] | 175 | GrpcEnvironment.Logger.Info("[ClientRunnerImpl.GetStats] GC collection counts: gen0 {0}, gen1 {1}, gen2 {2}, (histogram reset count:{3}, seconds since reset: {4})", |
| 176 | GC.CollectionCount(0), GC.CollectionCount(1), GC.CollectionCount(2), statsResetCount.Count, secondsElapsed); |
Jan Tattermusch | 01ce236 | 2017-04-25 20:53:51 +0200 | [diff] [blame] | 177 | |
Jan Tattermusch | d0c1bfa | 2015-10-22 19:14:57 -0700 | [diff] [blame] | 178 | // TODO: populate user time and system time |
| 179 | return new ClientStats |
| 180 | { |
| 181 | Latencies = histogramData, |
| 182 | TimeElapsed = secondsElapsed, |
| 183 | TimeUser = 0, |
| 184 | TimeSystem = 0 |
| 185 | }; |
| 186 | } |
| 187 | |
| 188 | public async Task StopAsync() |
| 189 | { |
| 190 | stoppedCts.Cancel(); |
Jan Tattermusch | e81adf3 | 2016-04-04 15:59:50 -0700 | [diff] [blame] | 191 | foreach (var runnerTask in runnerTasks) |
| 192 | { |
| 193 | await runnerTask; |
| 194 | } |
| 195 | foreach (var channel in channels) |
| 196 | { |
| 197 | await channel.ShutdownAsync(); |
| 198 | } |
Jan Tattermusch | d0c1bfa | 2015-10-22 19:14:57 -0700 | [diff] [blame] | 199 | } |
| 200 | |
Jan Tattermusch | 09c5e05 | 2016-05-18 22:24:27 -0700 | [diff] [blame] | 201 | private void RunUnary(Channel channel, IInterarrivalTimer timer, BasicProfiler optionalProfiler) |
Jan Tattermusch | d0c1bfa | 2015-10-22 19:14:57 -0700 | [diff] [blame] | 202 | { |
Jan Tattermusch | 09c5e05 | 2016-05-18 22:24:27 -0700 | [diff] [blame] | 203 | if (optionalProfiler != null) |
| 204 | { |
| 205 | Profilers.SetForCurrentThread(optionalProfiler); |
| 206 | } |
| 207 | |
| 208 | bool profilerReset = false; |
| 209 | |
Jan Tattermusch | f41ebc3 | 2016-06-22 12:47:14 -0700 | [diff] [blame] | 210 | var client = new BenchmarkService.BenchmarkServiceClient(channel); |
Jan Tattermusch | e26e2e5 | 2016-03-21 13:52:50 -0700 | [diff] [blame] | 211 | var request = CreateSimpleRequest(); |
Jan Tattermusch | d0c1bfa | 2015-10-22 19:14:57 -0700 | [diff] [blame] | 212 | var stopwatch = new Stopwatch(); |
| 213 | |
| 214 | while (!stoppedCts.Token.IsCancellationRequested) |
| 215 | { |
Jan Tattermusch | 09c5e05 | 2016-05-18 22:24:27 -0700 | [diff] [blame] | 216 | // after the first stats reset, also reset the profiler. |
| 217 | if (optionalProfiler != null && !profilerReset && statsResetCount.Count > 0) |
| 218 | { |
| 219 | optionalProfiler.Reset(); |
| 220 | profilerReset = true; |
| 221 | } |
| 222 | |
Jan Tattermusch | d0c1bfa | 2015-10-22 19:14:57 -0700 | [diff] [blame] | 223 | stopwatch.Restart(); |
| 224 | client.UnaryCall(request); |
| 225 | stopwatch.Stop(); |
| 226 | |
Jan Tattermusch | 18ce9d6 | 2015-11-18 15:41:10 -0800 | [diff] [blame] | 227 | // spec requires data point in nanoseconds. |
Jan Tattermusch | 19e3d3b | 2017-05-17 11:05:06 +0200 | [diff] [blame] | 228 | threadLocalHistogram.Value.AddObservation(stopwatch.Elapsed.TotalSeconds * SecondsToNanos); |
Jan Tattermusch | e81adf3 | 2016-04-04 15:59:50 -0700 | [diff] [blame] | 229 | |
| 230 | timer.WaitForNext(); |
Jan Tattermusch | d0c1bfa | 2015-10-22 19:14:57 -0700 | [diff] [blame] | 231 | } |
| 232 | } |
| 233 | |
Jan Tattermusch | e81adf3 | 2016-04-04 15:59:50 -0700 | [diff] [blame] | 234 | private async Task RunUnaryAsync(Channel channel, IInterarrivalTimer timer) |
Jan Tattermusch | e26e2e5 | 2016-03-21 13:52:50 -0700 | [diff] [blame] | 235 | { |
Jan Tattermusch | f41ebc3 | 2016-06-22 12:47:14 -0700 | [diff] [blame] | 236 | var client = new BenchmarkService.BenchmarkServiceClient(channel); |
Jan Tattermusch | e26e2e5 | 2016-03-21 13:52:50 -0700 | [diff] [blame] | 237 | var request = CreateSimpleRequest(); |
| 238 | var stopwatch = new Stopwatch(); |
| 239 | |
| 240 | while (!stoppedCts.Token.IsCancellationRequested) |
| 241 | { |
| 242 | stopwatch.Restart(); |
| 243 | await client.UnaryCallAsync(request); |
| 244 | stopwatch.Stop(); |
| 245 | |
| 246 | // spec requires data point in nanoseconds. |
Jan Tattermusch | 19e3d3b | 2017-05-17 11:05:06 +0200 | [diff] [blame] | 247 | threadLocalHistogram.Value.AddObservation(stopwatch.Elapsed.TotalSeconds * SecondsToNanos); |
Jan Tattermusch | e81adf3 | 2016-04-04 15:59:50 -0700 | [diff] [blame] | 248 | |
| 249 | await timer.WaitForNextAsync(); |
Jan Tattermusch | e26e2e5 | 2016-03-21 13:52:50 -0700 | [diff] [blame] | 250 | } |
| 251 | } |
| 252 | |
Jan Tattermusch | e81adf3 | 2016-04-04 15:59:50 -0700 | [diff] [blame] | 253 | private async Task RunStreamingPingPongAsync(Channel channel, IInterarrivalTimer timer) |
Jan Tattermusch | e26e2e5 | 2016-03-21 13:52:50 -0700 | [diff] [blame] | 254 | { |
Jan Tattermusch | f41ebc3 | 2016-06-22 12:47:14 -0700 | [diff] [blame] | 255 | var client = new BenchmarkService.BenchmarkServiceClient(channel); |
Jan Tattermusch | e26e2e5 | 2016-03-21 13:52:50 -0700 | [diff] [blame] | 256 | var request = CreateSimpleRequest(); |
| 257 | var stopwatch = new Stopwatch(); |
| 258 | |
| 259 | using (var call = client.StreamingCall()) |
| 260 | { |
| 261 | while (!stoppedCts.Token.IsCancellationRequested) |
| 262 | { |
| 263 | stopwatch.Restart(); |
| 264 | await call.RequestStream.WriteAsync(request); |
| 265 | await call.ResponseStream.MoveNext(); |
| 266 | stopwatch.Stop(); |
| 267 | |
| 268 | // spec requires data point in nanoseconds. |
Jan Tattermusch | 19e3d3b | 2017-05-17 11:05:06 +0200 | [diff] [blame] | 269 | threadLocalHistogram.Value.AddObservation(stopwatch.Elapsed.TotalSeconds * SecondsToNanos); |
Jan Tattermusch | e81adf3 | 2016-04-04 15:59:50 -0700 | [diff] [blame] | 270 | |
| 271 | await timer.WaitForNextAsync(); |
Jan Tattermusch | e26e2e5 | 2016-03-21 13:52:50 -0700 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | // finish the streaming call |
| 275 | await call.RequestStream.CompleteAsync(); |
| 276 | Assert.IsFalse(await call.ResponseStream.MoveNext()); |
| 277 | } |
| 278 | } |
| 279 | |
Jan Tattermusch | e81adf3 | 2016-04-04 15:59:50 -0700 | [diff] [blame] | 280 | private async Task RunGenericStreamingAsync(Channel channel, IInterarrivalTimer timer) |
Jan Tattermusch | e45ca5f | 2016-03-21 14:59:08 -0700 | [diff] [blame] | 281 | { |
Jan Tattermusch | d7079b2 | 2017-05-16 19:55:57 +0200 | [diff] [blame] | 282 | var request = cachedByteBufferRequest.Value; |
Jan Tattermusch | e45ca5f | 2016-03-21 14:59:08 -0700 | [diff] [blame] | 283 | var stopwatch = new Stopwatch(); |
| 284 | |
Jan Tattermusch | 253769e | 2016-03-21 16:25:59 -0700 | [diff] [blame] | 285 | var callDetails = new CallInvocationDetails<byte[], byte[]>(channel, GenericService.StreamingCallMethod, new CallOptions()); |
Jan Tattermusch | e45ca5f | 2016-03-21 14:59:08 -0700 | [diff] [blame] | 286 | |
| 287 | using (var call = Calls.AsyncDuplexStreamingCall(callDetails)) |
| 288 | { |
| 289 | while (!stoppedCts.Token.IsCancellationRequested) |
| 290 | { |
| 291 | stopwatch.Restart(); |
| 292 | await call.RequestStream.WriteAsync(request); |
| 293 | await call.ResponseStream.MoveNext(); |
| 294 | stopwatch.Stop(); |
| 295 | |
| 296 | // spec requires data point in nanoseconds. |
Jan Tattermusch | 19e3d3b | 2017-05-17 11:05:06 +0200 | [diff] [blame] | 297 | threadLocalHistogram.Value.AddObservation(stopwatch.Elapsed.TotalSeconds * SecondsToNanos); |
Jan Tattermusch | e81adf3 | 2016-04-04 15:59:50 -0700 | [diff] [blame] | 298 | |
| 299 | await timer.WaitForNextAsync(); |
Jan Tattermusch | e45ca5f | 2016-03-21 14:59:08 -0700 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | // finish the streaming call |
| 303 | await call.RequestStream.CompleteAsync(); |
| 304 | Assert.IsFalse(await call.ResponseStream.MoveNext()); |
| 305 | } |
| 306 | } |
| 307 | |
Jan Tattermusch | 09c5e05 | 2016-05-18 22:24:27 -0700 | [diff] [blame] | 308 | private Task RunClientAsync(Channel channel, IInterarrivalTimer timer, BasicProfiler optionalProfiler) |
Jan Tattermusch | e26e2e5 | 2016-03-21 13:52:50 -0700 | [diff] [blame] | 309 | { |
Jan Tattermusch | e45ca5f | 2016-03-21 14:59:08 -0700 | [diff] [blame] | 310 | if (payloadConfig.PayloadCase == PayloadConfig.PayloadOneofCase.BytebufParams) |
| 311 | { |
Jan Tattermusch | 87ba294 | 2016-05-16 17:18:00 -0700 | [diff] [blame] | 312 | GrpcPreconditions.CheckArgument(clientType == ClientType.AsyncClient, "Generic client only supports async API"); |
| 313 | GrpcPreconditions.CheckArgument(rpcType == RpcType.Streaming, "Generic client only supports streaming calls"); |
Jan Tattermusch | e458d84 | 2016-05-09 14:21:52 -0700 | [diff] [blame] | 314 | return RunGenericStreamingAsync(channel, timer); |
Jan Tattermusch | e45ca5f | 2016-03-21 14:59:08 -0700 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | GrpcPreconditions.CheckNotNull(payloadConfig.SimpleParams); |
Jan Tattermusch | 87ba294 | 2016-05-16 17:18:00 -0700 | [diff] [blame] | 318 | if (clientType == ClientType.SyncClient) |
Jan Tattermusch | e26e2e5 | 2016-03-21 13:52:50 -0700 | [diff] [blame] | 319 | { |
Jan Tattermusch | 87ba294 | 2016-05-16 17:18:00 -0700 | [diff] [blame] | 320 | GrpcPreconditions.CheckArgument(rpcType == RpcType.Unary, "Sync client can only be used for Unary calls in C#"); |
Jan Tattermusch | e458d84 | 2016-05-09 14:21:52 -0700 | [diff] [blame] | 321 | // create a dedicated thread for the synchronous client |
Jan Tattermusch | 09c5e05 | 2016-05-18 22:24:27 -0700 | [diff] [blame] | 322 | return Task.Factory.StartNew(() => RunUnary(channel, timer, optionalProfiler), TaskCreationOptions.LongRunning); |
Jan Tattermusch | e26e2e5 | 2016-03-21 13:52:50 -0700 | [diff] [blame] | 323 | } |
Jan Tattermusch | 87ba294 | 2016-05-16 17:18:00 -0700 | [diff] [blame] | 324 | else if (clientType == ClientType.AsyncClient) |
Jan Tattermusch | e26e2e5 | 2016-03-21 13:52:50 -0700 | [diff] [blame] | 325 | { |
| 326 | switch (rpcType) |
| 327 | { |
Jan Tattermusch | 87ba294 | 2016-05-16 17:18:00 -0700 | [diff] [blame] | 328 | case RpcType.Unary: |
Jan Tattermusch | e458d84 | 2016-05-09 14:21:52 -0700 | [diff] [blame] | 329 | return RunUnaryAsync(channel, timer); |
Jan Tattermusch | 87ba294 | 2016-05-16 17:18:00 -0700 | [diff] [blame] | 330 | case RpcType.Streaming: |
Jan Tattermusch | e458d84 | 2016-05-09 14:21:52 -0700 | [diff] [blame] | 331 | return RunStreamingPingPongAsync(channel, timer); |
Jan Tattermusch | e26e2e5 | 2016-03-21 13:52:50 -0700 | [diff] [blame] | 332 | } |
| 333 | } |
| 334 | throw new ArgumentException("Unsupported configuration."); |
| 335 | } |
| 336 | |
| 337 | private SimpleRequest CreateSimpleRequest() |
| 338 | { |
Jan Tattermusch | e45ca5f | 2016-03-21 14:59:08 -0700 | [diff] [blame] | 339 | GrpcPreconditions.CheckNotNull(payloadConfig.SimpleParams); |
Jan Tattermusch | e26e2e5 | 2016-03-21 13:52:50 -0700 | [diff] [blame] | 340 | return new SimpleRequest |
| 341 | { |
Jan Tattermusch | e45ca5f | 2016-03-21 14:59:08 -0700 | [diff] [blame] | 342 | Payload = CreateZerosPayload(payloadConfig.SimpleParams.ReqSize), |
| 343 | ResponseSize = payloadConfig.SimpleParams.RespSize |
Jan Tattermusch | e26e2e5 | 2016-03-21 13:52:50 -0700 | [diff] [blame] | 344 | }; |
| 345 | } |
| 346 | |
Jan Tattermusch | d0c1bfa | 2015-10-22 19:14:57 -0700 | [diff] [blame] | 347 | private static Payload CreateZerosPayload(int size) |
| 348 | { |
| 349 | return new Payload { Body = ByteString.CopyFrom(new byte[size]) }; |
| 350 | } |
Jan Tattermusch | e81adf3 | 2016-04-04 15:59:50 -0700 | [diff] [blame] | 351 | |
| 352 | private static IInterarrivalTimer CreateTimer(LoadParams loadParams, double loadMultiplier) |
| 353 | { |
| 354 | switch (loadParams.LoadCase) |
| 355 | { |
| 356 | case LoadParams.LoadOneofCase.ClosedLoop: |
| 357 | return new ClosedLoopInterarrivalTimer(); |
| 358 | case LoadParams.LoadOneofCase.Poisson: |
| 359 | return new PoissonInterarrivalTimer(loadParams.Poisson.OfferedLoad * loadMultiplier); |
| 360 | default: |
| 361 | throw new ArgumentException("Unknown load type"); |
| 362 | } |
| 363 | } |
Jan Tattermusch | d0c1bfa | 2015-10-22 19:14:57 -0700 | [diff] [blame] | 364 | } |
| 365 | } |