blob: 62a7347d420074cdd29ab831f9550f3b0d6b79b5 [file] [log] [blame]
Jan Tattermuschd0c1bfa2015-10-22 19:14:57 -07001#region Copyright notice and license
2
Craig Tiller6169d5f2016-03-31 07:46:18 -07003// Copyright 2015, Google Inc.
Jan Tattermuschd0c1bfa2015-10-22 19:14:57 -07004// 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
34using System;
35using System.Collections.Generic;
36using System.Diagnostics;
37using System.IO;
38using System.Linq;
39using System.Text.RegularExpressions;
40using System.Threading.Tasks;
41
42using CommandLine;
43using CommandLine.Text;
44using Grpc.Core;
45using Grpc.Core.Utils;
46using Grpc.Testing;
47using NUnit.Framework;
48
49namespace Grpc.IntegrationTesting
50{
51 public class QpsWorker
52 {
53 private class ServerOptions
54 {
Jan Tattermusch678ec902016-08-08 12:54:16 +080055 [Option("driver_port", Default = 0)]
Jan Tattermuschd0c1bfa2015-10-22 19:14:57 -070056 public int DriverPort { get; set; }
Jan Tattermuschd0c1bfa2015-10-22 19:14:57 -070057 }
58
59 ServerOptions options;
60
61 private QpsWorker(ServerOptions options)
62 {
63 this.options = options;
64 }
65
66 public static void Run(string[] args)
67 {
Jan Tattermusch90cbde82016-08-08 14:33:28 +080068 var parserResult = Parser.Default.ParseArguments<ServerOptions>(args)
69 .WithNotParsed((x) => Environment.Exit(1))
70 .WithParsed(options =>
71 {
72 var workerServer = new QpsWorker(options);
73 workerServer.RunAsync().Wait();
74 });
Jan Tattermuschd0c1bfa2015-10-22 19:14:57 -070075 }
76
Jan Tattermusch1cbb5672016-02-18 14:27:28 -080077 private async Task RunAsync()
Jan Tattermuschd0c1bfa2015-10-22 19:14:57 -070078 {
Jan Tattermusch8c31c142016-10-22 11:26:50 +020079 // (ThreadPoolSize == ProcessorCount) gives best throughput in benchmarks
80 // and doesn't seem to harm performance even when server and client
81 // are running on the same machine.
82 GrpcEnvironment.SetThreadPoolSize(Environment.ProcessorCount);
83
Jan Tattermuschd0c1bfa2015-10-22 19:14:57 -070084 string host = "0.0.0.0";
85 int port = options.DriverPort;
86
Jan Tattermusch1cbb5672016-02-18 14:27:28 -080087 var tcs = new TaskCompletionSource<object>();
88 var workerServiceImpl = new WorkerServiceImpl(() => { Task.Run(() => tcs.SetResult(null)); });
89
Jan Tattermuschd0c1bfa2015-10-22 19:14:57 -070090 var server = new Server
91 {
Jan Tattermusch1cbb5672016-02-18 14:27:28 -080092 Services = { WorkerService.BindService(workerServiceImpl) },
Jan Tattermuschd0c1bfa2015-10-22 19:14:57 -070093 Ports = { new ServerPort(host, options.DriverPort, ServerCredentials.Insecure )}
94 };
95 int boundPort = server.Ports.Single().BoundPort;
96 Console.WriteLine("Running qps worker server on " + string.Format("{0}:{1}", host, boundPort));
97 server.Start();
Jan Tattermusch1cbb5672016-02-18 14:27:28 -080098 await tcs.Task;
99 await server.ShutdownAsync();
Jan Tattermuschd0c1bfa2015-10-22 19:14:57 -0700100 }
101 }
102}