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; |
| 20 | using System.Collections.Generic; |
| 21 | using System.Diagnostics; |
| 22 | using System.IO; |
| 23 | using System.Linq; |
| 24 | using System.Text.RegularExpressions; |
| 25 | using System.Threading.Tasks; |
| 26 | |
| 27 | using CommandLine; |
| 28 | using CommandLine.Text; |
| 29 | using Grpc.Core; |
Jan Tattermusch | 66f8578 | 2017-02-24 19:44:16 +0100 | [diff] [blame] | 30 | using Grpc.Core.Logging; |
Jan Tattermusch | d0c1bfa | 2015-10-22 19:14:57 -0700 | [diff] [blame] | 31 | using Grpc.Core.Utils; |
| 32 | using Grpc.Testing; |
| 33 | using NUnit.Framework; |
| 34 | |
| 35 | namespace Grpc.IntegrationTesting |
| 36 | { |
| 37 | public class QpsWorker |
| 38 | { |
| 39 | private class ServerOptions |
| 40 | { |
Jan Tattermusch | 678ec90 | 2016-08-08 12:54:16 +0800 | [diff] [blame] | 41 | [Option("driver_port", Default = 0)] |
Jan Tattermusch | d0c1bfa | 2015-10-22 19:14:57 -0700 | [diff] [blame] | 42 | public int DriverPort { get; set; } |
Jan Tattermusch | d0c1bfa | 2015-10-22 19:14:57 -0700 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | ServerOptions options; |
| 46 | |
| 47 | private QpsWorker(ServerOptions options) |
| 48 | { |
| 49 | this.options = options; |
| 50 | } |
| 51 | |
| 52 | public static void Run(string[] args) |
| 53 | { |
Jan Tattermusch | 66f8578 | 2017-02-24 19:44:16 +0100 | [diff] [blame] | 54 | GrpcEnvironment.SetLogger(new ConsoleLogger()); |
Jan Tattermusch | 90cbde8 | 2016-08-08 14:33:28 +0800 | [diff] [blame] | 55 | var parserResult = Parser.Default.ParseArguments<ServerOptions>(args) |
| 56 | .WithNotParsed((x) => Environment.Exit(1)) |
| 57 | .WithParsed(options => |
| 58 | { |
| 59 | var workerServer = new QpsWorker(options); |
| 60 | workerServer.RunAsync().Wait(); |
| 61 | }); |
Jan Tattermusch | d0c1bfa | 2015-10-22 19:14:57 -0700 | [diff] [blame] | 62 | } |
| 63 | |
Jan Tattermusch | 1cbb567 | 2016-02-18 14:27:28 -0800 | [diff] [blame] | 64 | private async Task RunAsync() |
Jan Tattermusch | d0c1bfa | 2015-10-22 19:14:57 -0700 | [diff] [blame] | 65 | { |
| 66 | string host = "0.0.0.0"; |
| 67 | int port = options.DriverPort; |
| 68 | |
Jan Tattermusch | 1cbb567 | 2016-02-18 14:27:28 -0800 | [diff] [blame] | 69 | var tcs = new TaskCompletionSource<object>(); |
| 70 | var workerServiceImpl = new WorkerServiceImpl(() => { Task.Run(() => tcs.SetResult(null)); }); |
| 71 | |
Jan Tattermusch | d0c1bfa | 2015-10-22 19:14:57 -0700 | [diff] [blame] | 72 | var server = new Server |
| 73 | { |
Jan Tattermusch | 1cbb567 | 2016-02-18 14:27:28 -0800 | [diff] [blame] | 74 | Services = { WorkerService.BindService(workerServiceImpl) }, |
Jan Tattermusch | d0c1bfa | 2015-10-22 19:14:57 -0700 | [diff] [blame] | 75 | Ports = { new ServerPort(host, options.DriverPort, ServerCredentials.Insecure )} |
| 76 | }; |
| 77 | int boundPort = server.Ports.Single().BoundPort; |
Jan Tattermusch | 01ce236 | 2017-04-25 20:53:51 +0200 | [diff] [blame] | 78 | GrpcEnvironment.Logger.Info("Running qps worker server on {0}:{1}", host, boundPort); |
Jan Tattermusch | d0c1bfa | 2015-10-22 19:14:57 -0700 | [diff] [blame] | 79 | server.Start(); |
Jan Tattermusch | 1cbb567 | 2016-02-18 14:27:28 -0800 | [diff] [blame] | 80 | await tcs.Task; |
| 81 | await server.ShutdownAsync(); |
Jan Tattermusch | 01ce236 | 2017-04-25 20:53:51 +0200 | [diff] [blame] | 82 | |
Jan Tattermusch | 8251fdd | 2017-06-01 19:26:12 +0200 | [diff] [blame] | 83 | GrpcEnvironment.Logger.Info("GC collection counts (after shutdown): gen0 {0}, gen1 {1}, gen2 {2}", |
| 84 | GC.CollectionCount(0), GC.CollectionCount(1), GC.CollectionCount(2)); |
Jan Tattermusch | d0c1bfa | 2015-10-22 19:14:57 -0700 | [diff] [blame] | 85 | } |
| 86 | } |
| 87 | } |