blob: b17b2c2183a2860714503829fa04966f24dc77f1 [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;
Jan Tattermusch66f85782017-02-24 19:44:16 +010045using Grpc.Core.Logging;
Jan Tattermuschd0c1bfa2015-10-22 19:14:57 -070046using Grpc.Core.Utils;
47using Grpc.Testing;
48using NUnit.Framework;
49
50namespace Grpc.IntegrationTesting
51{
52 public class QpsWorker
53 {
54 private class ServerOptions
55 {
Jan Tattermusch678ec902016-08-08 12:54:16 +080056 [Option("driver_port", Default = 0)]
Jan Tattermuschd0c1bfa2015-10-22 19:14:57 -070057 public int DriverPort { get; set; }
Jan Tattermuschd0c1bfa2015-10-22 19:14:57 -070058 }
59
60 ServerOptions options;
61
62 private QpsWorker(ServerOptions options)
63 {
64 this.options = options;
65 }
66
67 public static void Run(string[] args)
68 {
Jan Tattermusch66f85782017-02-24 19:44:16 +010069 GrpcEnvironment.SetLogger(new ConsoleLogger());
Jan Tattermusch90cbde82016-08-08 14:33:28 +080070 var parserResult = Parser.Default.ParseArguments<ServerOptions>(args)
71 .WithNotParsed((x) => Environment.Exit(1))
72 .WithParsed(options =>
73 {
74 var workerServer = new QpsWorker(options);
75 workerServer.RunAsync().Wait();
76 });
Jan Tattermuschd0c1bfa2015-10-22 19:14:57 -070077 }
78
Jan Tattermusch1cbb5672016-02-18 14:27:28 -080079 private async Task RunAsync()
Jan Tattermuschd0c1bfa2015-10-22 19:14:57 -070080 {
Jan Tattermusch8c31c142016-10-22 11:26:50 +020081 // (ThreadPoolSize == ProcessorCount) gives best throughput in benchmarks
82 // and doesn't seem to harm performance even when server and client
83 // are running on the same machine.
84 GrpcEnvironment.SetThreadPoolSize(Environment.ProcessorCount);
85
Jan Tattermuschd0c1bfa2015-10-22 19:14:57 -070086 string host = "0.0.0.0";
87 int port = options.DriverPort;
88
Jan Tattermusch1cbb5672016-02-18 14:27:28 -080089 var tcs = new TaskCompletionSource<object>();
90 var workerServiceImpl = new WorkerServiceImpl(() => { Task.Run(() => tcs.SetResult(null)); });
91
Jan Tattermuschd0c1bfa2015-10-22 19:14:57 -070092 var server = new Server
93 {
Jan Tattermusch1cbb5672016-02-18 14:27:28 -080094 Services = { WorkerService.BindService(workerServiceImpl) },
Jan Tattermuschd0c1bfa2015-10-22 19:14:57 -070095 Ports = { new ServerPort(host, options.DriverPort, ServerCredentials.Insecure )}
96 };
97 int boundPort = server.Ports.Single().BoundPort;
98 Console.WriteLine("Running qps worker server on " + string.Format("{0}:{1}", host, boundPort));
99 server.Start();
Jan Tattermusch1cbb5672016-02-18 14:27:28 -0800100 await tcs.Task;
101 await server.ShutdownAsync();
Jan Tattermuschd0c1bfa2015-10-22 19:14:57 -0700102 }
103 }
104}