• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #region Copyright notice and license
2 
3 // Copyright 2015 gRPC authors.
4 //
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
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
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.
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;
30 using Grpc.Core.Logging;
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         {
41             [Option("driver_port", Default = 0)]
42             public int DriverPort { get; set; }
43         }
44 
45         ServerOptions options;
46 
QpsWorker(ServerOptions options)47         private QpsWorker(ServerOptions options)
48         {
49             this.options = options;
50         }
51 
Run(string[] args)52         public static void Run(string[] args)
53         {
54             GrpcEnvironment.SetLogger(new ConsoleLogger());
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                 });
62         }
63 
RunAsync()64         private async Task RunAsync()
65         {
66             string host = "0.0.0.0";
67             int port = options.DriverPort;
68 
69             var tcs = new TaskCompletionSource<object>();
70             var workerServiceImpl = new WorkerServiceImpl(() => { Task.Run(() => tcs.SetResult(null)); });
71 
72             var server = new Server
73             {
74                 Services = { WorkerService.BindService(workerServiceImpl) },
75                 Ports = { new ServerPort(host, options.DriverPort, ServerCredentials.Insecure )}
76             };
77             int boundPort = server.Ports.Single().BoundPort;
78             GrpcEnvironment.Logger.Info("Running qps worker server on {0}:{1}", host, boundPort);
79             server.Start();
80             await tcs.Task;
81             await server.ShutdownAsync();
82 
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));
85         }
86     }
87 }
88