blob: d401fa82d211e33984afad054caa38229f56edb0 [file] [log] [blame]
Jan Tattermuschd0c1bfa2015-10-22 19:14:57 -07001#region Copyright notice and license
2
3// Copyright 2015, Google Inc.
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
34using System;
35using System.Collections.Generic;
36using System.Diagnostics;
37using System.IO;
38using System.Linq;
39using System.Text.RegularExpressions;
40using System.Threading;
41using System.Threading.Tasks;
42using Google.Protobuf;
43using Grpc.Core;
44using Grpc.Core.Utils;
45using NUnit.Framework;
46using Grpc.Testing;
47
48namespace Grpc.IntegrationTesting
49{
50 /// <summary>
51 /// Helper methods to start server runners for performance testing.
52 /// </summary>
53 public static class ServerRunners
54 {
55 /// <summary>
56 /// Creates a started server runner.
57 /// </summary>
58 public static IServerRunner CreateStarted(ServerConfig config)
59 {
60 Grpc.Core.Utils.Preconditions.CheckArgument(config.ServerType == ServerType.ASYNC_SERVER);
61 var credentials = config.SecurityParams != null ? TestCredentials.CreateSslServerCredentials() : ServerCredentials.Insecure;
62
63 // TODO: qps_driver needs to setup payload properly...
64 int responseSize = config.PayloadConfig != null ? config.PayloadConfig.SimpleParams.RespSize : 0;
65 var server = new Server
66 {
67 Services = { BenchmarkService.BindService(new BenchmarkServiceImpl(responseSize)) },
Jan Tattermusch1cbb5672016-02-18 14:27:28 -080068 Ports = { new ServerPort("[::]", config.Port, credentials) }
Jan Tattermuschd0c1bfa2015-10-22 19:14:57 -070069 };
70
71 server.Start();
72 return new ServerRunnerImpl(server);
73 }
74 }
75
76 /// <summary>
77 /// Server runner.
78 /// </summary>
79 public class ServerRunnerImpl : IServerRunner
80 {
81 readonly Server server;
82 readonly WallClockStopwatch wallClockStopwatch = new WallClockStopwatch();
83
84 public ServerRunnerImpl(Server server)
85 {
86 this.server = Grpc.Core.Utils.Preconditions.CheckNotNull(server);
87 }
88
89 public int BoundPort
90 {
91 get
92 {
93 return server.Ports.Single().BoundPort;
94 }
95 }
96
97 /// <summary>
98 /// Gets server stats.
99 /// </summary>
100 /// <returns>The stats.</returns>
101 public ServerStats GetStats(bool reset)
102 {
103 var secondsElapsed = wallClockStopwatch.GetElapsedSnapshot(reset).TotalSeconds;
104
105 // TODO: populate user time and system time
106 return new ServerStats
107 {
108 TimeElapsed = secondsElapsed,
109 TimeUser = 0,
110 TimeSystem = 0
111 };
112 }
113
114 /// <summary>
115 /// Asynchronously stops the server.
116 /// </summary>
117 /// <returns>Task that finishes when server has shutdown.</returns>
118 public Task StopAsync()
119 {
120 return server.ShutdownAsync();
121 }
122 }
123
124}