-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorig-record-temps.py
122 lines (90 loc) · 2.75 KB
/
orig-record-temps.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
#!/usr/bin/python3
import re
import time
import statistics
import json
import multiprocessing
from subprocess import check_output, Popen
WARMUP_INTERVAL = 60
SAMPLE_COUNT = 60 * 5
SAMPLE_INTERVAL = 1
def clear_screen():
print('\x1b[H\x1b[2J', end='')
def read_sensors():
return check_output(['sensors']).decode()
def iter_temps(text):
for line in text.splitlines():
if ':' in line and not line.startswith('Adapter:'):
(label, tail) = line.split(':')
if label in ('temp1', 'temp2'):
continue
m = re.match('\s*\+(\d+)', tail)
temp = float(m.group(1))
yield (label, temp)
def parse_temps(text):
return dict(iter_temps(text))
def get_temps():
return parse_temps(read_sensors())
def summarize_temp(items):
return {
'max': max(items),
'mean': statistics.mean(items),
'median': statistics.median(items),
'min': min(items),
'stdev': statistics.stdev(items),
}
def analyze_temps(raw):
combined = []
summary = {}
for (k, v) in raw.items():
combined.append(statistics.mean(v))
summary[k] = summarize_temp(v)
summary['_combined'] = summarize_temp(combined)
return summary
def record_temps(count=SAMPLE_COUNT, interval=SAMPLE_INTERVAL):
raw = dict((k, []) for k in get_temps())
for i in range(count):
time.sleep(interval)
text = read_sensors()
clear_screen()
print(text)
print('[sample {} of {}]'.format(i + 1, count))
for (k, v) in parse_temps(text).items():
raw[k].append(v)
return raw
def dump_raw(start_ts, raw):
filename = 'temps-{}-raw.json'.format(start_ts)
with open(filename, 'x') as fp:
json.dump(raw, fp, indent=4, sort_keys=True)
def dump_summary(start_ts, summary):
filename = 'temps-{}-summary.json'.format(start_ts)
with open(filename, 'x') as fp:
json.dump(summary, fp, indent=4, sort_keys=True)
def print_summary(summary):
clear_screen()
for key in sorted(summary):
print('{}:'.format(key))
for (k, v) in sorted(summary[key].items()):
print(' {}: {:.1f}'.format(k, v))
def start_stress():
cmd = ['stress-ng', '-c', str(multiprocessing.cpu_count())]
print(cmd)
return Popen(cmd)
def wait_for_warmup():
print('Waiting {} seconds for warmup...'.format(WARMUP_INTERVAL))
time.sleep(WARMUP_INTERVAL)
def run():
start_ts = int(time.time())
print('Start at {}'.format(start_ts))
raw = record_temps()
dump_raw(start_ts, raw)
summary = analyze_temps(raw)
dump_summary(start_ts, summary)
print_summary(summary)
p = start_stress()
try:
wait_for_warmup()
run()
finally:
p.terminate()
p.wait()