-
Notifications
You must be signed in to change notification settings - Fork 0
/
spit.py
executable file
·66 lines (57 loc) · 1.89 KB
/
spit.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
#!/usr/bin/env python3
import argparse
import json
import random
import string
def randdata(fuel):
if fuel < 0:
return None
t = random.choice([dict,list,int,float,str,None])
l = random.randint(0, fuel*4)
s = lambda: ''.join(random.choice(string.ascii_letters) for _ in range(l))
if t is dict:
d = dict()
for n in range(l):
fuel -= 1
d[s()] = randdata(fuel)
return d
elif t is list:
d = list()
for n in range(l):
fuel -= 1
d.append(randdata(fuel))
return d
elif t is int:
return l
elif t is float:
return random.random()
elif t is str:
return s()
else:
return None
get = lambda addr,key,val: f'''curl -v -s --write-out '\\n' {addr}/kv/{key}'''
delete = lambda addr,key,val: f'''curl -v -s -X DELETE {addr}/kv/{key}'''
put = lambda addr,key,val: f'''curl -v -s -X PUT -H "Content-type: application/json" -d '{json.dumps(val)}' {addr}/kv/{key}'''
if __name__ == '__main__':
ap = argparse.ArgumentParser()
ap.add_argument('addr')
ap.add_argument('--verbose', action='store_true')
ap.add_argument('--mut', action='store_true')
ns = ap.parse_args()
print('#!/usr/bin/env bash')
print('set -x')
request_count = 10_000
test_length_sec = 8 * 60
requests_per_sec = request_count / test_length_sec
sec_per_request = 1 / requests_per_sec
print('request count:', request_count)
print('test length sec:', test_length_sec)
print('RPS:', requests_per_sec)
print('request delay:', sec_per_request)
for n in range(request_count):
req = random.choice([get,delete,put]) if ns.mut else get
cmd = req(ns.addr,random.choice(string.ascii_lowercase),randdata(5))
if not ns.verbose:
cmd += ' 2> /dev/null'
print(cmd)
print(f'sleep {sec_per_request:10.10f}')