test_unary_1k_ping_pong.swift 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * Copyright 2021, gRPC Authors All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. import GRPC
  17. import NIO
  18. class UnaryPingPongBenchmark: Benchmark {
  19. let rpcs: Int
  20. let request: Echo_EchoRequest
  21. private var group: EventLoopGroup!
  22. private var server: Server!
  23. private var client: ClientConnection!
  24. private let clientInterceptors: Echo_EchoClientInterceptorFactoryProtocol?
  25. private let serverInterceptors: Echo_EchoServerInterceptorFactoryProtocol?
  26. init(
  27. rpcs: Int,
  28. request: String,
  29. clientInterceptors: Int = 0,
  30. serverInterceptors: Int = 0
  31. ) {
  32. self.rpcs = rpcs
  33. self.request = .with { $0.text = request }
  34. self.clientInterceptors = clientInterceptors > 0
  35. ? makeEchoClientInterceptors(count: clientInterceptors)
  36. : nil
  37. self.serverInterceptors = serverInterceptors > 0
  38. ? makeEchoServerInterceptors(count: serverInterceptors)
  39. : nil
  40. }
  41. func setUp() throws {
  42. self.group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
  43. self.server = try makeEchoServer(
  44. group: self.group,
  45. interceptors: self.serverInterceptors
  46. ).wait()
  47. self.client = makeClientConnection(
  48. group: self.group,
  49. port: self.server.channel.localAddress!.port!
  50. )
  51. }
  52. func tearDown() throws {
  53. try self.client.close().wait()
  54. try self.server.close().wait()
  55. try self.group.syncShutdownGracefully()
  56. }
  57. func run() throws -> Int {
  58. let echo = Echo_EchoClient(channel: self.client, interceptors: self.clientInterceptors)
  59. var responseLength = 0
  60. for _ in 0 ..< self.rpcs {
  61. let get = echo.get(self.request)
  62. let response = try get.response.wait()
  63. responseLength += response.text.count
  64. }
  65. return responseLength
  66. }
  67. }
  68. func run(identifier: String) {
  69. measure(identifier: identifier) {
  70. let benchmark = UnaryPingPongBenchmark(rpcs: 1000, request: "")
  71. return try! benchmark.runOnce()
  72. }
  73. measure(identifier: identifier + "_interceptors_server") {
  74. let benchmark = UnaryPingPongBenchmark(rpcs: 1000, request: "", serverInterceptors: 5)
  75. return try! benchmark.runOnce()
  76. }
  77. measure(identifier: identifier + "_interceptors_client") {
  78. let benchmark = UnaryPingPongBenchmark(rpcs: 1000, request: "", clientInterceptors: 5)
  79. return try! benchmark.runOnce()
  80. }
  81. }