greeter_monitor.py 1.04 KB
Newer Older
Nane Kratzke's avatar
Nane Kratzke committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import flask
import os, time, requests, statistics
import grpc
import helloworld_pb2
import helloworld_pb2_grpc

grpc_svc = os.environ.get("GRPC_SVC", "localhost")
grpc_port = os.environ.get("GRPC_PORT", "5555")

rest_svc = os.environ.get("REST_SVC", "localhost")
rest_port = os.environ.get("REST_PORT", "5000")

channel = grpc.insecure_channel(f"{ grpc_svc }:{ grpc_port }")
stub = helloworld_pb2_grpc.GreeterStub(channel)

app = flask.Flask(__name__)

@app.route('/', methods=['GET'])
def monitor():
    grpc_s = []
    rest_s = []
    n = 100

    for i in range(0, n):
        start = time.time()
        x = stub.SayHello(helloworld_pb2.HelloRequest(name='you'))
        grpc_s.append(time.time() - start)

        start = time.time()
        y = requests.get(f"http://{ rest_svc }:{ rest_port }/hello/you")
        rest_s.append(time.time() - start)
        
    return ", ".join([
        f"gRPC: { statistics.mean(grpc_s) * 1000 }ms",
        f"REST: { statistics.mean(rest_s) * 1000 }ms",
        f"n = { n }"
    ])

app.run(host="0.0.0.0", port=8080)