BenchmarkService.swift 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. private import Atomics
  17. internal import GRPCCore
  18. import struct Foundation.Data
  19. @available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
  20. struct BenchmarkService: Grpc_Testing_BenchmarkService.ServiceProtocol {
  21. /// Used to check if the server can be streaming responses.
  22. private let working = ManagedAtomic<Bool>(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. ) async throws -> ServerResponse.Single<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.Single(
  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: ServerRequest.Stream<Grpc_Testing_SimpleRequest>
  45. ) async throws -> ServerResponse.Stream<Grpc_Testing_SimpleResponse> {
  46. return ServerResponse.Stream { writer in
  47. for try await message in request.messages {
  48. if message.responseStatus.isInitialized {
  49. try self.checkOkStatus(message.responseStatus)
  50. }
  51. let responseMessage = Grpc_Testing_SimpleResponse.with {
  52. $0.payload = Grpc_Testing_Payload.with {
  53. $0.body = Data(count: Int(message.responseSize))
  54. }
  55. }
  56. try await writer.write(responseMessage)
  57. }
  58. return [:]
  59. }
  60. }
  61. /// Single-sided unbounded streaming from client to server.
  62. /// The server returns a payload with the size requested by the client once the client does WritesDone.
  63. func streamingFromClient(
  64. request: ServerRequest.Stream<Grpc_Testing_SimpleRequest>
  65. ) async throws -> ServerResponse.Single<Grpc_Testing_SimpleResponse> {
  66. var responseSize = 0
  67. for try await message in request.messages {
  68. if message.responseStatus.isInitialized {
  69. try self.checkOkStatus(message.responseStatus)
  70. }
  71. responseSize = Int(message.responseSize)
  72. }
  73. return ServerResponse.Single(
  74. message: .with {
  75. $0.payload = .with {
  76. $0.body = Data(count: responseSize)
  77. }
  78. }
  79. )
  80. }
  81. /// Single-sided unbounded streaming from server to client.
  82. /// The server repeatedly returns a payload with the size requested by the client.
  83. func streamingFromServer(
  84. request: ServerRequest.Single<Grpc_Testing_SimpleRequest>
  85. ) async throws -> ServerResponse.Stream<Grpc_Testing_SimpleResponse> {
  86. if request.message.responseStatus.isInitialized {
  87. try self.checkOkStatus(request.message.responseStatus)
  88. }
  89. let response = Grpc_Testing_SimpleResponse.with {
  90. $0.payload = .with {
  91. $0.body = Data(count: Int(request.message.responseSize))
  92. }
  93. }
  94. return ServerResponse.Stream { writer in
  95. while self.working.load(ordering: .relaxed) {
  96. try await writer.write(response)
  97. }
  98. return [:]
  99. }
  100. }
  101. /// Two-sided unbounded streaming between server to client.
  102. /// Both sides send the content of their own choice to the other.
  103. func streamingBothWays(
  104. request: ServerRequest.Stream<Grpc_Testing_SimpleRequest>
  105. ) async throws -> ServerResponse.Stream<Grpc_Testing_SimpleResponse> {
  106. // The 100 size is used by the other implementations as well.
  107. // We are using the same canned response size for all responses
  108. // as it is allowed by the spec.
  109. let response = Grpc_Testing_SimpleResponse.with {
  110. $0.payload = .with {
  111. $0.body = Data(count: 100)
  112. }
  113. }
  114. // Marks if the inbound streaming is ongoing or finished.
  115. let inboundStreaming = ManagedAtomic<Bool>(true)
  116. return ServerResponse.Stream { writer in
  117. try await withThrowingTaskGroup(of: Void.self) { group in
  118. group.addTask {
  119. for try await message in request.messages {
  120. if message.responseStatus.isInitialized {
  121. try self.checkOkStatus(message.responseStatus)
  122. }
  123. }
  124. inboundStreaming.store(false, ordering: .relaxed)
  125. }
  126. group.addTask {
  127. while inboundStreaming.load(ordering: .relaxed)
  128. && self.working.load(ordering: .acquiring)
  129. {
  130. try await writer.write(response)
  131. }
  132. }
  133. try await group.next()
  134. group.cancelAll()
  135. return [:]
  136. }
  137. }
  138. }
  139. }
  140. @available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
  141. extension BenchmarkService {
  142. private func checkOkStatus(_ responseStatus: Grpc_Testing_EchoStatus) throws {
  143. guard let code = Status.Code(rawValue: Int(responseStatus.code)) else {
  144. throw RPCError(code: .invalidArgument, message: "The response status code is invalid.")
  145. }
  146. if let code = RPCError.Code(code) {
  147. throw RPCError(code: code, message: responseStatus.message)
  148. }
  149. }
  150. }