| 1 |
# Copyright 2019 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 |
|
| 15 |
import contextlib |
| 16 |
import datetime |
| 17 |
import subprocess |
| 18 |
import sys |
| 19 |
import threading |
| 20 |
import time |
| 21 |
|
| 22 |
import grpc |
| 23 |
import grpc.experimental |
| 24 |
|
| 25 |
_PORT = 5741 |
| 26 |
_MESSAGE_SIZE = 4 |
| 27 |
_RESPONSE_COUNT = 32 * 1024 |
| 28 |
|
| 29 |
_SERVER_CODE = """ |
| 30 |
import datetime |
| 31 |
import threading |
| 32 |
import grpc |
| 33 |
from concurrent import futures |
| 34 |
from src.python.grpcio_tests.tests.stress import unary_stream_benchmark_pb2 |
| 35 |
from src.python.grpcio_tests.tests.stress import unary_stream_benchmark_pb2_grpc |
| 36 |
|
| 37 |
class Handler(unary_stream_benchmark_pb2_grpc.UnaryStreamBenchmarkServiceServicer): |
| 38 |
|
| 39 |
def Benchmark(self, request, context): |
| 40 |
payload = b'\\x00\\x01' * int(request.message_size / 2) |
| 41 |
for _ in range(request.response_count): |
| 42 |
yield unary_stream_benchmark_pb2.BenchmarkResponse(response=payload) |
| 43 |
|
| 44 |
|
| 45 |
server = grpc.server(futures.ThreadPoolExecutor(max_workers=1)) |
| 46 |
server.add_insecure_port('[::]:%d') |
| 47 |
unary_stream_benchmark_pb2_grpc.add_UnaryStreamBenchmarkServiceServicer_to_server(Handler(), server) |
| 48 |
server.start() |
| 49 |
server.wait_for_termination() |
| 50 |
""" % _PORT |
| 51 |
|
| 52 |
try: |
| 53 |
from src.python.grpcio_tests.tests.stress import \ |
| 54 |
unary_stream_benchmark_pb2_grpc |
| 55 |
from src.python.grpcio_tests.tests.stress import unary_stream_benchmark_pb2 |
| 56 |
|
| 57 |
_GRPC_CHANNEL_OPTIONS = [ |
| 58 |
('grpc.max_metadata_size', 16 * 1024 * 1024), |
| 59 |
('grpc.max_receive_message_length', 64 * 1024 * 1024), |
| 60 |
(grpc.experimental.ChannelOptions.SingleThreadedUnaryStream, 1), |
| 61 |
] |
| 62 |
|
| 63 |
@contextlib.contextmanager |
| 64 |
def _running_server(): |
| 65 |
server_process = subprocess.Popen([sys.executable, '-c', _SERVER_CODE], |
| 66 |
stdout=subprocess.PIPE, |
| 67 |
stderr=subprocess.PIPE) |
| 68 |
try: |
| 69 |
yield |
| 70 |
finally: |
| 71 |
server_process.terminate() |
| 72 |
server_process.wait() |
| 73 |
sys.stdout.write("stdout: {}".format(server_process.stdout.read())) |
| 74 |
sys.stdout.flush() |
| 75 |
sys.stdout.write("stderr: {}".format(server_process.stderr.read())) |
| 76 |
sys.stdout.flush() |
| 77 |
|
| 78 |
def profile(message_size, response_count): |
| 79 |
request = unary_stream_benchmark_pb2.BenchmarkRequest( |
| 80 |
message_size=message_size, response_count=response_count) |
| 81 |
with grpc.insecure_channel('[::]:{}'.format(_PORT), |
| 82 |
options=_GRPC_CHANNEL_OPTIONS) as channel: |
| 83 |
stub = unary_stream_benchmark_pb2_grpc.UnaryStreamBenchmarkServiceStub( |
| 84 |
channel) |
| 85 |
start = datetime.datetime.now() |
| 86 |
call = stub.Benchmark(request, wait_for_ready=True) |
| 87 |
for message in call: |
| 88 |
pass |
| 89 |
end = datetime.datetime.now() |
| 90 |
return end - start |
| 91 |
|
| 92 |
def main(): |
| 93 |
with _running_server(): |
| 94 |
for i in range(1000): |
| 95 |
latency = profile(_MESSAGE_SIZE, 1024) |
| 96 |
sys.stdout.write("{}\n".format(latency.total_seconds())) |
| 97 |
sys.stdout.flush() |
| 98 |
|
| 99 |
if __name__ == '__main__': |
| 100 |
main() |
| 101 |
|
| 102 |
except ImportError: |
| 103 |
# NOTE(rbellevi): The test runner should not load this module. |
| 104 |
pass |