-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcpu_test.py
51 lines (46 loc) · 1.41 KB
/
cpu_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
import psutil
import threading
import math
import time
from time_efficiency import time_efficiency_decorator
import pandas as pd
cpu_utilization = []
def print_cpu_utilization():
"""
start this to print cpu utilization every 5 sec
"""
while True:
cpu_util = psutil.cpu_percent()
print("CPU percent: {0}".format(cpu_util))
cpu_utilization.append(cpu_util)
time.sleep(5)
@time_efficiency_decorator
def cpu_benchmark():
"""
floating point math to test cpu
"""
for i in range(1000):
for x in range(1, 1000):
math.pi * 2 ** x
for x in range(1, 100000):
float(x) / math.pi
for x in range(1, 10000):
math.pi / x
if __name__ == '__main__':
results = {
'Trial': [],
'Time Taken': [],
'CPU Utilization': []
}
cpu_stats_thread = threading.Thread(target=print_cpu_utilization, name='cpu_stats_thread')
cpu_stats_thread.daemon = True
cpu_stats_thread.start()
for i in range(100):
time_taken = cpu_benchmark()
results['Trial'].append(i + 1)
results['Time Taken'].append(time_taken)
cpu_util_string = ', '.join(str(o) for o in cpu_utilization)
results['CPU Utilization'].append(cpu_util_string)
cpu_utilization.clear()
results_df = pd.DataFrame.from_dict(results)
results_df.to_csv('cpu_test_results.csv', index=False)