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 Foundation
  17. import EchoModel
  18. import EchoImplementation
  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_EchoServiceClient!
  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 configuration = ClientConnection.Configuration(
  39. target: .socketAddress(self.server.channel.localAddress!),
  40. eventLoopGroup: self.group
  41. )
  42. let connection = ClientConnection(configuration: configuration)
  43. self.client = .init(connection: connection)
  44. }
  45. override func run() throws {
  46. let batchSize = 100
  47. for lowerBound in stride(from: 0, to: self.requestCount, by: batchSize) {
  48. let upperBound = min(lowerBound + batchSize, self.requestCount)
  49. let requests = (lowerBound..<upperBound).map { _ in
  50. client.get(Echo_EchoRequest.with { $0.text = self.requestText }).response
  51. }
  52. try EventLoopFuture.andAllSucceed(requests, on: self.client.connection.eventLoop).wait()
  53. }
  54. }
  55. override func tearDown() throws {
  56. try self.client.connection.close().wait()
  57. try self.group.syncShutdownGracefully()
  58. try super.tearDown()
  59. }
  60. }
  61. /// Tests bidirectional throughput by sending requests over a single stream.
  62. class Bidi: Unary {
  63. let batchSize: Int
  64. init(requests: Int, text: String, batchSize: Int) {
  65. self.batchSize = batchSize
  66. super.init(requests: requests, text: text)
  67. }
  68. override func run() throws {
  69. let update = self.client.update { _ in }
  70. for _ in stride(from: 0, to: self.requestCount, by: self.batchSize) {
  71. let batch = (0..<self.batchSize).map { _ in
  72. Echo_EchoRequest.with { $0.text = self.requestText }
  73. }
  74. update.sendMessages(batch, promise: nil)
  75. }
  76. update.sendEnd(promise: nil)
  77. _ = try update.status.wait()
  78. }
  79. }