Jan Tattermusch | d0c1bfa | 2015-10-22 19:14:57 -0700 | [diff] [blame] | 1 | #region Copyright notice and license |
| 2 | |
Craig Tiller | 6169d5f | 2016-03-31 07:46:18 -0700 | [diff] [blame] | 3 | // Copyright 2015, Google Inc. |
Jan Tattermusch | d0c1bfa | 2015-10-22 19:14:57 -0700 | [diff] [blame] | 4 | // All rights reserved. |
| 5 | // |
| 6 | // Redistribution and use in source and binary forms, with or without |
| 7 | // modification, are permitted provided that the following conditions are |
| 8 | // met: |
| 9 | // |
| 10 | // * Redistributions of source code must retain the above copyright |
| 11 | // notice, this list of conditions and the following disclaimer. |
| 12 | // * Redistributions in binary form must reproduce the above |
| 13 | // copyright notice, this list of conditions and the following disclaimer |
| 14 | // in the documentation and/or other materials provided with the |
| 15 | // distribution. |
| 16 | // * Neither the name of Google Inc. nor the names of its |
| 17 | // contributors may be used to endorse or promote products derived from |
| 18 | // this software without specific prior written permission. |
| 19 | // |
| 20 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | |
| 32 | #endregion |
| 33 | |
| 34 | using System; |
Jan Tattermusch | 09c5e05 | 2016-05-18 22:24:27 -0700 | [diff] [blame] | 35 | using System.Collections.Concurrent; |
Jan Tattermusch | d0c1bfa | 2015-10-22 19:14:57 -0700 | [diff] [blame] | 36 | using System.Collections.Generic; |
| 37 | using System.Diagnostics; |
| 38 | using System.IO; |
| 39 | using System.Linq; |
| 40 | using System.Text.RegularExpressions; |
| 41 | using System.Threading; |
| 42 | using System.Threading.Tasks; |
| 43 | using Google.Protobuf; |
| 44 | using Grpc.Core; |
Jan Tattermusch | 09c5e05 | 2016-05-18 22:24:27 -0700 | [diff] [blame] | 45 | using Grpc.Core.Internal; |
Jan Tattermusch | 3d6644a | 2016-03-21 09:46:15 -0700 | [diff] [blame] | 46 | using Grpc.Core.Logging; |
Jan Tattermusch | 09c5e05 | 2016-05-18 22:24:27 -0700 | [diff] [blame] | 47 | using Grpc.Core.Profiling; |
Jan Tattermusch | d0c1bfa | 2015-10-22 19:14:57 -0700 | [diff] [blame] | 48 | using Grpc.Core.Utils; |
| 49 | using NUnit.Framework; |
| 50 | using Grpc.Testing; |
| 51 | |
| 52 | namespace Grpc.IntegrationTesting |
| 53 | { |
| 54 | /// <summary> |
| 55 | /// Helper methods to start client runners for performance testing. |
| 56 | /// </summary> |
Jan Tattermusch | 3d6644a | 2016-03-21 09:46:15 -0700 | [diff] [blame] | 57 | public class ClientRunners |
Jan Tattermusch | d0c1bfa | 2015-10-22 19:14:57 -0700 | [diff] [blame] | 58 | { |
Jan Tattermusch | 3d6644a | 2016-03-21 09:46:15 -0700 | [diff] [blame] | 59 | static readonly ILogger Logger = GrpcEnvironment.Logger.ForType<ClientRunners>(); |
| 60 | |
Jan Tattermusch | 09c5e05 | 2016-05-18 22:24:27 -0700 | [diff] [blame] | 61 | // Profilers to use for clients. |
| 62 | static readonly BlockingCollection<BasicProfiler> profilers = new BlockingCollection<BasicProfiler>(); |
| 63 | |
| 64 | internal static void AddProfiler(BasicProfiler profiler) |
| 65 | { |
| 66 | GrpcPreconditions.CheckNotNull(profiler); |
| 67 | profilers.Add(profiler); |
| 68 | } |
| 69 | |
Jan Tattermusch | d0c1bfa | 2015-10-22 19:14:57 -0700 | [diff] [blame] | 70 | /// <summary> |
| 71 | /// Creates a started client runner. |
| 72 | /// </summary> |
| 73 | public static IClientRunner CreateStarted(ClientConfig config) |
| 74 | { |
Jan Tattermusch | 3d6644a | 2016-03-21 09:46:15 -0700 | [diff] [blame] | 75 | Logger.Debug("ClientConfig: {0}", config); |
Jan Tattermusch | 3d6644a | 2016-03-21 09:46:15 -0700 | [diff] [blame] | 76 | |
Jan Tattermusch | 3d6644a | 2016-03-21 09:46:15 -0700 | [diff] [blame] | 77 | if (config.AsyncClientThreads != 0) |
| 78 | { |
| 79 | Logger.Warning("ClientConfig.AsyncClientThreads is not supported for C#. Ignoring the value"); |
| 80 | } |
| 81 | if (config.CoreLimit != 0) |
| 82 | { |
| 83 | Logger.Warning("ClientConfig.CoreLimit is not supported for C#. Ignoring the value"); |
| 84 | } |
| 85 | if (config.CoreList.Count > 0) |
| 86 | { |
| 87 | Logger.Warning("ClientConfig.CoreList is not supported for C#. Ignoring the value"); |
| 88 | } |
Jan Tattermusch | d0c1bfa | 2015-10-22 19:14:57 -0700 | [diff] [blame] | 89 | |
Jan Tattermusch | e81adf3 | 2016-04-04 15:59:50 -0700 | [diff] [blame] | 90 | var channels = CreateChannels(config.ClientChannels, config.ServerTargets, config.SecurityParams); |
| 91 | |
| 92 | return new ClientRunnerImpl(channels, |
| 93 | config.ClientType, |
| 94 | config.RpcType, |
| 95 | config.OutstandingRpcsPerChannel, |
| 96 | config.LoadParams, |
| 97 | config.PayloadConfig, |
Jan Tattermusch | 09c5e05 | 2016-05-18 22:24:27 -0700 | [diff] [blame] | 98 | config.HistogramParams, |
| 99 | () => GetNextProfiler()); |
Jan Tattermusch | e81adf3 | 2016-04-04 15:59:50 -0700 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | private static List<Channel> CreateChannels(int clientChannels, IEnumerable<string> serverTargets, SecurityParams securityParams) |
| 103 | { |
| 104 | GrpcPreconditions.CheckArgument(clientChannels > 0, "clientChannels needs to be at least 1."); |
| 105 | GrpcPreconditions.CheckArgument(serverTargets.Count() > 0, "at least one serverTarget needs to be specified."); |
| 106 | |
| 107 | var credentials = securityParams != null ? TestCredentials.CreateSslCredentials() : ChannelCredentials.Insecure; |
Jan Tattermusch | 3d6644a | 2016-03-21 09:46:15 -0700 | [diff] [blame] | 108 | List<ChannelOption> channelOptions = null; |
Jan Tattermusch | e81adf3 | 2016-04-04 15:59:50 -0700 | [diff] [blame] | 109 | if (securityParams != null && securityParams.ServerHostOverride != "") |
Jan Tattermusch | 3d6644a | 2016-03-21 09:46:15 -0700 | [diff] [blame] | 110 | { |
| 111 | channelOptions = new List<ChannelOption> |
| 112 | { |
Jan Tattermusch | e81adf3 | 2016-04-04 15:59:50 -0700 | [diff] [blame] | 113 | new ChannelOption(ChannelOptions.SslTargetNameOverride, securityParams.ServerHostOverride) |
Jan Tattermusch | 3d6644a | 2016-03-21 09:46:15 -0700 | [diff] [blame] | 114 | }; |
| 115 | } |
Jan Tattermusch | d0c1bfa | 2015-10-22 19:14:57 -0700 | [diff] [blame] | 116 | |
Jan Tattermusch | e81adf3 | 2016-04-04 15:59:50 -0700 | [diff] [blame] | 117 | var result = new List<Channel>(); |
| 118 | for (int i = 0; i < clientChannels; i++) |
| 119 | { |
| 120 | var target = serverTargets.ElementAt(i % serverTargets.Count()); |
| 121 | var channel = new Channel(target, credentials, channelOptions); |
| 122 | result.Add(channel); |
| 123 | } |
| 124 | return result; |
Jan Tattermusch | d0c1bfa | 2015-10-22 19:14:57 -0700 | [diff] [blame] | 125 | } |
Jan Tattermusch | 09c5e05 | 2016-05-18 22:24:27 -0700 | [diff] [blame] | 126 | |
| 127 | private static BasicProfiler GetNextProfiler() |
| 128 | { |
| 129 | BasicProfiler result = null; |
| 130 | profilers.TryTake(out result); |
| 131 | return result; |
| 132 | } |
Jan Tattermusch | d0c1bfa | 2015-10-22 19:14:57 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Jan Tattermusch | 09c5e05 | 2016-05-18 22:24:27 -0700 | [diff] [blame] | 135 | internal class ClientRunnerImpl : IClientRunner |
Jan Tattermusch | d0c1bfa | 2015-10-22 19:14:57 -0700 | [diff] [blame] | 136 | { |
Jan Tattermusch | 18ce9d6 | 2015-11-18 15:41:10 -0800 | [diff] [blame] | 137 | const double SecondsToNanos = 1e9; |
| 138 | |
Jan Tattermusch | e81adf3 | 2016-04-04 15:59:50 -0700 | [diff] [blame] | 139 | readonly List<Channel> channels; |
Jan Tattermusch | e26e2e5 | 2016-03-21 13:52:50 -0700 | [diff] [blame] | 140 | readonly ClientType clientType; |
| 141 | readonly RpcType rpcType; |
Jan Tattermusch | e45ca5f | 2016-03-21 14:59:08 -0700 | [diff] [blame] | 142 | readonly PayloadConfig payloadConfig; |
Jan Tattermusch | d7079b2 | 2017-05-16 19:55:57 +0200 | [diff] [blame] | 143 | readonly Lazy<byte[]> cachedByteBufferRequest; |
Jan Tattermusch | 19e3d3b | 2017-05-17 11:05:06 +0200 | [diff] [blame] | 144 | readonly ThreadLocal<Histogram> threadLocalHistogram; |
Jan Tattermusch | d0c1bfa | 2015-10-22 19:14:57 -0700 | [diff] [blame] | 145 | |
Jan Tattermusch | e81adf3 | 2016-04-04 15:59:50 -0700 | [diff] [blame] | 146 | readonly List<Task> runnerTasks; |
| 147 | readonly CancellationTokenSource stoppedCts = new CancellationTokenSource(); |
Jan Tattermusch | d0c1bfa | 2015-10-22 19:14:57 -0700 | [diff] [blame] | 148 | readonly WallClockStopwatch wallClockStopwatch = new WallClockStopwatch(); |
Jan Tattermusch | 09c5e05 | 2016-05-18 22:24:27 -0700 | [diff] [blame] | 149 | readonly AtomicCounter statsResetCount = new AtomicCounter(); |
Jan Tattermusch | d0c1bfa | 2015-10-22 19:14:57 -0700 | [diff] [blame] | 150 | |
Jan Tattermusch | 09c5e05 | 2016-05-18 22:24:27 -0700 | [diff] [blame] | 151 | 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] | 152 | { |
Jan Tattermusch | e81adf3 | 2016-04-04 15:59:50 -0700 | [diff] [blame] | 153 | GrpcPreconditions.CheckArgument(outstandingRpcsPerChannel > 0, "outstandingRpcsPerChannel"); |
Jan Tattermusch | 8c839e8 | 2016-04-12 15:12:08 -0700 | [diff] [blame] | 154 | GrpcPreconditions.CheckNotNull(histogramParams, "histogramParams"); |
Jan Tattermusch | e81adf3 | 2016-04-04 15:59:50 -0700 | [diff] [blame] | 155 | this.channels = new List<Channel>(channels); |
Jan Tattermusch | e26e2e5 | 2016-03-21 13:52:50 -0700 | [diff] [blame] | 156 | this.clientType = clientType; |
| 157 | this.rpcType = rpcType; |
Jan Tattermusch | e45ca5f | 2016-03-21 14:59:08 -0700 | [diff] [blame] | 158 | this.payloadConfig = payloadConfig; |
Jan Tattermusch | d7079b2 | 2017-05-16 19:55:57 +0200 | [diff] [blame] | 159 | this.cachedByteBufferRequest = new Lazy<byte[]>(() => new byte[payloadConfig.BytebufParams.ReqSize]); |
Jan Tattermusch | 19e3d3b | 2017-05-17 11:05:06 +0200 | [diff] [blame] | 160 | this.threadLocalHistogram = new ThreadLocal<Histogram>(() => new Histogram(histogramParams.Resolution, histogramParams.MaxPossible), true); |
Jan Tattermusch | d0c1bfa | 2015-10-22 19:14:57 -0700 | [diff] [blame] | 161 | |
Jan Tattermusch | e81adf3 | 2016-04-04 15:59:50 -0700 | [diff] [blame] | 162 | this.runnerTasks = new List<Task>(); |
| 163 | foreach (var channel in this.channels) |
| 164 | { |
| 165 | for (int i = 0; i < outstandingRpcsPerChannel; i++) |
| 166 | { |
| 167 | var timer = CreateTimer(loadParams, 1.0 / this.channels.Count / outstandingRpcsPerChannel); |
Jan Tattermusch | 09c5e05 | 2016-05-18 22:24:27 -0700 | [diff] [blame] | 168 | var optionalProfiler = profilerFactory(); |
| 169 | this.runnerTasks.Add(RunClientAsync(channel, timer, optionalProfiler)); |
Jan Tattermusch | e81adf3 | 2016-04-04 15:59:50 -0700 | [diff] [blame] | 170 | } |
| 171 | } |
Jan Tattermusch | d0c1bfa | 2015-10-22 19:14:57 -0700 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | public ClientStats GetStats(bool reset) |
| 175 | { |
Jan Tattermusch | 19e3d3b | 2017-05-17 11:05:06 +0200 | [diff] [blame] | 176 | var histogramData = new HistogramData(); |
| 177 | foreach (var hist in threadLocalHistogram.Values) |
| 178 | { |
| 179 | hist.GetSnapshot(histogramData, reset); |
| 180 | } |
| 181 | |
Jan Tattermusch | d0c1bfa | 2015-10-22 19:14:57 -0700 | [diff] [blame] | 182 | var secondsElapsed = wallClockStopwatch.GetElapsedSnapshot(reset).TotalSeconds; |
| 183 | |
Jan Tattermusch | 09c5e05 | 2016-05-18 22:24:27 -0700 | [diff] [blame] | 184 | if (reset) |
| 185 | { |
| 186 | statsResetCount.Increment(); |
| 187 | } |
| 188 | |
Jan Tattermusch | 8251fdd | 2017-06-01 19:26:12 +0200 | [diff] [blame] | 189 | GrpcEnvironment.Logger.Info("[ClientRunnerImpl.GetStats] GC collection counts: gen0 {0}, gen1 {1}, gen2 {2}, (histogram reset count:{3}, seconds since reset: {4})", |
| 190 | GC.CollectionCount(0), GC.CollectionCount(1), GC.CollectionCount(2), statsResetCount.Count, secondsElapsed); |
Jan Tattermusch | 01ce236 | 2017-04-25 20:53:51 +0200 | [diff] [blame] | 191 | |
Jan Tattermusch | d0c1bfa | 2015-10-22 19:14:57 -0700 | [diff] [blame] | 192 | // TODO: populate user time and system time |
| 193 | return new ClientStats |
| 194 | { |
| 195 | Latencies = histogramData, |
| 196 | TimeElapsed = secondsElapsed, |
| 197 | TimeUser = 0, |
| 198 | TimeSystem = 0 |
| 199 | }; |
| 200 | } |
| 201 | |
| 202 | public async Task StopAsync() |
| 203 | { |
| 204 | stoppedCts.Cancel(); |
Jan Tattermusch | e81adf3 | 2016-04-04 15:59:50 -0700 | [diff] [blame] | 205 | foreach (var runnerTask in runnerTasks) |
| 206 | { |
| 207 | await runnerTask; |
| 208 | } |
| 209 | foreach (var channel in channels) |
| 210 | { |
| 211 | await channel.ShutdownAsync(); |
| 212 | } |
Jan Tattermusch | d0c1bfa | 2015-10-22 19:14:57 -0700 | [diff] [blame] | 213 | } |
| 214 | |
Jan Tattermusch | 09c5e05 | 2016-05-18 22:24:27 -0700 | [diff] [blame] | 215 | private void RunUnary(Channel channel, IInterarrivalTimer timer, BasicProfiler optionalProfiler) |
Jan Tattermusch | d0c1bfa | 2015-10-22 19:14:57 -0700 | [diff] [blame] | 216 | { |
Jan Tattermusch | 09c5e05 | 2016-05-18 22:24:27 -0700 | [diff] [blame] | 217 | if (optionalProfiler != null) |
| 218 | { |
| 219 | Profilers.SetForCurrentThread(optionalProfiler); |
| 220 | } |
| 221 | |
| 222 | bool profilerReset = false; |
| 223 | |
Jan Tattermusch | f41ebc3 | 2016-06-22 12:47:14 -0700 | [diff] [blame] | 224 | var client = new BenchmarkService.BenchmarkServiceClient(channel); |
Jan Tattermusch | e26e2e5 | 2016-03-21 13:52:50 -0700 | [diff] [blame] | 225 | var request = CreateSimpleRequest(); |
Jan Tattermusch | d0c1bfa | 2015-10-22 19:14:57 -0700 | [diff] [blame] | 226 | var stopwatch = new Stopwatch(); |
| 227 | |
| 228 | while (!stoppedCts.Token.IsCancellationRequested) |
| 229 | { |
Jan Tattermusch | 09c5e05 | 2016-05-18 22:24:27 -0700 | [diff] [blame] | 230 | // after the first stats reset, also reset the profiler. |
| 231 | if (optionalProfiler != null && !profilerReset && statsResetCount.Count > 0) |
| 232 | { |
| 233 | optionalProfiler.Reset(); |
| 234 | profilerReset = true; |
| 235 | } |
| 236 | |
Jan Tattermusch | d0c1bfa | 2015-10-22 19:14:57 -0700 | [diff] [blame] | 237 | stopwatch.Restart(); |
| 238 | client.UnaryCall(request); |
| 239 | stopwatch.Stop(); |
| 240 | |
Jan Tattermusch | 18ce9d6 | 2015-11-18 15:41:10 -0800 | [diff] [blame] | 241 | // spec requires data point in nanoseconds. |
Jan Tattermusch | 19e3d3b | 2017-05-17 11:05:06 +0200 | [diff] [blame] | 242 | threadLocalHistogram.Value.AddObservation(stopwatch.Elapsed.TotalSeconds * SecondsToNanos); |
Jan Tattermusch | e81adf3 | 2016-04-04 15:59:50 -0700 | [diff] [blame] | 243 | |
| 244 | timer.WaitForNext(); |
Jan Tattermusch | d0c1bfa | 2015-10-22 19:14:57 -0700 | [diff] [blame] | 245 | } |
| 246 | } |
| 247 | |
Jan Tattermusch | e81adf3 | 2016-04-04 15:59:50 -0700 | [diff] [blame] | 248 | private async Task RunUnaryAsync(Channel channel, IInterarrivalTimer timer) |
Jan Tattermusch | e26e2e5 | 2016-03-21 13:52:50 -0700 | [diff] [blame] | 249 | { |
Jan Tattermusch | f41ebc3 | 2016-06-22 12:47:14 -0700 | [diff] [blame] | 250 | var client = new BenchmarkService.BenchmarkServiceClient(channel); |
Jan Tattermusch | e26e2e5 | 2016-03-21 13:52:50 -0700 | [diff] [blame] | 251 | var request = CreateSimpleRequest(); |
| 252 | var stopwatch = new Stopwatch(); |
| 253 | |
| 254 | while (!stoppedCts.Token.IsCancellationRequested) |
| 255 | { |
| 256 | stopwatch.Restart(); |
| 257 | await client.UnaryCallAsync(request); |
| 258 | stopwatch.Stop(); |
| 259 | |
| 260 | // spec requires data point in nanoseconds. |
Jan Tattermusch | 19e3d3b | 2017-05-17 11:05:06 +0200 | [diff] [blame] | 261 | threadLocalHistogram.Value.AddObservation(stopwatch.Elapsed.TotalSeconds * SecondsToNanos); |
Jan Tattermusch | e81adf3 | 2016-04-04 15:59:50 -0700 | [diff] [blame] | 262 | |
| 263 | await timer.WaitForNextAsync(); |
Jan Tattermusch | e26e2e5 | 2016-03-21 13:52:50 -0700 | [diff] [blame] | 264 | } |
| 265 | } |
| 266 | |
Jan Tattermusch | e81adf3 | 2016-04-04 15:59:50 -0700 | [diff] [blame] | 267 | private async Task RunStreamingPingPongAsync(Channel channel, IInterarrivalTimer timer) |
Jan Tattermusch | e26e2e5 | 2016-03-21 13:52:50 -0700 | [diff] [blame] | 268 | { |
Jan Tattermusch | f41ebc3 | 2016-06-22 12:47:14 -0700 | [diff] [blame] | 269 | var client = new BenchmarkService.BenchmarkServiceClient(channel); |
Jan Tattermusch | e26e2e5 | 2016-03-21 13:52:50 -0700 | [diff] [blame] | 270 | var request = CreateSimpleRequest(); |
| 271 | var stopwatch = new Stopwatch(); |
| 272 | |
| 273 | using (var call = client.StreamingCall()) |
| 274 | { |
| 275 | while (!stoppedCts.Token.IsCancellationRequested) |
| 276 | { |
| 277 | stopwatch.Restart(); |
| 278 | await call.RequestStream.WriteAsync(request); |
| 279 | await call.ResponseStream.MoveNext(); |
| 280 | stopwatch.Stop(); |
| 281 | |
| 282 | // spec requires data point in nanoseconds. |
Jan Tattermusch | 19e3d3b | 2017-05-17 11:05:06 +0200 | [diff] [blame] | 283 | threadLocalHistogram.Value.AddObservation(stopwatch.Elapsed.TotalSeconds * SecondsToNanos); |
Jan Tattermusch | e81adf3 | 2016-04-04 15:59:50 -0700 | [diff] [blame] | 284 | |
| 285 | await timer.WaitForNextAsync(); |
Jan Tattermusch | e26e2e5 | 2016-03-21 13:52:50 -0700 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | // finish the streaming call |
| 289 | await call.RequestStream.CompleteAsync(); |
| 290 | Assert.IsFalse(await call.ResponseStream.MoveNext()); |
| 291 | } |
| 292 | } |
| 293 | |
Jan Tattermusch | e81adf3 | 2016-04-04 15:59:50 -0700 | [diff] [blame] | 294 | private async Task RunGenericStreamingAsync(Channel channel, IInterarrivalTimer timer) |
Jan Tattermusch | e45ca5f | 2016-03-21 14:59:08 -0700 | [diff] [blame] | 295 | { |
Jan Tattermusch | d7079b2 | 2017-05-16 19:55:57 +0200 | [diff] [blame] | 296 | var request = cachedByteBufferRequest.Value; |
Jan Tattermusch | e45ca5f | 2016-03-21 14:59:08 -0700 | [diff] [blame] | 297 | var stopwatch = new Stopwatch(); |
| 298 | |
Jan Tattermusch | 253769e | 2016-03-21 16:25:59 -0700 | [diff] [blame] | 299 | var callDetails = new CallInvocationDetails<byte[], byte[]>(channel, GenericService.StreamingCallMethod, new CallOptions()); |
Jan Tattermusch | e45ca5f | 2016-03-21 14:59:08 -0700 | [diff] [blame] | 300 | |
| 301 | using (var call = Calls.AsyncDuplexStreamingCall(callDetails)) |
| 302 | { |
| 303 | while (!stoppedCts.Token.IsCancellationRequested) |
| 304 | { |
| 305 | stopwatch.Restart(); |
| 306 | await call.RequestStream.WriteAsync(request); |
| 307 | await call.ResponseStream.MoveNext(); |
| 308 | stopwatch.Stop(); |
| 309 | |
| 310 | // spec requires data point in nanoseconds. |
Jan Tattermusch | 19e3d3b | 2017-05-17 11:05:06 +0200 | [diff] [blame] | 311 | threadLocalHistogram.Value.AddObservation(stopwatch.Elapsed.TotalSeconds * SecondsToNanos); |
Jan Tattermusch | e81adf3 | 2016-04-04 15:59:50 -0700 | [diff] [blame] | 312 | |
| 313 | await timer.WaitForNextAsync(); |
Jan Tattermusch | e45ca5f | 2016-03-21 14:59:08 -0700 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | // finish the streaming call |
| 317 | await call.RequestStream.CompleteAsync(); |
| 318 | Assert.IsFalse(await call.ResponseStream.MoveNext()); |
| 319 | } |
| 320 | } |
| 321 | |
Jan Tattermusch | 09c5e05 | 2016-05-18 22:24:27 -0700 | [diff] [blame] | 322 | private Task RunClientAsync(Channel channel, IInterarrivalTimer timer, BasicProfiler optionalProfiler) |
Jan Tattermusch | e26e2e5 | 2016-03-21 13:52:50 -0700 | [diff] [blame] | 323 | { |
Jan Tattermusch | e45ca5f | 2016-03-21 14:59:08 -0700 | [diff] [blame] | 324 | if (payloadConfig.PayloadCase == PayloadConfig.PayloadOneofCase.BytebufParams) |
| 325 | { |
Jan Tattermusch | 87ba294 | 2016-05-16 17:18:00 -0700 | [diff] [blame] | 326 | GrpcPreconditions.CheckArgument(clientType == ClientType.AsyncClient, "Generic client only supports async API"); |
| 327 | GrpcPreconditions.CheckArgument(rpcType == RpcType.Streaming, "Generic client only supports streaming calls"); |
Jan Tattermusch | e458d84 | 2016-05-09 14:21:52 -0700 | [diff] [blame] | 328 | return RunGenericStreamingAsync(channel, timer); |
Jan Tattermusch | e45ca5f | 2016-03-21 14:59:08 -0700 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | GrpcPreconditions.CheckNotNull(payloadConfig.SimpleParams); |
Jan Tattermusch | 87ba294 | 2016-05-16 17:18:00 -0700 | [diff] [blame] | 332 | if (clientType == ClientType.SyncClient) |
Jan Tattermusch | e26e2e5 | 2016-03-21 13:52:50 -0700 | [diff] [blame] | 333 | { |
Jan Tattermusch | 87ba294 | 2016-05-16 17:18:00 -0700 | [diff] [blame] | 334 | 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] | 335 | // create a dedicated thread for the synchronous client |
Jan Tattermusch | 09c5e05 | 2016-05-18 22:24:27 -0700 | [diff] [blame] | 336 | return Task.Factory.StartNew(() => RunUnary(channel, timer, optionalProfiler), TaskCreationOptions.LongRunning); |
Jan Tattermusch | e26e2e5 | 2016-03-21 13:52:50 -0700 | [diff] [blame] | 337 | } |
Jan Tattermusch | 87ba294 | 2016-05-16 17:18:00 -0700 | [diff] [blame] | 338 | else if (clientType == ClientType.AsyncClient) |
Jan Tattermusch | e26e2e5 | 2016-03-21 13:52:50 -0700 | [diff] [blame] | 339 | { |
| 340 | switch (rpcType) |
| 341 | { |
Jan Tattermusch | 87ba294 | 2016-05-16 17:18:00 -0700 | [diff] [blame] | 342 | case RpcType.Unary: |
Jan Tattermusch | e458d84 | 2016-05-09 14:21:52 -0700 | [diff] [blame] | 343 | return RunUnaryAsync(channel, timer); |
Jan Tattermusch | 87ba294 | 2016-05-16 17:18:00 -0700 | [diff] [blame] | 344 | case RpcType.Streaming: |
Jan Tattermusch | e458d84 | 2016-05-09 14:21:52 -0700 | [diff] [blame] | 345 | return RunStreamingPingPongAsync(channel, timer); |
Jan Tattermusch | e26e2e5 | 2016-03-21 13:52:50 -0700 | [diff] [blame] | 346 | } |
| 347 | } |
| 348 | throw new ArgumentException("Unsupported configuration."); |
| 349 | } |
| 350 | |
| 351 | private SimpleRequest CreateSimpleRequest() |
| 352 | { |
Jan Tattermusch | e45ca5f | 2016-03-21 14:59:08 -0700 | [diff] [blame] | 353 | GrpcPreconditions.CheckNotNull(payloadConfig.SimpleParams); |
Jan Tattermusch | e26e2e5 | 2016-03-21 13:52:50 -0700 | [diff] [blame] | 354 | return new SimpleRequest |
| 355 | { |
Jan Tattermusch | e45ca5f | 2016-03-21 14:59:08 -0700 | [diff] [blame] | 356 | Payload = CreateZerosPayload(payloadConfig.SimpleParams.ReqSize), |
| 357 | ResponseSize = payloadConfig.SimpleParams.RespSize |
Jan Tattermusch | e26e2e5 | 2016-03-21 13:52:50 -0700 | [diff] [blame] | 358 | }; |
| 359 | } |
| 360 | |
Jan Tattermusch | d0c1bfa | 2015-10-22 19:14:57 -0700 | [diff] [blame] | 361 | private static Payload CreateZerosPayload(int size) |
| 362 | { |
| 363 | return new Payload { Body = ByteString.CopyFrom(new byte[size]) }; |
| 364 | } |
Jan Tattermusch | e81adf3 | 2016-04-04 15:59:50 -0700 | [diff] [blame] | 365 | |
| 366 | private static IInterarrivalTimer CreateTimer(LoadParams loadParams, double loadMultiplier) |
| 367 | { |
| 368 | switch (loadParams.LoadCase) |
| 369 | { |
| 370 | case LoadParams.LoadOneofCase.ClosedLoop: |
| 371 | return new ClosedLoopInterarrivalTimer(); |
| 372 | case LoadParams.LoadOneofCase.Poisson: |
| 373 | return new PoissonInterarrivalTimer(loadParams.Poisson.OfferedLoad * loadMultiplier); |
| 374 | default: |
| 375 | throw new ArgumentException("Unknown load type"); |
| 376 | } |
| 377 | } |
Jan Tattermusch | d0c1bfa | 2015-10-22 19:14:57 -0700 | [diff] [blame] | 378 | } |
| 379 | } |