PerformanceWorker.swift 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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 ArgumentParser
  17. import GRPCCore
  18. import GRPCHTTP2Core
  19. import GRPCHTTP2TransportNIOPosix
  20. import NIOPosix
  21. @main
  22. @available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
  23. struct PerformanceWorker: AsyncParsableCommand {
  24. static var configuration: CommandConfiguration {
  25. CommandConfiguration(
  26. commandName: "performance-worker",
  27. discussion: """
  28. This program starts a gRPC server running the 'worker' service. The worker service is \
  29. instructed by a driver program to become a benchmark client or a benchmark server.
  30. Typically at least two workers are started (at least one server and one client), and the \
  31. driver instructs benchmark clients to execute various scenarios against benchmark servers. \
  32. Results are reported back to the driver once scenarios have been completed.
  33. See https://grpc.io/docs/guides/benchmarking for more details.
  34. """
  35. )
  36. }
  37. @Option(
  38. name: .customLong("driver_port"),
  39. help: "Port to listen on for connections from the driver."
  40. )
  41. var driverPort: Int
  42. func run() async throws {
  43. debugOnly {
  44. print("[WARNING] performance-worker built in DEBUG mode, results won't be representative.")
  45. }
  46. let transport = HTTP2ServerTransport.Posix(
  47. address: .ipv4(host: "127.0.0.1", port: self.driverPort),
  48. config: .defaults
  49. )
  50. let server = GRPCServer(transport: transport, services: [WorkerService()])
  51. try await server.run()
  52. }
  53. }
  54. private func debugOnly(_ body: () -> Void) {
  55. assert(alwaysTrue(body))
  56. }
  57. private func alwaysTrue(_ body: () -> Void) -> Bool {
  58. body()
  59. return true
  60. }