EmbeddedClientThroughput.swift 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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")
  38. self.requestHead = _GRPCRequestHead(
  39. method: "POST",
  40. scheme: "http",
  41. path: "/echo.Echo/Get",
  42. host: "localhost",
  43. timeout: .infinite,
  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.pipeline.addHandlers([
  57. _GRPCClientChannelHandler<Echo_EchoRequest, Echo_EchoResponse>(streamID: .init(1), callType: .unary, logger: self.logger),
  58. _UnaryRequestChannelHandler(requestHead: self.requestHead, request: .init(self.request, compressed: false))
  59. ]).wait()
  60. // Trigger the request handler.
  61. channel.pipeline.fireChannelActive()
  62. // Read out the request frames.
  63. var requestFrames = 0
  64. while let _ = try channel.readOutbound(as: HTTP2Frame.self) {
  65. requestFrames += 1
  66. }
  67. assert(requestFrames == 3) // headers, data, empty data (end-stream)
  68. // Okay, let's build a response.
  69. // Required headers.
  70. let responseHeaders: HPACKHeaders = [
  71. ":status": "200",
  72. "content-type": "application/grpc+proto"
  73. ]
  74. let headerFrame = HTTP2Frame(streamID: .init(1), payload: .headers(.init(headers: responseHeaders)))
  75. // Some data.
  76. let response = try Echo_EchoResponse.with { $0.text = self.requestText }.serializedData()
  77. var buffer = channel.allocator.buffer(capacity: response.count + 5)
  78. buffer.writeInteger(UInt8(0)) // compression byte
  79. buffer.writeInteger(UInt32(response.count))
  80. buffer.writeBytes(response)
  81. let dataFrame = HTTP2Frame(streamID: .init(1), payload: .data(.init(data: .byteBuffer(buffer))))
  82. // Required trailers.
  83. let responseTrailers: HPACKHeaders = [
  84. "grpc-status": "0",
  85. "grpc-message": "ok"
  86. ]
  87. let trailersFrame = HTTP2Frame(streamID: .init(1), payload: .headers(.init(headers: responseTrailers)))
  88. // Now write the response frames back into the channel.
  89. try channel.writeInbound(headerFrame)
  90. try channel.writeInbound(dataFrame)
  91. try channel.writeInbound(trailersFrame)
  92. // And read them back out.
  93. var responseParts = 0
  94. while let _ = try channel.readOutbound(as: _GRPCClientResponsePart<Echo_EchoResponse>.self) {
  95. responseParts += 1
  96. }
  97. assert(responseParts == 4, "received \(responseParts) response parts")
  98. }
  99. }
  100. }