UnaryThroughput.swift 2.7 KB

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