test_unary_1k_ping_pong.swift 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. init(rpcs: Int, request: String) {
  25. self.rpcs = rpcs
  26. self.request = .with { $0.text = request }
  27. }
  28. func setUp() throws {
  29. self.group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
  30. self.server = try makeEchoServer(group: self.group).wait()
  31. self.client = makeClientConnection(
  32. group: self.group,
  33. port: self.server.channel.localAddress!.port!
  34. )
  35. }
  36. func tearDown() throws {
  37. try self.client.close().wait()
  38. try self.server.close().wait()
  39. try self.group.syncShutdownGracefully()
  40. }
  41. func run() throws -> Int {
  42. let echo = Echo_EchoClient(channel: self.client)
  43. var responseLength = 0
  44. for _ in 0 ..< self.rpcs {
  45. let get = echo.get(self.request)
  46. let response = try get.response.wait()
  47. responseLength += response.text.count
  48. }
  49. return responseLength
  50. }
  51. }
  52. func run(identifier: String) {
  53. measure(identifier: identifier) {
  54. let benchmark = UnaryPingPongBenchmark(rpcs: 1000, request: "")
  55. return try! benchmark.runOnce()
  56. }
  57. }