Browse Source

Add performance worker CLI (#1946)

Motivation:

The perf worker needs a CLI to configure the port to listen on as well
as start the worker server.

Modification:

- Add CLI

Result:

Can run performance-worker
George Barnett 1 year ago
parent
commit
ed90b7c7ae

+ 2 - 1
Package.swift

@@ -263,7 +263,8 @@ extension Target {
       .grpcHTTP2TransportNIOPosix,
       .grpcProtobuf,
       .nioCore,
-      .nioFileSystem
+      .nioFileSystem,
+      .argumentParser
     ]
   )
 

+ 70 - 0
Sources/performance-worker/PerformanceWorker.swift

@@ -0,0 +1,70 @@
+/*
+ * Copyright 2024, gRPC Authors All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import ArgumentParser
+import GRPCCore
+import GRPCHTTP2Core
+import GRPCHTTP2TransportNIOPosix
+import NIOPosix
+
+@main
+@available(macOS 14.0, iOS 17.0, watchOS 10.0, tvOS 17.0, *)
+struct PerformanceWorker: AsyncParsableCommand {
+  static var configuration: CommandConfiguration {
+    CommandConfiguration(
+      commandName: "performance-worker",
+      discussion: """
+        This program starts a gRPC server running the 'worker' service. The worker service is \
+        instructed by a driver program to become a benchmark client or a benchmark server.
+
+        Typically at least two workers are started (at least one server and one client), and the \
+        driver instructs benchmark clients to execute various scenarios against benchmark servers. \
+        Results are reported back to the driver once scenarios have been completed.
+
+        See https://grpc.io/docs/guides/benchmarking for more details.
+        """
+    )
+  }
+
+  @Option(
+    name: .customLong("driver_port"),
+    help: "Port to listen on for connections from the driver."
+  )
+  var driverPort: Int
+
+  func run() async throws {
+    debugOnly {
+      print("[WARNING] performance-worker built in DEBUG mode, results won't be representative.")
+    }
+
+    let transport = HTTP2ServerTransport.Posix(
+      address: .ipv4(host: "127.0.0.1", port: self.driverPort),
+      config: .defaults
+    )
+
+    let server = GRPCServer(transport: transport, services: [WorkerService()])
+    try await server.run()
+  }
+}
+
+private func debugOnly(_ body: () -> Void) {
+  assert(alwaysTrue(body))
+}
+
+private func alwaysTrue(_ body: () -> Void) -> Bool {
+  body()
+  return true
+}

+ 0 - 18
Sources/performance-worker/main.swift

@@ -1,18 +0,0 @@
-/*
- * Copyright 2024, gRPC Authors All rights reserved.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-func main(args: [String]) throws {
-}