-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtesting.py
396 lines (350 loc) · 11.1 KB
/
testing.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
import unittest
import sys
from master import *
import time
import datetime
import string
import os
bug = [0, ]
debugV = 0
def genPair(krange, vrange):
kid = randint(0, krange - 1)
val = randint(0, vrange - 1)
return kid, val
def debug(s):
global debugV
print(str(s) + " debug information " + str(debugV))
debugV = debugV + 1
def setup(numServers):
for i in range(numServers):
joinServer(i)
time.sleep(0.01)
joinClient(i+5, i)
testCase = 'long_value'
# here you can put: 'eventualConsistency' 'read-your-write' 'monotonicReads' 'performance'
# 'joinServer' 'killServer' 'printStore' 'joinClient' 'breakConnection' 'createConnection' 'stabilize' 'put' 'get'
# 'linear_topology' 'long_value'
if testCase == 'eventualConsistency':
print ('test [eventual Consistency] property ')
numServer = 5
numClient = 5
setup(numServer)
keys = list(string.ascii_lowercase)
keys = ['a', 'b','c']
err = 0
gt = [['ERR_KEY'] * len(keys) for i in range(numClient)] # ground truth
for i in xrange(1000):
for j in range(numClient):
kId, v = genPair(len(keys), 10000)
gt[j][kId] = v
# print 'put ', j, keys[kId], v
put(j+5, keys[kId], v)
get(j+5, keys[kId])
stabilize()
# after stabilize, for each key, check if the values getting from all clients are the same
for i in range(len(keys)):
values = ['None'] * numClient
for j in range(numClient):
values[j] = get(j+5, keys[i])
for k in range(numClient):
if values[k] != values[(k + 1) % numClient]:
err += 1
#print 'get: ', values[k], ', ground truth is: ', values[(k + 1) % numClient]
# try to seperate it into different graph, isolate server 0
for i in xrange(1000):
for j in range(numClient):
kId, v = genPair(len(keys), 10000)
gt[j][kId] = v
# print 'put ', j, keys[kId], v
put(j+5, keys[kId], v)
get(j+5, keys[kId])
print 'Get ', err, ' mismatches. 0 err means achieve eventually consistency'
err = 0
breakConnection(0, 1)
breakConnection(0, 2)
breakConnection(0, 3)
breakConnection(0, 4)
stabilize()
print 'since we isolate server 0, prinStore of server 0 whould be different and others are the same'
printStore(0)
printStore(1)
printStore(2)
printStore(3)
printStore(4)
createConnection(0,1)
stabilize()
print 'Now we re-connect server 0 and server 1'
# after stabilize, for each key, check if the values getting from all clients are the same
for i in range(len(keys)):
values = ['None'] * numClient
for j in range(numClient):
values[j] = get(j + 5, keys[i])
for k in range(numClient):
if values[k] != values[(k + 1) % numClient]:
err += 1
# print 'get: ', values[k], ', ground truth is: ', values[(k + 1) % numClient]
# try to seperate it into different graph, isolate server 0
for i in xrange(1000):
for j in range(numClient):
kId, v = genPair(len(keys), 10000)
gt[j][kId] = v
# print 'put ', j, keys[kId], v
put(j + 5, keys[kId], v)
get(j + 5, keys[kId])
print 'Get ', err, ' mismatches. 0 err means achieve eventually consistency'
os._exit(1)
elif testCase == 'read-your-write':
print ('test [read-your-write] property ')
setup(3)
for i in range(100):
put(5, 'x', i)
for i in range(10):
put(6, 'x', 100 + i)
breakConnection(6, 1)
createConnection(6, 0)
print(get(6, 'x')) # 99
breakConnection(5, 0)
createConnection(5, 1)
print(get(5, 'x')) # ERR_DEP
stabilize()
print(get(6, 'x')) # 99
print(get(5, 'x')) # 99
print ('we have c0 conntects to s0 and c1 connects to s1 intially and put some values to key x. Then, we switch connection and check if client could get latest write(or output ERR_DEP')
os._exit(1)
elif testCase == 'monotonicReads': # Test Monotonic Reads.
print ('test [Monotonic Reads] property ')
joinServer(4)
joinServer(3)
joinClient(5, 3)
joinClient(7, 4)
put(5, 'x', 0)
put(7, 'x', 1)
joinClient(8, 3)
print (get(8, 'x'))
breakConnection(8, 3)
createConnection(8, 4)
print (get(8, 'x'))
# time.sleep(0.5)
breakConnection(8, 4)
createConnection(8, 3)
print (get(8, 'x'), ' this value should be ERR_DEP because it switch to previous server') # get ERR_DEP
print 'done Monotonic Reads check'
os._exit(1)
elif testCase == 'performance': # test performance
setup(5)
start = time.time()
for j in range(5):
for i in range(100):
put(j+5, 'x', j * 100 + i)
end = time.time()
putTime = (end - start)
start = time.time()
stabilize()
end = time.time()
stableTime = (end - start)
start = time.time()
for j in range(5):
for i in range(100):
get(j+5, 'x')
end = time.time()
getTime = (end - start)
print '[only 1 key] put: ', putTime / 500.0, ', get: ', getTime / 500.0, ', stable: ', stableTime
totalTemp = putTime+getTime
start = time.time()
for j in range(5):
for i in range(100):
put(j+5, j * 100 + i, j * 100 + i)
end = time.time()
putTime = (end - start)
start = time.time()
stabilize()
end = time.time()
stableTime = (end - start)
start = time.time()
for j in range(5):
for i in range(100):
get(j+5, str(j * 100 + i))
end = time.time()
getTime = (end - start)
totalTemp += putTime + getTime
print'[many keys] put: ', putTime / 500.0, ', get: ', getTime / 500.0, ', stable: ', stableTime
print 'Per second perform', 2000/totalTemp/1.0, 'instructions'
os._exit(1)
elif testCase == 'joinServer': # joinServer
print (' test joinServer. perform basic commands combining with stabilize and printStore to see if we join server correctly')
# breakservers
joinServer(0)
joinServer(1)
joinServer(2)
joinServer(3)
joinServer(4)
joinClient(5, 0)
put(5, "x", 0)
createConnection(0, 1)
createConnection(5, 1) # c0 with s1
put(5, "x", 2) # put x:2 in server 1
createConnection(5, 0)
print get(5, "x") # ERR_DEP
# breakServers(0, 1)
stabilize() # do nothing
print get(5, "x") # 2
createConnection(0, 1)
stabilize()
print get(5, "x") # 2
print 'finish testing'
for i in range(5):
printStore(i)
os._exit(1)
elif testCase == 'killServer':
setup(3)
put(5, 0, 0)
killServer(0)
print get(5, 0)
createConnection(5, 0)
print get(5, 0)
print 'Finish test killServer. client 5 only connect to server0. Once server 0 got killed, we cant get any data from client 5'
os._exit(1)
elif testCase == 'killServer2':
setup(3)
put(5, 0, 0)
put(6, 0, 1)
put(5, 0, 2)
killServer(0)
print get(6, 0)
print get(5, 0)
#printStore(0)
print 'Finish test killServer. client 5 only connect to server0. Once server 0 got killed, we cant get any data from client 5'
os._exit(1)
elif testCase == 'printStore': # printStore
print ('perform basic prinStore operation')
setup(5)
for j in range(5):
for i in range(10):
put(j+5, j * 10 + i, j * 10 + i)
printStore(j)
os._exit(1)
elif testCase == 'joinClient':
print "test [joinClient] API. "
joinServer(0)
joinServer(1)
time.sleep(0.05)
joinClient(0, 0)
joinClient(1, 0)
time.sleep(0.05)
put(0, "x", 0)
print get(1, "x") # 0
put(1, "x", 1)
put(0, "x", 2)
put(0, "x", 3)
print get(1, "x") # 3
stabilize()
print get(1, "x") # 3
put(0, "x", 4)
print get(0, "x") # 4
print "finished"
os._exit(1)
elif testCase == 'breakConnection': # test breackConnection/Connection between servers
print ('test [breakConnection] API. ')
numSer = 2
setup(numSer)
for j in range(numSer):
for i in range(10):
put(j+5, 'x', j * 10 + i)
breakConnection(0, 1) #
breakConnection(0, 1) #
stabilize()
print 'We break the connection between s0 and s1. since server 0 and 1 do not connect. After stabilizing they do not have consistent view'
for i in range(numSer):
printStore(i)
createConnection(0, 1)
stabilize()
print 'We re-create connection between server 0 and 1. After stabilizing they now have consistent view'
for i in range(numSer):
printStore(i)
os._exit(1)
elif testCase == 'createConnection': # 1 server, 2 client
print ('test [createConnection] API. ')
numSer = 3
setup(numSer)
for j in range(numSer):
for i in range(10):
put(j+5, 'y', j * 10 + i)
breakConnection(0, 1) #
stabilize()
print 'since server 0 and 1 do not connect. After stabilizing they do not have consistent view'
for i in range(numSer):
printStore(i)
createConnection(0, 1)
stabilize()
print 'We re-create connection between server 0 and 1. After stabilizing they now have consistent view'
for i in range(numSer):
printStore(i)
os._exit(1)
elif testCase == 'stabilize':
numSer = 5
setup(numSer)
for j in range(numSer):
for i in range(10):
put(j+5, 'x', j * 10 + i)
print 'Before stabilize, each server has different view'
for i in range(numSer):
printStore(i)
stabilize()
print 'After stabilize, each server has same view'
for i in range(numSer):
printStore(i)
# print get(i, 'x')
os._exit(1)
elif testCase == 'put':
print 'test [put] API'
setup(2)
put(5, 'x', 0)
printStore(0)
os._exit(1)
elif testCase == 'get':
print 'test normal [get] API'
setup(2)
put(5, 'x', 0)
os._exit(1)
elif testCase == 'linear_topology':
numServer = 5
numClient = 5
print 'we first break connection between all servers and then add one-by-one'
#keys = list(string.ascii_lowercase)
keys = ['a', 'b', 'c']
# create linear topology
setup(5)
for i in range(5):
for j in range(i+1, 5):
breakConnection(i, j)
for i in xrange(1000):
for j in range(numClient):
kId, v = genPair(len(keys), 10000)
# print 'put ', j, keys[kId], v
put(j+5, keys[kId], v)
for k in range(5):
for i in range(100):
for j in range (len(keys)):
kId, v = genPair(len(keys), 10000)
put(k+5, keys[kId], v)
for j in range(5):
print '-----------After ', j, ' times stabilize---------------- '
for i in range (5):
printStore(i)
if j < 4:
createConnection(j, j+1)
start = time.time()
stabilize()
end = time.time()
print j+1, ' time: ', end - start
os._exit(1)
elif testCase == 'long_value':
v = ''
keys = list(string.ascii_lowercase)
for i in range(3500):
id = randint(0, len(keys) - 1)
v += keys[id]
setup(2)
put (5, 'x', v)
print get (5, 'x')
os._exit(1)