-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.py
97 lines (87 loc) · 2.97 KB
/
client.py
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import grpc
import calc_pb2
import calc_pb2_grpc
import random
from time import sleep
import time
def getInputFromUser():
a = input("Enter value a: ")
b = input("Enter value a: ")
try:
a = int(a)
b = int(b)
except Exception as e:
print(e)
print("No valid values entered, starting again!")
return None, None
return a, b
def randomDigits():
i = 1
sum = 0
while i < 10:
a = random.randint(1, 10)
sum += a
request = calc_pb2.Digits(a=a, counter=i)
print(f"Request {i}: {a}")
yield request
i += 1
print(f"Expected result: {sum}")
def readFile():
file = "client.py"
with open(file, 'rb') as f:
file = f.read()
return file
def run():
with grpc.insecure_channel("localhost:50051") as channel:
stub = calc_pb2_grpc.CalcerStub(channel)
# selection = input("What do you want to calc today + (1), - (2), * (3), / (4), stream random digits to server, get one reply (5), ... get intermediate values (6) ")
selection = "7"
if selection == "1":
print("Adding it is")
a, b = getInputFromUser()
request = calc_pb2.CalcRequest(a=a, b=b)
response = stub.Add(request)
print(response)
elif selection == "2":
print("Subtraction it is")
a, b = getInputFromUser()
request = calc_pb2.CalcRequest(a=a, b=b)
response = stub.Subtract(request)
print(response)
elif selection == "3":
print("Multiplication it is")
a, b = getInputFromUser()
request = calc_pb2.CalcRequest(a=a, b=b)
response = stub.Multiply(request)
print(response)
elif selection == "4":
a, b = getInputFromUser()
request = calc_pb2.CalcRequest(a=a, b=b)
response = stub.Divide(request)
print(response)
elif selection == "5":
# client streams digits to server, server creates *one* response
print("Streaming random values")
response = stub.MultiPlus(randomDigits())
print(response)
elif selection == "6":
# client streams digits to server, server sends back intermediate responses and one final response
print("Streaming random values")
responses = stub.InteractiveMultiPlus(randomDigits())
for response in responses:
print(response)
elif selection == "7":
count = 10000
durationTotal = 0
while count > 0:
start = time.time()
request = calc_pb2.FileRequest(file=readFile())
response = stub.HashFile(request)
end = time.time()
duration = end - start
durationTotal += duration
count -= 1
print(durationTotal)
print(response)
if __name__ == "__main__":
run()