_EmbeddedThroughput.swift 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*
  2. * Copyright 2020, 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 Logging
  17. import NIOCore
  18. import NIOEmbedded
  19. import SwiftProtobuf
  20. extension EmbeddedChannel {
  21. /// Configures an `EmbeddedChannel` for the `EmbeddedClientThroughput` benchmark.
  22. ///
  23. /// - Important: This is **not** part of the public API.
  24. public func _configureForEmbeddedThroughputTest<Request: Message, Response: Message>(
  25. callType: GRPCCallType,
  26. logger: Logger,
  27. requestType: Request.Type = Request.self,
  28. responseType: Response.Type = Response.self
  29. ) -> EventLoopFuture<Void> {
  30. return self.pipeline.addHandlers([
  31. GRPCClientChannelHandler(
  32. callType: callType,
  33. maximumReceiveMessageLength: .max,
  34. logger: logger
  35. ),
  36. GRPCClientCodecHandler(
  37. serializer: ProtobufSerializer<Request>(),
  38. deserializer: ProtobufDeserializer<Response>()
  39. ),
  40. ])
  41. }
  42. public func _configureForEmbeddedServerTest(
  43. servicesByName serviceProviders: [Substring: CallHandlerProvider],
  44. encoding: ServerMessageEncoding,
  45. normalizeHeaders: Bool,
  46. logger: Logger
  47. ) -> EventLoopFuture<Void> {
  48. let codec = HTTP2ToRawGRPCServerCodec(
  49. servicesByName: serviceProviders,
  50. encoding: encoding,
  51. errorDelegate: nil,
  52. normalizeHeaders: normalizeHeaders,
  53. maximumReceiveMessageLength: .max,
  54. logger: logger
  55. )
  56. return self.pipeline.addHandler(codec)
  57. }
  58. public func _configureForServerFuzzing(configuration: Server.Configuration) throws {
  59. let configurator = GRPCServerPipelineConfigurator(configuration: configuration)
  60. // We're always on an `EmbeddedEventLoop`, this is fine.
  61. try self.pipeline.syncOperations.addHandler(configurator)
  62. }
  63. }