EmbeddedClientThroughput.swift 4.0 KB

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