• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2020 The gRPC Authors
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15import argparse
16import asyncio
17import logging
18
19from grpc.experimental import aio
20
21from src.proto.grpc.testing import worker_service_pb2_grpc
22from tests_aio.benchmark import worker_servicer
23
24
25async def run_worker_server(port: int) -> None:
26    server = aio.server()
27
28    servicer = worker_servicer.WorkerServicer()
29    worker_service_pb2_grpc.add_WorkerServiceServicer_to_server(
30        servicer, server
31    )
32
33    server.add_insecure_port("[::]:{}".format(port))
34
35    await server.start()
36
37    await servicer.wait_for_quit()
38    await server.stop(None)
39
40
41if __name__ == "__main__":
42    logging.basicConfig(level=logging.DEBUG)
43    parser = argparse.ArgumentParser(
44        description="gRPC Python performance testing worker"
45    )
46    parser.add_argument(
47        "--driver_port",
48        type=int,
49        dest="port",
50        help="The port the worker should listen on",
51    )
52    parser.add_argument(
53        "--uvloop", action="store_true", help="Use uvloop or not"
54    )
55    args = parser.parse_args()
56
57    if args.uvloop:
58        import uvloop
59
60        asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
61        loop = uvloop.new_event_loop()
62        asyncio.set_event_loop(loop)
63
64    asyncio.get_event_loop().run_until_complete(run_worker_server(args.port))
65