PerformanceWorker.swift 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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 GRPCNIOTransportHTTP2
  19. import NIOPosix
  20. @main
  21. struct PerformanceWorker: AsyncParsableCommand {
  22. static var configuration: CommandConfiguration {
  23. CommandConfiguration(
  24. commandName: "performance-worker",
  25. discussion: """
  26. This program starts a gRPC server running the 'worker' service. The worker service is \
  27. instructed by a driver program to become a benchmark client or a benchmark server.
  28. Typically at least two workers are started (at least one server and one client), and the \
  29. driver instructs benchmark clients to execute various scenarios against benchmark servers. \
  30. Results are reported back to the driver once scenarios have been completed.
  31. See https://grpc.io/docs/guides/benchmarking for more details.
  32. """
  33. )
  34. }
  35. @Option(
  36. name: .customLong("driver_port"),
  37. help: "Port to listen on for connections from the driver."
  38. )
  39. var driverPort: Int
  40. func run() async throws {
  41. debugOnly {
  42. print("[WARNING] performance-worker built in DEBUG mode, results won't be representative.")
  43. }
  44. let server = GRPCServer(
  45. transport: .http2NIOPosix(
  46. address: .ipv4(host: "127.0.0.1", port: self.driverPort),
  47. transportSecurity: .plaintext
  48. ),
  49. services: [WorkerService()]
  50. )
  51. try await server.serve()
  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. }