EmbeddedClientThroughput.swift 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 NIO
  17. import NIOHTTP2
  18. import NIOHPACK
  19. import GRPC
  20. import EchoModel
  21. import Logging
  22. /// Tests the throughput on the client side by firing a unary request through an embedded channel
  23. /// and writing back enough gRPC as HTTP/2 frames to get through the state machine.
  24. ///
  25. /// This only measures the handlers in the child channel.
  26. class EmbeddedClientThroughput: Benchmark {
  27. private let requestCount: Int
  28. private let requestText: String
  29. private var logger: Logger!
  30. private var requestHead: _GRPCRequestHead!
  31. private var request: Echo_EchoRequest!
  32. init(requests: Int, text: String) {
  33. self.requestCount = requests
  34. self.requestText = text
  35. }
  36. func setUp() throws {
  37. self.logger = Logger(label: "io.grpc.testing", factory: { _ in SwiftLogNoOpLogHandler() })
  38. self.requestHead = _GRPCRequestHead(
  39. method: "POST",
  40. scheme: "http",
  41. path: "/echo.Echo/Get",
  42. host: "localhost",
  43. deadline: .distantFuture,
  44. customMetadata: [:],
  45. encoding: .disabled
  46. )
  47. self.request = .with {
  48. $0.text = self.requestText
  49. }
  50. }
  51. func tearDown() throws {
  52. }
  53. func run() throws {
  54. for _ in 0..<self.requestCount {
  55. let channel = EmbeddedChannel()
  56. try channel._configureForEmbeddedThroughputTest(
  57. callType: .unary,
  58. logger: self.logger,
  59. requestType: Echo_EchoRequest.self,
  60. responseType: Echo_EchoResponse.self
  61. ).wait()
  62. // Trigger the request handler.
  63. channel.pipeline.fireChannelActive()
  64. // Write the request parts.
  65. try channel.writeOutbound(_GRPCClientRequestPart<Echo_EchoRequest>.head(self.requestHead))
  66. try channel.writeOutbound(_GRPCClientRequestPart<Echo_EchoRequest>.message(.init(self.request, compressed: false)))
  67. try channel.writeOutbound(_GRPCClientRequestPart<Echo_EchoRequest>.end)
  68. // Read out the request frames.
  69. var requestFrames = 0
  70. while let _ = try channel.readOutbound(as: HTTP2Frame.self) {
  71. requestFrames += 1
  72. }
  73. assert(requestFrames == 3) // headers, data, empty data (end-stream)
  74. // Okay, let's build a response.
  75. // Required headers.
  76. let responseHeaders: HPACKHeaders = [
  77. ":status": "200",
  78. "content-type": "application/grpc+proto"
  79. ]
  80. let headerFrame = HTTP2Frame(streamID: .init(1), payload: .headers(.init(headers: responseHeaders)))
  81. // Some data.
  82. let response = try Echo_EchoResponse.with { $0.text = self.requestText }.serializedData()
  83. var buffer = channel.allocator.buffer(capacity: response.count + 5)
  84. buffer.writeInteger(UInt8(0)) // compression byte
  85. buffer.writeInteger(UInt32(response.count))
  86. buffer.writeBytes(response)
  87. let dataFrame = HTTP2Frame(streamID: .init(1), payload: .data(.init(data: .byteBuffer(buffer))))
  88. // Required trailers.
  89. let responseTrailers: HPACKHeaders = [
  90. "grpc-status": "0",
  91. "grpc-message": "ok"
  92. ]
  93. let trailersFrame = HTTP2Frame(streamID: .init(1), payload: .headers(.init(headers: responseTrailers)))
  94. // Now write the response frames back into the channel.
  95. try channel.writeInbound(headerFrame)
  96. try channel.writeInbound(dataFrame)
  97. try channel.writeInbound(trailersFrame)
  98. // And read them back out.
  99. var responseParts = 0
  100. while let _ = try channel.readOutbound(as: _GRPCClientResponsePart<Echo_EchoResponse>.self) {
  101. responseParts += 1
  102. }
  103. assert(responseParts == 4, "received \(responseParts) response parts")
  104. }
  105. }
  106. }