-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
401 lines (299 loc) · 9.92 KB
/
test.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
397
398
399
400
401
#!/usr/bin/python3
import re
import time
import statistics
import json
import multiprocessing
from subprocess import check_output, Popen, PIPE
from os import path, makedirs
from psutil import cpu_percent
WARMUP_INTERVAL = 1
SAMPLE_INTERVAL = 1
STRESS_TIME = 3
SAMPLE_COUNT = STRESS_TIME
TESTDIR = 'testresults/'
def clear_screen():
print('\x1b[H\x1b[2J', end='')
def read_cputemp():
return check_output(['sensors']).decode()
def read_cpuclock():
return check_output(['grep', '-i', 'MHz', '/proc/cpuinfo']).decode()
def read_cpuutil(): #returns: [0.0, 10.0, 9.1, 0.0, 9.1, 9.1, 0.0, 0.0]
return cpu_percent(interval=0.1, percpu=True)
def read_memusage():
memusage = Popen(['free', '-k'], stdout=PIPE)
return check_output(['grep', 'Mem'],
stdin=memusage.stdout).decode()
def read_gputemps():
nvidia = Popen(['nvidia-smi', '-q'], stdout=PIPE)
return check_output(['grep', 'GPU Current Temp'],
stdin=nvidia.stdout).decode()
def read_gpupower():
nvidia = Popen(['nvidia-smi', '-q'], stdout=PIPE)
return check_output(['grep', 'Power Draw'], stdin=nvidia.stdout).decode()
def read_gpuutil():
nvidia = Popen(['nvidia-smi', '-q'], stdout=PIPE)
return check_output(['grep', 'Gpu'], stdin=nvidia.stdout).decode()
def read_gpufan():
nvidia = Popen(['nvidia-smi', '-q'], stdout=PIPE)
return check_output(['grep', 'Fan'], stdin=nvidia.stdout).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)
if m: #check if regex match exists
temp = float(m.group(1))
else: #make sure something happens
temp = float(0)
yield (label, temp)
def iter_cpuclock(text):
for line in text.splitlines():
if ':' in line:
(label, tail) = line.split(':')
if tail:
print("printing label: ", label)
print("printing tail: ", tail)
else:
print("dumb")
yield (label, tail)
def iter_cpuutil(text):
corenum = 0
for i in text:
label = "Core: " + str(corenum)
tail = i
corenuma += 1
yield (label, tail)
def iter_memusage(text):
for line in text.splitlines():
if ':' in line and line.startswith('Mem:'):
(label, tail) = line.split(':')
m = re.split('\s+',tail)
if m: #check if regex match exists
temp = float(m[3])
else: #make sure something happens
temp = float(0)
yield (label, temp)
def iter_gputemps(text):
for line in text.splitlines():
if ':' in line:
(label, tail) = line.split(':')
if tail:
temp = float(m.group(1))
else:
temp = float(0)
yield (label, temp)
def iter_gpupower(text):
for line in text.splitlines():
if ':' in line:
(label, tail) = line.split(':')
if tail:
temp = float(m.group(1))
else:
temp = float(0)
yield (label, temp)
def iter_gpuutil(text):
for line in text.splitlines():
if ':' in line:
(label, tail) = line.split(':')
if tail:
temp = float(m.group(1))
else:
temp = float(0)
yield (label, temp)
def iter_gpufan(text):
for line in text.splitlines():
if ':' in line:
(label, tail) = line.split(':')
if tail:
temp = float(m.group(1))
else:
temp = float(0)
yield (label, temp)
def parse_temps(text):
return dict(iter_temps(text))
def parse_cpuclock(text):
return dict(iter_cpuclock(text))
def parse_cpuutil(text):
return dict(iter_cpuutil(text))
def parse_memusage(text):
return dict(iter_memusage(text))
def parse_gputemps(text):
return dict(iter_gputemps(text))
def parse_gpupower(text):
return dict(iter_gpupower(text))
def parse_gpuutil(text):
return dict(iter_gpuutil(text))
def parse_gpufan(text):
return dict(iter_gpufan(text))
def get_temps():
return parse_temps(read_cputemp())
def get_cpuclock():
return parse_cpuclock(read_cpuclock())
def get_cpuutil():
return parse_cpuutil(read_cpuutil())
def get_memusage():
return parse_memusage(read_memusage())
def get_gputemps():
return parse_gputemps(read_gputemps())
def get_gpupower():
return parse_gpupower(read_gpupower())
def get_gpuutil():
return parse_gpuutil(read_gpuutil())
def get_gpufan():
return parse_gpufan(read_gpufan())
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 analyze_cpuclock(raw):
combined = []
summary = {}
for (k, v) in raw.items():
print("printing v: ", v)
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_cputemp()
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 record_cpuclock(count=SAMPLE_COUNT, interval=SAMPLE_INTERVAL):
raw = dict((k, []) for k in get_cpuclock())
for i in range(count):
time.sleep(interval)
text = read_cpuclock()
clear_screen()
print(text)
print('[sample {} of {}]'.format(i + 1, count))
for (k, v) in parse_cpuclock(text).items():
raw[k].append(v)
return raw
def record_cpuutil(count=SAMPLE_COUNT, interval=SAMPLE_INTERVAL):
raw = dict((k, []) for k in get_cpuutil())
for i in range(count):
time.sleep(interval)
text = read_cpuutil()
clear_screen()
print(text)
print('[sample {} of {}]'.format(i + 1, count))
for (k, v) in parse_cpuutil(text).items():
raw[k].append(v)
return raw
def record_memusage(count=SAMPLE_COUNT, interval=SAMPLE_INTERVAL):
raw = dict((k, []) for k in get_memusage())
for i in range(count):
time.sleep(interval)
text = read_memusage()
clear_screen()
print(text)
print('[sample {} of {}]'.format(i + 1, count))
for (k, v) in parse_memusage(text).items():
raw[k].append(v)
return raw
def record_gputemps(count=SAMPLE_COUNT, interval=SAMPLE_INTERVAL):
raw = dict((k, []) for k in get_gputemps())
for i in range(count):
time.sleep(interval)
text = read_gputemps()
clear_screen()
print(text)
print('[sample {} of {}]'.format(i + 1, count))
for (k, v) in parse_gputemps(text).items():
raw[k].append(v)
return raw
def record_gpupower(count=SAMPLE_COUNT, interval=SAMPLE_INTERVAL):
raw = dict((k, []) for k in get_gpupower())
for i in range(count):
time.sleep(interval)
text = read_gpupower()
clear_screen()
print(text)
print('[sample {} of {}]'.format(i + 1, count))
for (k, v) in parse_gpupower(text).items():
raw[k].append(v)
return raw
def record_gpuutil(count=SAMPLE_COUNT, interval=SAMPLE_INTERVAL):
raw = dict((k, []) for k in get_gpuutil())
for i in range(count):
time.sleep(interval)
text = read_gpuutil()
clear_screen()
print(text)
print('[sample {} of {}]'.format(i + 1, count))
for (k, v) in parse_gpuutil(text).items():
raw[k].append(v)
return raw
def record_gpufan(count=SAMPLE_COUNT, interval=SAMPLE_INTERVAL):
raw = dict((k, []) for k in get_gpufan())
for i in range(count):
time.sleep(interval)
text = read_gpufan()
clear_screen()
print(text)
print('[sample {} of {}]'.format(i + 1, count))
for (k, v) in parse_gpufan(text).items():
raw[k].append(v)
return raw
def dump_raw(custname, raw):
if not path.exists(TESTDIR):
makedirs(TESTDIR)
filename = TESTDIR + 'temps-{}-raw.json'.format(custname)
with open(filename, 'x') as fp:
json.dump(raw, fp, indent=4, sort_keys=True)
def dump_summary(custname, summary):
if not path.exists(TESTDIR):
makedirs(TESTDIR)
filename = TESTDIR + 'temps-{}-summary.json'.format(custname)
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_cpustress():
print("cputesting")
return 0
def start_gpustress():
print("gputesting")
return 0
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_cpuclock()
dump_raw(custname, raw)
summary = analyze_temps(raw)
dump_summary(custname, summary)
print_summary(summary)
custname = input("What would you like to call this test?\n")
p = start_cpustress()
q = start_gpustress()
wait_for_warmup()
run()