def test_multiple_stream():
sys.stdout = ThreadPrinter()
sys.argv = ["", '{"LogLevel": 1, "PingTimeoutDuration": 5000}']
col = MockStreamCollector("MyStreamCollector", 99)
col.start()
t_end = time.time() + 5
# wait for our collector to print its preamble
while len(sys.stdout.lines) == 0 and time.time() < t_end:
time.sleep(.1)
resp = json.loads(sys.stdout.lines[0])
client = StreamCollectorStub(
grpc.insecure_channel(resp["ListenAddress"]))
metric = snap.Metric(
namespace=[snap.NamespaceElement(value="intel"),
snap.NamespaceElement(value="streaming"),
snap.NamespaceElement(value="random"),
snap.NamespaceElement(value="int")],
version=1,
unit="some unit",
description="some description",
config={"send_multiple": True}
)
mtr = iter([CollectArg(metric).pb])
metrics = client.StreamMetrics(mtr)
a = next(metrics)
assert len(a.Metrics_Reply.metrics) == 3
col.stop()
python类insecure_channel()的实例源码
def test_stream_max_collect_duration():
sys.stdout = ThreadPrinter()
sys.argv = ["", '{"LogLevel": 1, "PingTimeoutDuration": 5000}']
col = MockStreamCollector("MyStreamCollector", 99)
col.start()
t_end = time.time() + 5
# wait for our collector to print its preamble
while len(sys.stdout.lines) == 0 and time.time() < t_end:
time.sleep(.1)
resp = json.loads(sys.stdout.lines[0])
client = StreamCollectorStub(
grpc.insecure_channel(resp["ListenAddress"]))
metric = snap.Metric(
namespace=[snap.NamespaceElement(value="intel"),
snap.NamespaceElement(value="streaming"),
snap.NamespaceElement(value="random"),
snap.NamespaceElement(value="int")],
version=1,
config={
"max-collect-duration": 2,
"stream_delay": 3
},
unit="some unit",
description="some description")
col_arg = CollectArg(metric).pb
mtr = iter([col_arg])
metrics = client.StreamMetrics(mtr)
start_waiting_for_new_metric = time.time()
a = next(metrics)
retrieve_metric_time = time.time()
assert round(retrieve_metric_time - start_waiting_for_new_metric) == 2
assert len(a.Metrics_Reply.metrics) == 0
start_waiting_for_new_metric = time.time()
a = next(metrics)
retrieve_metric_time = time.time()
assert round(retrieve_metric_time - start_waiting_for_new_metric) == 1
assert len(a.Metrics_Reply.metrics) == 1
col.stop()
def test_stream_max_metrics_buffer_with_max_collect_duration():
sys.stdout = ThreadPrinter()
sys.argv = ["", '{"LogLevel": 1, "PingTimeoutDuration": 5000}']
col = MockStreamCollector("MyStreamCollector", 99)
col.start()
t_end = time.time() + 5
# wait for our collector to print its preamble
while len(sys.stdout.lines) == 0 and time.time() < t_end:
time.sleep(.1)
resp = json.loads(sys.stdout.lines[0])
client = StreamCollectorStub(
grpc.insecure_channel(resp["ListenAddress"]))
metric = snap.Metric(
namespace=[snap.NamespaceElement(value="intel"),
snap.NamespaceElement(value="streaming"),
snap.NamespaceElement(value="random"),
snap.NamespaceElement(value="int")],
version=1,
config={
"stream_delay": 3,
"max-collect-duration": 4,
"max-metrics-buffer": 3
},
unit="some unit",
description="some description")
col_arg = CollectArg(metric).pb
mtr = iter([col_arg])
metrics = client.StreamMetrics(mtr)
start_waiting_for_new_metric = time.time()
a = next(metrics)
retrieve_metric_time = time.time()
assert round(retrieve_metric_time - start_waiting_for_new_metric) == 4
assert len(a.Metrics_Reply.metrics) == 1
start_waiting_for_new_metric = time.time()
a = next(metrics)
retrieve_metric_time = time.time()
assert round(retrieve_metric_time - start_waiting_for_new_metric) == 4
assert len(a.Metrics_Reply.metrics) == 1
col.stop()
def publisher_client():
"""Returns a client (grpc) fixture that is passed into publisher
tests """
sys.stdout = ThreadPrinter()
pub = MockPublisher("MyPublisher", 1)
pub.start()
t_end = time.time() + 5
# wait for our collector to print its preamble
while len(sys.stdout.lines) == 0 and time.time() < t_end:
time.sleep(.1)
resp = json.loads(sys.stdout.lines[0])
client = PublisherStub(
grpc.insecure_channel(resp["ListenAddress"]))
yield client
pub.stop()
def collector_client():
"""Returns a client (grpc) fixture that is passed into collector tests"""
sys.stdout = ThreadPrinter()
sys.argv = ["", '{"LogLevel": 1, "PingTimeoutDuration": 5000}']
col = MockCollector("MyCollector", 99)
col.start()
t_end = time.time() + 5
# wait for our collector to print its preamble
while len(sys.stdout.lines) == 0 and time.time() < t_end:
time.sleep(.1)
resp = json.loads(sys.stdout.lines[0])
client = CollectorStub(
grpc.insecure_channel(resp["ListenAddress"]))
yield client
col.stop()
def _get_prediction_service_stub():
channel = grpc.insecure_channel('{}:{}'.format('127.0.0.1', 9000))
return prediction_service.PredictionServiceStub(channel)
def run_peer_server_as_process_and_stub(port, radiostation_port=conf.PORT_RADIOSTATION, group_id=None, score=None):
process = run_peer_server_as_process(port, radiostation_port, group_id, score)
channel = grpc.insecure_channel('localhost:' + str(port))
stub = loopchain_pb2_grpc.PeerServiceStub(channel)
util.request_server_in_time(stub.GetStatus, loopchain_pb2.StatusRequest(request=""))
return process, stub
def run_black_peer_server_as_process_and_stub(port, radiostation_port=conf.PORT_RADIOSTATION, group_id=None):
process = run_black_peer_server_as_process(port, radiostation_port, group_id)
channel = grpc.insecure_channel('localhost:' + str(port))
stub = loopchain_pb2_grpc.PeerServiceStub(channel)
return process, stub
def run_radio_station_as_process_and_stub(port):
process = run_radio_station_as_process(port)
channel = grpc.insecure_channel('localhost:' + str(port))
stub = loopchain_pb2_grpc.RadioStationStub(channel)
util.request_server_in_time(stub.GetStatus, loopchain_pb2.StatusRequest(request=""))
return process, stub
def menu4_1(params=None):
admin_manager = AdminManager("demotool")
print("\nInput Peer Target [IP]:[port] (default '' -> 127.0.0.1:7100, [port] -> 127.0.0.1:[port])")
choice = input(" >> ")
if choice == "":
choice = "127.0.0.1:7100"
elif choice.find(':') == -1:
choice = "127.0.0.1:" + choice
select_channel_index = 0
select_channel_string = ""
for channel in admin_manager.get_channel_list():
if select_channel_index != 0:
select_channel_string += ", "
select_channel_string += f"{select_channel_index}: {admin_manager.get_channel_list()[select_channel_index]}"
select_channel_index += 1
print(f"Select Channel ({select_channel_string})")
channel_choice = input(" >> ")
try:
test_globals["channel_name"] = admin_manager.get_channel_list()[int(channel_choice)]
except Exception as e:
print(f"wrong channel number! Now use default channel({admin_manager.get_channel_list()[0]})\n")
test_globals["channel_name"] = admin_manager.get_channel_list()[0]
print("your input: " + choice)
channel = grpc.insecure_channel(choice)
peer_stub = loopchain_pb2_grpc.PeerServiceStub(channel)
response = peer_stub.GetStatus(loopchain_pb2.StatusRequest(request="hello"), conf.GRPC_TIMEOUT)
print("Peer Status: " + str(response))
menu4(peer_stub)
def __make_stub(self, is_stub_reuse=True):
if util.datetime_diff_in_mins(self.__stub_update_time) >= conf.STUB_REUSE_TIMEOUT or \
not is_stub_reuse or self.__stub is None:
util.logger.spam(f"StubManager:__make_stub is_stub_reuse({is_stub_reuse}) self.__stub({self.__stub})")
self.__stub = util.get_stub_to_server(self.__target, self.__stub_type, is_check_status=False)
# if self.__is_secure:
# # TODO need treat to secure channel but not yet
# channel = grpc.insecure_channel(self.__target)
# else:
# channel = grpc.insecure_channel(self.__target)
#
# self.__stub = self.__stub_type(channel)
self.__stub_update_time = datetime.datetime.now()
else:
pass
def set_stub_port(self, port, IP_address):
self.__stub_to_peer_service = loopchain_pb2_grpc.PeerServiceStub(
grpc.insecure_channel(IP_address + ':' + str(port)))
def get_stub_to_server(target, stub_class, time_out_seconds=None, is_check_status=True):
"""gRPC connection to server
:return: stub to server
"""
if time_out_seconds is None:
time_out_seconds = conf.CONNECTION_RETRY_TIMEOUT
stub = None
start_time = timeit.default_timer()
duration = timeit.default_timer() - start_time
while stub is None and duration < time_out_seconds:
try:
logging.debug("(util) get stub to server target: " + str(target))
channel = grpc.insecure_channel(target)
stub = stub_class(channel)
if is_check_status:
stub.Request(loopchain_pb2.Message(code=message_code.Request.status), conf.GRPC_TIMEOUT)
except Exception as e:
logging.warning("Connect to Server Error(get_stub_to_server): " + str(e))
logging.debug("duration(" + str(duration)
+ ") interval(" + str(conf.CONNECTION_RETRY_INTERVAL)
+ ") timeout(" + str(time_out_seconds) + ")")
# RETRY_INTERVAL ?? ??? TIMEOUT ??? ?? ??
time.sleep(conf.CONNECTION_RETRY_INTERVAL)
duration = timeit.default_timer() - start_time
stub = None
return stub
def __handler_connect(self, request, context):
"""make stub to peer service
:param request: message=target of peer_service
:param context:
:return:
"""
logging.debug("__handler_connect %s", request.message)
self.__stub_to_peer_service = loopchain_pb2_grpc.PeerServiceStub(grpc.insecure_channel(request.message))
return_code = (message_code.Response.success, message_code.Response.fail)[self.__stub_to_peer_service is None]
return loopchain_pb2.Message(code=return_code)
def run(self):
remote = self.remote
channel = grpc.insecure_channel(remote)
stub = thegame_pb2_grpc.TheGameStub(channel)
self._queue = queue.Queue()
try:
request_iterator = self._gen()
response_iterator = stub.Game(request_iterator)
for response in response_iterator:
self._game_state = response
self._queue.put(self._response_to_controls(response))
finally:
self._queue.put(Stop)
def run(self):
self._parse()
remote = self.remote
channel = grpc.insecure_channel(remote)
stub = thegame_pb2_grpc.TheGameStub(channel)
for response in stub.View(thegame_pb2.ViewRequest(token=self.token)):
self._game_state = response
self._response_to_controls(response)
def get_stub_from_manager_endpoint(manager_endpoint):
endpoint = manager_endpoint.endpoint
# logger.debug("looking for endpoint %s" % endpoint)
channel = grpc.insecure_channel(endpoint)
return messages_pb2_grpc.ManagerAgentStub(channel)
def run():
channel = grpc.insecure_channel('localhost:50051')
stub = messages_pb2_grpc.RegistrationServiceStub(channel)
response = stub.register(
messages_pb2.RegisterMessage(name='manager_name', endpoint='localhost', description='This is a very long '
'description, bla bla bla'
' bla bla bla bla bla bla '
'bla bla bla'))
print("Greeter client received: %s" % response.result)
def predict(self, request_data, request_timeout=10):
logger.info('Sending request to tfserving model')
logger.info('Model name: ' + str(self.model_name))
logger.info('Model version: ' + str(self.model_version))
logger.info('Host: ' + str(self.host))
tensor_shape = request_data.shape
if self.model_name == 'incv4' or self.model_name == 'res152':
features_tensor_proto = tf.contrib.util.make_tensor_proto(request_data, shape=tensor_shape)
else:
features_tensor_proto = tf.contrib.util.make_tensor_proto(request_data,
dtype=tf.float32, shape=tensor_shape)
# Create gRPC client and request
channel = grpc.insecure_channel(self.host)
stub = PredictionServiceStub(channel)
request = PredictRequest()
request.model_spec.name = self.model_name
if self.model_version > 0:
request.model_spec.version.value = self.model_version
request.inputs['inputs'].CopyFrom(features_tensor_proto)
try:
result = stub.Predict(request, timeout=request_timeout)
logger.info('Got scores with len: ' + str(len(list(result.outputs['scores'].float_val))))
return list(result.outputs['scores'].float_val)
except RpcError as e:
logger.error(e)
logger.error('Prediction failed!')
def predict(self, request_data, request_timeout=10):
logger.info('Sending request to tfserving model')
logger.info('Model name: ' + str(self.model_name))
logger.info('Model version: ' + str(self.model_version))
logger.info('Host: ' + str(self.host))
tensor_shape = request_data.shape
if self.model_name == 'incv4' or self.model_name == 'res152':
features_tensor_proto = tf.contrib.util.make_tensor_proto(request_data, shape=tensor_shape)
else:
features_tensor_proto = tf.contrib.util.make_tensor_proto(request_data,
dtype=tf.float32, shape=tensor_shape)
# Create gRPC client and request
channel = grpc.insecure_channel(self.host)
stub = PredictionServiceStub(channel)
request = PredictRequest()
request.model_spec.name = self.model_name
if self.model_version > 0:
request.model_spec.version.value = self.model_version
request.inputs['inputs'].CopyFrom(features_tensor_proto)
try:
result = stub.Predict(request, timeout=request_timeout)
logger.debug('Predicted scores with len: ' + str(len(list(result.outputs['scores'].float_val))))
return list(result.outputs['scores'].float_val)
except RpcError as e:
logger.warning(e)
logger.warning('Prediction failed. Mock client will return empty prediction of length: '
+ str(self.num_scores))
return [0] * self.num_scores