| 1 |
# Copyright 2016 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 |
from concurrent import futures |
| 16 |
import multiprocessing |
| 17 |
import random |
| 18 |
import threading |
| 19 |
import time |
| 20 |
|
| 21 |
try: |
| 22 |
# The resource module is not available on Windows. While this server only |
| 23 |
# supports Linux, we must still be _importable_ on Windows. |
| 24 |
import resource |
| 25 |
except ImportError: |
| 26 |
pass |
| 27 |
|
| 28 |
import grpc |
| 29 |
|
| 30 |
from src.proto.grpc.testing import benchmark_service_pb2_grpc |
| 31 |
from src.proto.grpc.testing import control_pb2 |
| 32 |
from src.proto.grpc.testing import stats_pb2 |
| 33 |
from src.proto.grpc.testing import worker_service_pb2_grpc |
| 34 |
from tests.qps import benchmark_client |
| 35 |
from tests.qps import benchmark_server |
| 36 |
from tests.qps import client_runner |
| 37 |
from tests.qps import histogram |
| 38 |
from tests.unit import resources |
| 39 |
from tests.unit import test_common |
| 40 |
|
| 41 |
|
| 42 |
class Snapshotter: |
| 43 |
|
| 44 |
def __init__(self): |
| 45 |
self._start_time = 0.0 |
| 46 |
self._end_time = 0.0 |
| 47 |
self._last_utime = 0.0 |
| 48 |
self._utime = 0.0 |
| 49 |
self._last_stime = 0.0 |
| 50 |
self._stime = 0.0 |
| 51 |
|
| 52 |
def get_time_elapsed(self): |
| 53 |
return self._end_time - self._start_time |
| 54 |
|
| 55 |
def get_utime(self): |
| 56 |
return self._utime - self._last_utime |
| 57 |
|
| 58 |
def get_stime(self): |
| 59 |
return self._stime - self._last_stime |
| 60 |
|
| 61 |
def snapshot(self): |
| 62 |
self._end_time = time.time() |
| 63 |
|
| 64 |
usage = resource.getrusage(resource.RUSAGE_SELF) |
| 65 |
self._utime = usage.ru_utime |
| 66 |
self._stime = usage.ru_stime |
| 67 |
|
| 68 |
def reset(self): |
| 69 |
self._start_time = self._end_time |
| 70 |
self._last_utime = self._utime |
| 71 |
self._last_stime = self._stime |
| 72 |
|
| 73 |
def stats(self): |
| 74 |
return { |
| 75 |
"time_elapsed": self.get_time_elapsed(), |
| 76 |
"time_user": self.get_utime(), |
| 77 |
"time_system": self.get_stime(), |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
class WorkerServer(worker_service_pb2_grpc.WorkerServiceServicer): |
| 82 |
"""Python Worker Server implementation.""" |
| 83 |
|
| 84 |
def __init__(self, server_port=None): |
| 85 |
self._quit_event = threading.Event() |
| 86 |
self._server_port = server_port |
| 87 |
self._snapshotter = Snapshotter() |
| 88 |
|
| 89 |
def RunServer(self, request_iterator, context): |
| 90 |
config = next(request_iterator).setup #pylint: disable=stop-iteration-return |
| 91 |
server, port = self._create_server(config) |
| 92 |
cores = multiprocessing.cpu_count() |
| 93 |
server.start() |
| 94 |
self._snapshotter.snapshot() |
| 95 |
self._snapshotter.reset() |
| 96 |
yield self._get_server_status(port, cores) |
| 97 |
|
| 98 |
for request in request_iterator: |
| 99 |
self._snapshotter.snapshot() |
| 100 |
status = self._get_server_status(port, cores) |
| 101 |
if request.mark.reset: |
| 102 |
self._snapshotter.reset() |
| 103 |
yield status |
| 104 |
server.stop(None) |
| 105 |
|
| 106 |
def _get_server_status(self, port, cores): |
| 107 |
stats = stats_pb2.ServerStats(**self._snapshotter.stats()) |
| 108 |
return control_pb2.ServerStatus(stats=stats, port=port, cores=cores) |
| 109 |
|
| 110 |
def _create_server(self, config): |
| 111 |
if config.async_server_threads == 0: |
| 112 |
# This is the default concurrent.futures thread pool size, but |
| 113 |
# None doesn't seem to work |
| 114 |
server_threads = multiprocessing.cpu_count() * 5 |
| 115 |
else: |
| 116 |
server_threads = config.async_server_threads |
| 117 |
server = test_common.test_server(max_workers=server_threads) |
| 118 |
if config.server_type == control_pb2.ASYNC_SERVER: |
| 119 |
servicer = benchmark_server.BenchmarkServer() |
| 120 |
benchmark_service_pb2_grpc.add_BenchmarkServiceServicer_to_server( |
| 121 |
servicer, server) |
| 122 |
elif config.server_type == control_pb2.ASYNC_GENERIC_SERVER: |
| 123 |
resp_size = config.payload_config.bytebuf_params.resp_size |
| 124 |
servicer = benchmark_server.GenericBenchmarkServer(resp_size) |
| 125 |
method_implementations = { |
| 126 |
'StreamingCall': |
| 127 |
grpc.stream_stream_rpc_method_handler(servicer.StreamingCall |
| 128 |
), |
| 129 |
'UnaryCall': |
| 130 |
grpc.unary_unary_rpc_method_handler(servicer.UnaryCall), |
| 131 |
} |
| 132 |
handler = grpc.method_handlers_generic_handler( |
| 133 |
'grpc.testing.BenchmarkService', method_implementations) |
| 134 |
server.add_generic_rpc_handlers((handler,)) |
| 135 |
else: |
| 136 |
raise Exception('Unsupported server type {}'.format( |
| 137 |
config.server_type)) |
| 138 |
|
| 139 |
if self._server_port is not None and config.port == 0: |
| 140 |
server_port = self._server_port |
| 141 |
else: |
| 142 |
server_port = config.port |
| 143 |
|
| 144 |
if config.HasField('security_params'): # Use SSL |
| 145 |
server_creds = grpc.ssl_server_credentials( |
| 146 |
((resources.private_key(), resources.certificate_chain()),)) |
| 147 |
port = server.add_secure_port('[::]:{}'.format(server_port), |
| 148 |
server_creds) |
| 149 |
else: |
| 150 |
port = server.add_insecure_port('[::]:{}'.format(server_port)) |
| 151 |
|
| 152 |
return (server, port) |
| 153 |
|
| 154 |
def RunClient(self, request_iterator, context): |
| 155 |
config = next(request_iterator).setup #pylint: disable=stop-iteration-return |
| 156 |
client_runners = [] |
| 157 |
qps_data = histogram.Histogram(config.histogram_params.resolution, |
| 158 |
config.histogram_params.max_possible) |
| 159 |
self._snapshotter.snapshot() |
| 160 |
self._snapshotter.reset() |
| 161 |
|
| 162 |
# Create a client for each channel |
| 163 |
for i in range(config.client_channels): |
| 164 |
server = config.server_targets[i % len(config.server_targets)] |
| 165 |
runner = self._create_client_runner(server, config, qps_data) |
| 166 |
client_runners.append(runner) |
| 167 |
runner.start() |
| 168 |
|
| 169 |
self._snapshotter.snapshot() |
| 170 |
yield self._get_client_status(qps_data) |
| 171 |
|
| 172 |
# Respond to stat requests |
| 173 |
for request in request_iterator: |
| 174 |
self._snapshotter.snapshot() |
| 175 |
status = self._get_client_status(qps_data) |
| 176 |
if request.mark.reset: |
| 177 |
qps_data.reset() |
| 178 |
self._snapshotter.reset() |
| 179 |
yield status |
| 180 |
|
| 181 |
# Cleanup the clients |
| 182 |
for runner in client_runners: |
| 183 |
runner.stop() |
| 184 |
|
| 185 |
def _get_client_status(self, qps_data): |
| 186 |
latencies = qps_data.get_data() |
| 187 |
stats = stats_pb2.ClientStats(latencies=latencies, |
| 188 |
**self._snapshotter.stats()) |
| 189 |
return control_pb2.ClientStatus(stats=stats) |
| 190 |
|
| 191 |
def _create_client_runner(self, server, config, qps_data): |
| 192 |
no_ping_pong = False |
| 193 |
if config.client_type == control_pb2.SYNC_CLIENT: |
| 194 |
if config.rpc_type == control_pb2.UNARY: |
| 195 |
client = benchmark_client.UnarySyncBenchmarkClient( |
| 196 |
server, config, qps_data) |
| 197 |
elif config.rpc_type == control_pb2.STREAMING: |
| 198 |
client = benchmark_client.StreamingSyncBenchmarkClient( |
| 199 |
server, config, qps_data) |
| 200 |
elif config.rpc_type == control_pb2.STREAMING_FROM_SERVER: |
| 201 |
no_ping_pong = True |
| 202 |
client = benchmark_client.ServerStreamingSyncBenchmarkClient( |
| 203 |
server, config, qps_data) |
| 204 |
elif config.client_type == control_pb2.ASYNC_CLIENT: |
| 205 |
if config.rpc_type == control_pb2.UNARY: |
| 206 |
client = benchmark_client.UnaryAsyncBenchmarkClient( |
| 207 |
server, config, qps_data) |
| 208 |
else: |
| 209 |
raise Exception('Async streaming client not supported') |
| 210 |
else: |
| 211 |
raise Exception('Unsupported client type {}'.format( |
| 212 |
config.client_type)) |
| 213 |
|
| 214 |
# In multi-channel tests, we split the load across all channels |
| 215 |
load_factor = float(config.client_channels) |
| 216 |
if config.load_params.WhichOneof('load') == 'closed_loop': |
| 217 |
runner = client_runner.ClosedLoopClientRunner( |
| 218 |
client, config.outstanding_rpcs_per_channel, no_ping_pong) |
| 219 |
else: # Open loop Poisson |
| 220 |
alpha = config.load_params.poisson.offered_load / load_factor |
| 221 |
|
| 222 |
def poisson(): |
| 223 |
while True: |
| 224 |
yield random.expovariate(alpha) |
| 225 |
|
| 226 |
runner = client_runner.OpenLoopClientRunner(client, poisson()) |
| 227 |
|
| 228 |
return runner |
| 229 |
|
| 230 |
def CoreCount(self, request, context): |
| 231 |
return control_pb2.CoreResponse(cores=multiprocessing.cpu_count()) |
| 232 |
|
| 233 |
def QuitWorker(self, request, context): |
| 234 |
self._quit_event.set() |
| 235 |
return control_pb2.Void() |
| 236 |
|
| 237 |
def wait_for_quit(self): |
| 238 |
self._quit_event.wait() |