2
0

UnaryThroughput.swift 2.8 KB

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