EmbeddedClientThroughput.swift 3.7 KB

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