-
Notifications
You must be signed in to change notification settings - Fork 0
/
measure.py
113 lines (97 loc) · 3.38 KB
/
measure.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
"""File: measure.py
This file contains code to measure the performance of the Ansible plugin.
"""
import subprocess
from collections import defaultdict
import numpy as np
import os
import pickle
import time
def test(name):
"""Test a single playbook."""
print('testing', name, end='... ', flush=True)
program = f"""- hosts: all
become: no
gather_facts: no
tasks:
- include_tasks: playbooks/measurements/{name}
"""
with open('temp.yml', 'w') as f:
f.write(program)
run = '/usr/bin/time -f "%e" sudo ansible-playbook -vvv temp.yml -i hosts'
ret = subprocess.run(run, shell=True, capture_output=True)
try:
seconds = float(ret.stderr)
except:
seconds = -1
print(ret)
print(seconds)
return seconds
def test_all(tests, n, running):
"""Test multiple playbooks."""
results = defaultdict(list)
for _ in range(n):
if running:
test('runplc.yml')
for l in tests:
results[l].append(test(name=l))
saveresults(results, n=n, running=running)
def saveresults(results, n, extra='', running=False):
"""Save the results of a test run to file."""
current_time = time.strftime("%H%M%S", time.localtime())
hostname = os.uname()[1]
runs = 'notrunning'
if running:
runs = 'running'
filename = f"measurements_{hostname}_{current_time}_{n}rounds_{runs}{extra}.p"
print('dumping to', filename, end='... ')
with open(filename, 'wb') as handle:
pickle.dump(results, handle, protocol=pickle.HIGHEST_PROTOCOL)
print('done.')
def combineresults(files):
"""Combine multiple results into one dictionary."""
combined = defaultdict(list)
leng = 0
for f in files:
with open(f, 'rb') as handle:
b = pickle.load(handle)
for key, value in b.items():
combined[key] += value
leng += len(value)
runs = 'notrunning' not in f
saveresults(combined, n=leng, extra='_combined', running=runs)
def analyse(filename, decimals=2):
"""Analyse a file with results."""
with open(filename, 'rb') as handle:
b = pickle.load(handle)
for key, value in b.items():
print(key, 'raw', value, 'μ', np.round(np.mean(value), decimals),
'σ', np.round(np.std(value), decimals))
if __name__ == '__main__':
# Make sure we have sudo
subprocess.run("sudo ls", shell=True, capture_output=True)
# Change this to the dicts to combine
# combinefiles = []
combinefiles = ['measurements_hostname_174620_15rounds_running.p']
# Change this to the dicts to analyse
analysefiles = ['measurements_hostname_174620_15rounds_running.p']
# Testrounds
N = 15
# Running PLC
RUNNING = True
if combinefiles:
combineresults(combinefiles)
else:
if RUNNING:
test('runplc.yml')
else:
y = input('Make sure nothing is running on PLC and type y')
assert y == 'y', "This is an insulting message."
test_all(['addclient.yml', 'modifyclient.yml', 'removeclient.yml',
'adduser.yml', 'modifyuser.yml', 'removeuser.yml',
'modifysettings.yml', 'resetsettings.yml',
'applyprogram.yml', 'removeprogram.yml',
'modifyhardware.yml', 'resethardware.yml',
], n=N, running=RUNNING)
for a in analysefiles:
analyse(a)