ServerRunners.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #region Copyright notice and license
  2. // Copyright 2015, Google Inc.
  3. // All rights reserved.
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. #endregion
  31. using System;
  32. using System.Collections.Generic;
  33. using System.Diagnostics;
  34. using System.IO;
  35. using System.Linq;
  36. using System.Text.RegularExpressions;
  37. using System.Threading;
  38. using System.Threading.Tasks;
  39. using Google.Protobuf;
  40. using Grpc.Core;
  41. using Grpc.Core.Utils;
  42. using NUnit.Framework;
  43. using Grpc.Testing;
  44. namespace Grpc.IntegrationTesting
  45. {
  46. /// <summary>
  47. /// Helper methods to start server runners for performance testing.
  48. /// </summary>
  49. public static class ServerRunners
  50. {
  51. /// <summary>
  52. /// Creates a started server runner.
  53. /// </summary>
  54. public static IServerRunner CreateStarted(ServerConfig config)
  55. {
  56. Grpc.Core.Utils.Preconditions.CheckArgument(config.ServerType == ServerType.ASYNC_SERVER);
  57. var credentials = config.SecurityParams != null ? TestCredentials.CreateSslServerCredentials() : ServerCredentials.Insecure;
  58. // TODO: qps_driver needs to setup payload properly...
  59. int responseSize = config.PayloadConfig != null ? config.PayloadConfig.SimpleParams.RespSize : 0;
  60. var server = new Server
  61. {
  62. Services = { BenchmarkService.BindService(new BenchmarkServiceImpl(responseSize)) },
  63. Ports = { new ServerPort(config.Host, config.Port, credentials) }
  64. };
  65. server.Start();
  66. return new ServerRunnerImpl(server);
  67. }
  68. }
  69. /// <summary>
  70. /// Server runner.
  71. /// </summary>
  72. public class ServerRunnerImpl : IServerRunner
  73. {
  74. readonly Server server;
  75. readonly WallClockStopwatch wallClockStopwatch = new WallClockStopwatch();
  76. public ServerRunnerImpl(Server server)
  77. {
  78. this.server = Grpc.Core.Utils.Preconditions.CheckNotNull(server);
  79. }
  80. public int BoundPort
  81. {
  82. get
  83. {
  84. return server.Ports.Single().BoundPort;
  85. }
  86. }
  87. /// <summary>
  88. /// Gets server stats.
  89. /// </summary>
  90. /// <returns>The stats.</returns>
  91. public ServerStats GetStats(bool reset)
  92. {
  93. var secondsElapsed = wallClockStopwatch.GetElapsedSnapshot(reset).TotalSeconds;
  94. // TODO: populate user time and system time
  95. return new ServerStats
  96. {
  97. TimeElapsed = secondsElapsed,
  98. TimeUser = 0,
  99. TimeSystem = 0
  100. };
  101. }
  102. /// <summary>
  103. /// Asynchronously stops the server.
  104. /// </summary>
  105. /// <returns>Task that finishes when server has shutdown.</returns>
  106. public Task StopAsync()
  107. {
  108. return server.ShutdownAsync();
  109. }
  110. }
  111. }