Skip to content

Commit 652df6b

Browse files
committed
Add benchmarkctl.py
benchmarkctl.py is a small python utility that allows to control the benchmarksql Flask service from the command line.
1 parent 3d577ab commit 652df6b

1 file changed

Lines changed: 136 additions & 0 deletions

File tree

benchmarkctl.py

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
#!/usr/bin/env python3
2+
3+
import requests
4+
import json
5+
import re
6+
import sys
7+
8+
def main():
9+
if len(sys.argv) < 5:
10+
usage()
11+
sys.exit(2)
12+
13+
with open(sys.argv[1], 'r') as fd:
14+
config = json.load(fd)
15+
16+
command = sys.argv[2]
17+
appnode = sys.argv[3]
18+
dbnode = sys.argv[4]
19+
20+
extra_opts = {}
21+
for opt in sys.argv[5:]:
22+
key, val = opt.split('=')
23+
extra_opts[key] = val
24+
25+
# TODO: sanity checks for all args
26+
27+
bmsql = BenchmarkSQL(config, appnode, dbnode, extra_opts)
28+
29+
if command == 'build':
30+
result = bmsql.build()
31+
print(result['current_job_type'])
32+
elif command == 'destroy':
33+
result = bmsql.destroy()
34+
print(result['current_job_type'])
35+
elif command == 'run':
36+
result = bmsql.run()
37+
print(result['current_job_type'])
38+
print(result['current_job_id'])
39+
elif command == 'cancel':
40+
result = bmsql.cancel()
41+
print(result['current_job_type'])
42+
elif command == 'status':
43+
result = bmsql.status()
44+
print(result['current_job_type'])
45+
print(result['current_job_id'])
46+
elif command == 'txsummary':
47+
result = bmsql.txsummary(sys.argv[5])
48+
print(json.dumps(result['txsummary'], indent=2))
49+
else:
50+
print("unknown command '%s'"%(command,))
51+
sys.exit(2)
52+
53+
def usage():
54+
print("""usage: benchmarkctl CONFIG.json COMMAND APPNODE DBNODE
55+
""", file = sys.stderr)
56+
57+
58+
class BenchmarkSQL:
59+
def __init__(self, config, appnode, dbnode, extra_opts = None):
60+
with open(config['properties_template'], 'r') as fd:
61+
properties = fd.read()
62+
overrides = config['properties']
63+
overrides.update(config['dbnodes'][dbnode]['properties'])
64+
overrides.update(config['appnodes'][appnode]['properties'])
65+
if extra_opts is not None:
66+
overrides.update(extra_opts)
67+
for key in overrides:
68+
properties, n = re.subn('^%s=.*$'%(key),
69+
'%s=%s'%(key, overrides[key]),
70+
properties,
71+
flags = re.MULTILINE)
72+
if n == 0:
73+
properties += "\n\n" + key + "=" + overrides[key] + "\n"
74+
75+
self.config = config
76+
self.appnode = appnode
77+
self.appconf = config['appnodes'][appnode]
78+
self.dbnode = dbnode
79+
self.dbconf = config['dbnodes'][dbnode]
80+
self.properties = properties
81+
82+
def status(self):
83+
url = self.appconf['api_url']
84+
req = {
85+
'command': 'status'
86+
}
87+
res = requests.post(url, data = {'request': json.dumps(req)})
88+
return json.loads(res.text)
89+
90+
def txsummary(self, run_id):
91+
url = self.appconf['api_url']
92+
req = {
93+
'command': 'txsummary',
94+
'run_id': int(run_id)
95+
}
96+
res = requests.post(url, data = {'request': json.dumps(req)})
97+
return json.loads(res.text)
98+
99+
def build(self):
100+
url = self.appconf['api_url']
101+
req = {
102+
'command': 'build',
103+
'properties': self.properties
104+
}
105+
res = requests.post(url, data = {'request': json.dumps(req)})
106+
return json.loads(res.text)
107+
108+
def destroy(self):
109+
url = self.appconf['api_url']
110+
req = {
111+
'command': 'destroy',
112+
'properties': self.properties
113+
}
114+
res = requests.post(url, data = {'request': json.dumps(req)})
115+
return json.loads(res.text)
116+
117+
def run(self):
118+
url = self.appconf['api_url']
119+
req = {
120+
'command': 'run',
121+
'properties': self.properties
122+
}
123+
res = requests.post(url, data = {'request': json.dumps(req)})
124+
return json.loads(res.text)
125+
126+
def cancel(self):
127+
url = self.appconf['api_url']
128+
req = {
129+
'command': 'cancel',
130+
'properties': self.properties
131+
}
132+
res = requests.post(url, data = {'request': json.dumps(req)})
133+
return json.loads(res.text)
134+
135+
if __name__ == '__main__':
136+
main()

0 commit comments

Comments
 (0)