UnaryThroughput.swift 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * Copyright 2019, 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 NIOCore
  18. import NIOPosix
  19. /// Tests unary throughput by sending requests on a single connection.
  20. ///
  21. /// Requests are sent in batches of (up-to) 100 requests. This is due to
  22. /// https://github.com/apple/swift-nio-http2/issues/87#issuecomment-483542401.
  23. class Unary: ServerProvidingBenchmark {
  24. private var group: EventLoopGroup!
  25. private(set) var client: Echo_EchoClient!
  26. let requestCount: Int
  27. let requestText: String
  28. init(requests: Int, text: String) {
  29. self.requestCount = requests
  30. self.requestText = text
  31. super.init(providers: [MinimalEchoProvider()])
  32. }
  33. override func setUp() throws {
  34. try super.setUp()
  35. self.group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
  36. let channel = ClientConnection.insecure(group: self.group)
  37. .connect(host: "127.0.0.1", port: self.server.channel.localAddress!.port!)
  38. self.client = .init(channel: channel)
  39. }
  40. override func run() throws -> Int {
  41. var messages = 0
  42. let batchSize = 100
  43. for lowerBound in stride(from: 0, to: self.requestCount, by: batchSize) {
  44. let upperBound = min(lowerBound + batchSize, self.requestCount)
  45. let requests = (lowerBound ..< upperBound).map { _ in
  46. client.get(Echo_EchoRequest.with { $0.text = self.requestText }).response
  47. }
  48. messages += requests.count
  49. try EventLoopFuture.andAllSucceed(requests, on: self.group.next()).wait()
  50. }
  51. return messages
  52. }
  53. override func tearDown() throws {
  54. try self.client.channel.close().wait()
  55. try self.group.syncShutdownGracefully()
  56. try super.tearDown()
  57. }
  58. }
  59. /// Tests bidirectional throughput by sending requests over a single stream.
  60. class Bidi: Unary {
  61. let batchSize: Int
  62. init(requests: Int, text: String, batchSize: Int) {
  63. self.batchSize = batchSize
  64. super.init(requests: requests, text: text)
  65. }
  66. override func run() throws -> Int {
  67. var messages = 0
  68. let update = self.client.update { _ in }
  69. for _ in stride(from: 0, to: self.requestCount, by: self.batchSize) {
  70. let batch = (0 ..< self.batchSize).map { _ in
  71. Echo_EchoRequest.with { $0.text = self.requestText }
  72. }
  73. messages += batch.count
  74. update.sendMessages(batch, promise: nil)
  75. }
  76. update.sendEnd(promise: nil)
  77. _ = try update.status.wait()
  78. return messages
  79. }
  80. }