-
Notifications
You must be signed in to change notification settings - Fork 0
/
Single_Throughput_benchmark.py
184 lines (174 loc) · 5.52 KB
/
Single_Throughput_benchmark.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import sys
import random
import socket
import time
import _thread
from threading import Lock
import selectors
from subprocess import Popen, PIPE
inp = []
ADDR_FAMILY = socket.AF_INET
SOCK_TYPE = socket.SOCK_STREAM
SERVERS_IP = ['localhost:4322', 'localhost:4324', 'localhost:4326']
BUF_SIZE = 8000
responsed = 0
err_cnt = 0
delay_analysis = []
error_analysis = []
delay = []
sum_delay = 0.0
change_in_leader = False
sel = selectors.DefaultSelector()
connection_id = {}
ret = {}
change_in_leader = False
VALID_ERROR_PERCENTAGE = 0.1
MAX_RPS = 1000
vis = [0] * 100
lock = Lock()
def generateString(Size):
f = ""
for _ in range(0, Size):
f += chr(random.randrange(0, 26) + ord('a'))
return f
def reset(inputSize):
global commited, inp
inp = []
connect()
for _ in range(0, inputSize):
inp.append(generateString(keySize) + ":" + generateString(valueSize))
commited = 0
def find_throughput(rps):
global err_cnt, commited, throughput_analysis
err_cnt = 0
reset(25 * rps)
iterations = 0
startTime = time.time()
while time.time() - startTime < 25.0:
st = time.time()
i = 0
while i < rps:
packet = ''
while i < rps and len(packet) + len(inp[(iterations * rps + i)]) < BUF_SIZE:
packet += str(inp[(iterations * rps + i)] + '#' + str(iterations) + '$')
i += 1
try:
mySocket.sendall(packet.encode())
except socket.error:
print("ERROR IN SENDING")
pass
dif = time.time() - st
if dif < 1.0:
time.sleep(1.0 - dif)
iterations += 1
time.sleep(16.0)
def connect(IP = None):
print("new Connection")
global mySocket, change_in_leader
try:
mySocket.close()
except:
pass
mySocket = socket.socket(ADDR_FAMILY, SOCK_TYPE)
i = 0
while i < len(SERVERS_IP):
is_changed = True
targetAddr, port = SERVERS_IP[i].split(':')
port = int(port)
if IP:
targetAddr, port = IP.split(':')
port = int(port)
port += 1
IP = None
i -= 1
try:
print("Connect to", targetAddr, port)
mySocket.connect((targetAddr, port))
leader = str(mySocket.recv(BUF_SIZE).decode())
print(leader)
while leader != str(targetAddr)+":"+str(port-1):
targetAddr, port = leader.split(':')
port = int(port)
port += 1
mySocket.close()
print("closed")
try:
mySocket = socket.socket(ADDR_FAMILY, SOCK_TYPE)
mySocket.connect((targetAddr, port))
print("connected to the leader")
except socket.error:
is_changed = False
break
print("Waiting for the leader response")
leader = str(mySocket.recv(BUF_SIZE).decode())
print(leader)
if is_changed:
print("Connected")
change_in_leader = True
return
except socket.error:
print("Socket error when connecting")
continue
i += 1
connect()
def revc_respone():
global mySocket, commited, delay_analysis, error_analysis, err_cnt
mySocket.settimeout(200.0)
ret = ''
while True:
try:
ret += mySocket.recv(BUF_SIZE).decode()
# print(ret)
ind, last = ret.find('$'), 0
while ind != -1:
sep = ret.find('#', last)
err = ret[last:sep]
res = ret[sep + 1:ind]
err = int(err)
if err == -1:
connect(res)
ret = ''
elif err == 0:
res = int(res)
if res and vis[res-1] == 0:
print(throughput_analysis[int(res)])
vis[res-1] = 1
throughput_analysis[res] += 1
else:
error_analysis.append((err, res, time.time()))
err_cnt += 1
last = ind + 1
ind = ret.find('$', last)
ret = ret[last:]
except socket.timeout:
print("socket timeout in receiving from the server")
continue
except socket.error as e:
pass
def main():
global inp, conn, keySize, valueSize, iterations, throughput_analysis
if(len(sys.argv) != 5):
print("Iteration - RPS - the size of the key - the size of the value")
sys.exit(0)
iterations = int(sys.argv[1])
rps = int(sys.argv[2])
keySize = int(sys.argv[3])
valueSize = int(sys.argv[4])
print("All the data in delay_client_analysis and error_client_analysis will be delated y|n ?")
res = input()
if(res != 'y'):
sys.exit(0)
throughput_analysis = [0] * iterations
connect()
print("connected")
_thread.start_new_thread(revc_respone, ())
find_throughput(rps)
with open("throughput_client_analysis.txt", 'w+') as f:
for i in range(0, len(throughput_analysis)):
f.write(str(throughput_analysis[i]) + '\n')
with open("error_client_analysis.txt", 'w+') as f:
for i in range(0, len(error_analysis)):
f.write(str(error_analysis[i][0]) + " " + str(error_analysis[i][1]) +
" " + str(error_analysis[i][2]) + '\n')
if __name__ == '__main__':
main()