BenchmarkService.swift 6.1 KB

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