• 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    server.add_insecure_port('[::]:{}'.format(port))
33
34    await server.start()
35
36    await servicer.wait_for_quit()
37    await server.stop(None)
38
39
40if __name__ == '__main__':
41    logging.basicConfig(level=logging.DEBUG)
42    parser = argparse.ArgumentParser(
43        description='gRPC Python performance testing worker')
44    parser.add_argument('--driver_port',
45                        type=int,
46                        dest='port',
47                        help='The port the worker should listen on')
48    parser.add_argument('--uvloop',
49                        action='store_true',
50                        help='Use uvloop or not')
51    args = parser.parse_args()
52
53    if args.uvloop:
54        import uvloop
55        asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
56        loop = uvloop.new_event_loop()
57        asyncio.set_event_loop(loop)
58
59    asyncio.get_event_loop().run_until_complete(run_worker_server(args.port))
60