BenchmarkServiceImpl.swift 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 Foundation
  17. import GRPC
  18. import NIO
  19. /// Implementation of asynchronous service for benchmarking.
  20. final class AsyncQPSServerImpl: Grpc_Testing_BenchmarkServiceProvider {
  21. /// One request followed by one response.
  22. /// The server returns the client payload as-is.
  23. func unaryCall(request: Grpc_Testing_SimpleRequest,
  24. context: StatusOnlyCallContext) -> EventLoopFuture<Grpc_Testing_SimpleResponse> {
  25. do {
  26. return context.eventLoop
  27. .makeSucceededFuture(try AsyncQPSServerImpl.processSimpleRPC(request: request))
  28. } catch {
  29. return context.eventLoop.makeFailedFuture(error)
  30. }
  31. }
  32. /// Repeated sequence of one request followed by one response.
  33. /// Should be called streaming ping-pong
  34. /// The server returns the client payload as-is on each response
  35. func streamingCall(
  36. context: StreamingResponseCallContext<Grpc_Testing_SimpleResponse>
  37. ) -> EventLoopFuture<(StreamEvent<Grpc_Testing_SimpleRequest>) -> Void> {
  38. return context.eventLoop.makeSucceededFuture({ event in
  39. switch event {
  40. case let .message(request):
  41. do {
  42. let response = try AsyncQPSServerImpl.processSimpleRPC(request: request)
  43. context.sendResponse(response, promise: nil)
  44. } catch {
  45. context.statusPromise.fail(error)
  46. }
  47. case .end:
  48. context.statusPromise.succeed(.ok)
  49. }
  50. })
  51. }
  52. /// Single-sided unbounded streaming from client to server
  53. /// The server returns the client payload as-is once the client does WritesDone
  54. func streamingFromClient(
  55. context: UnaryResponseCallContext<Grpc_Testing_SimpleResponse>
  56. ) -> EventLoopFuture<(StreamEvent<Grpc_Testing_SimpleRequest>) -> Void> {
  57. context.logger.warning("streamingFromClient not implemented yet")
  58. return context.eventLoop.makeFailedFuture(GRPCStatus(
  59. code: GRPCStatus.Code.unimplemented,
  60. message: "Not implemented"
  61. ))
  62. }
  63. /// Single-sided unbounded streaming from server to client
  64. /// The server repeatedly returns the client payload as-is
  65. func streamingFromServer(
  66. request: Grpc_Testing_SimpleRequest,
  67. context: StreamingResponseCallContext<Grpc_Testing_SimpleResponse>
  68. ) -> EventLoopFuture<GRPCStatus> {
  69. context.logger.warning("streamingFromServer not implemented yet")
  70. return context.eventLoop.makeFailedFuture(GRPCStatus(
  71. code: GRPCStatus.Code.unimplemented,
  72. message: "Not implemented"
  73. ))
  74. }
  75. /// Two-sided unbounded streaming between server to client
  76. /// Both sides send the content of their own choice to the other
  77. func streamingBothWays(
  78. context: StreamingResponseCallContext<Grpc_Testing_SimpleResponse>
  79. ) -> EventLoopFuture<(StreamEvent<Grpc_Testing_SimpleRequest>) -> Void> {
  80. context.logger.warning("streamingBothWays not implemented yet")
  81. return context.eventLoop.makeFailedFuture(GRPCStatus(
  82. code: GRPCStatus.Code.unimplemented,
  83. message: "Not implemented"
  84. ))
  85. }
  86. /// Make a payload for sending back to the client.
  87. private static func makePayload(type: Grpc_Testing_PayloadType,
  88. size: Int) throws -> Grpc_Testing_Payload {
  89. if type != .compressable {
  90. // Making a payload which is not compressable is hard - and not implemented in
  91. // other implementations too.
  92. throw GRPCStatus(code: .internalError, message: "Failed to make payload")
  93. }
  94. var payload = Grpc_Testing_Payload()
  95. payload.body = Data(count: size)
  96. payload.type = type
  97. return payload
  98. }
  99. /// Process a simple RPC.
  100. /// - parameters:
  101. /// - request: The request from the client.
  102. /// - returns: A response to send back to the client.
  103. private static func processSimpleRPC(
  104. request: Grpc_Testing_SimpleRequest
  105. ) throws -> Grpc_Testing_SimpleResponse {
  106. var response = Grpc_Testing_SimpleResponse()
  107. if request.responseSize > 0 {
  108. response.payload = try self.makePayload(
  109. type: request.responseType,
  110. size: Int(request.responseSize)
  111. )
  112. }
  113. return response
  114. }
  115. }