PerformanceWorker.swift 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 server = GRPCServer(
  47. transport: .http2NIOPosix(
  48. address: .ipv4(host: "127.0.0.1", port: self.driverPort),
  49. config: .defaults(transportSecurity: .plaintext)
  50. ),
  51. services: [WorkerService()]
  52. )
  53. try await server.serve()
  54. }
  55. }
  56. private func debugOnly(_ body: () -> Void) {
  57. assert(alwaysTrue(body))
  58. }
  59. private func alwaysTrue(_ body: () -> Void) -> Bool {
  60. body()
  61. return true
  62. }