BenchmarkService.swift 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. * Copyright 2024, 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 GRPCCore
  17. import Synchronization
  18. import struct Foundation.Data
  19. final class BenchmarkService: Grpc_Testing_BenchmarkService.ServiceProtocol {
  20. /// Used to check if the server can be streaming responses.
  21. private let working = Atomic(true)
  22. /// One request followed by one response.
  23. /// The server returns a client payload with the size requested by the client.
  24. func unaryCall(
  25. request: ServerRequest<Grpc_Testing_SimpleRequest>,
  26. context: ServerContext
  27. ) async throws -> ServerResponse<Grpc_Testing_SimpleResponse> {
  28. // Throw an error if the status is not `ok`. Otherwise, an `ok` status is automatically sent
  29. // if the request is successful.
  30. if request.message.responseStatus.isInitialized {
  31. try self.checkOkStatus(request.message.responseStatus)
  32. }
  33. return ServerResponse(
  34. message: .with {
  35. $0.payload = Grpc_Testing_Payload.with {
  36. $0.body = Data(count: Int(request.message.responseSize))
  37. }
  38. }
  39. )
  40. }
  41. /// Repeated sequence of one request followed by one response.
  42. /// The server returns a payload with the size requested by the client for each received message.
  43. func streamingCall(
  44. request: StreamingServerRequest<Grpc_Testing_SimpleRequest>,
  45. context: ServerContext
  46. ) async throws -> StreamingServerResponse<Grpc_Testing_SimpleResponse> {
  47. return StreamingServerResponse { writer in
  48. for try await message in request.messages {
  49. if message.responseStatus.isInitialized {
  50. try self.checkOkStatus(message.responseStatus)
  51. }
  52. let responseMessage = Grpc_Testing_SimpleResponse.with {
  53. $0.payload = Grpc_Testing_Payload.with {
  54. $0.body = Data(count: Int(message.responseSize))
  55. }
  56. }
  57. try await writer.write(responseMessage)
  58. }
  59. return [:]
  60. }
  61. }
  62. /// Single-sided unbounded streaming from client to server.
  63. /// The server returns a payload with the size requested by the client once the client does WritesDone.
  64. func streamingFromClient(
  65. request: StreamingServerRequest<Grpc_Testing_SimpleRequest>,
  66. context: ServerContext
  67. ) async throws -> ServerResponse<Grpc_Testing_SimpleResponse> {
  68. var responseSize = 0
  69. for try await message in request.messages {
  70. if message.responseStatus.isInitialized {
  71. try self.checkOkStatus(message.responseStatus)
  72. }
  73. responseSize = Int(message.responseSize)
  74. }
  75. return ServerResponse(
  76. message: .with {
  77. $0.payload = .with {
  78. $0.body = Data(count: responseSize)
  79. }
  80. }
  81. )
  82. }
  83. /// Single-sided unbounded streaming from server to client.
  84. /// The server repeatedly returns a payload with the size requested by the client.
  85. func streamingFromServer(
  86. request: ServerRequest<Grpc_Testing_SimpleRequest>,
  87. context: ServerContext
  88. ) async throws -> StreamingServerResponse<Grpc_Testing_SimpleResponse> {
  89. if request.message.responseStatus.isInitialized {
  90. try self.checkOkStatus(request.message.responseStatus)
  91. }
  92. let response = Grpc_Testing_SimpleResponse.with {
  93. $0.payload = .with {
  94. $0.body = Data(count: Int(request.message.responseSize))
  95. }
  96. }
  97. return StreamingServerResponse { writer in
  98. while self.working.load(ordering: .relaxed) {
  99. try await writer.write(response)
  100. }
  101. return [:]
  102. }
  103. }
  104. /// Two-sided unbounded streaming between server to client.
  105. /// Both sides send the content of their own choice to the other.
  106. func streamingBothWays(
  107. request: StreamingServerRequest<Grpc_Testing_SimpleRequest>,
  108. context: ServerContext
  109. ) async throws -> StreamingServerResponse<Grpc_Testing_SimpleResponse> {
  110. // The 100 size is used by the other implementations as well.
  111. // We are using the same canned response size for all responses
  112. // as it is allowed by the spec.
  113. let response = Grpc_Testing_SimpleResponse.with {
  114. $0.payload = .with {
  115. $0.body = Data(count: 100)
  116. }
  117. }
  118. final class InboundStreamingSignal: Sendable {
  119. private let _isStreaming: Atomic<Bool>
  120. init() {
  121. self._isStreaming = Atomic(true)
  122. }
  123. var isStreaming: Bool {
  124. self._isStreaming.load(ordering: .relaxed)
  125. }
  126. func stop() {
  127. self._isStreaming.store(false, ordering: .relaxed)
  128. }
  129. }
  130. // Marks if the inbound streaming is ongoing or finished.
  131. let inbound = InboundStreamingSignal()
  132. return StreamingServerResponse { writer in
  133. try await withThrowingTaskGroup(of: Void.self) { group in
  134. group.addTask {
  135. for try await message in request.messages {
  136. if message.responseStatus.isInitialized {
  137. try self.checkOkStatus(message.responseStatus)
  138. }
  139. }
  140. inbound.stop()
  141. }
  142. group.addTask {
  143. while inbound.isStreaming && self.working.load(ordering: .acquiring) {
  144. try await writer.write(response)
  145. }
  146. }
  147. try await group.next()
  148. group.cancelAll()
  149. return [:]
  150. }
  151. }
  152. }
  153. }
  154. extension BenchmarkService {
  155. private func checkOkStatus(_ responseStatus: Grpc_Testing_EchoStatus) throws {
  156. guard let code = Status.Code(rawValue: Int(responseStatus.code)) else {
  157. throw RPCError(code: .invalidArgument, message: "The response status code is invalid.")
  158. }
  159. if let code = RPCError.Code(code) {
  160. throw RPCError(code: code, message: responseStatus.message)
  161. }
  162. }
  163. }