forked from MahmudulHasn/python-energy-microscope
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecorator.py
More file actions
68 lines (59 loc) · 2.43 KB
/
Copy pathdecorator.py
File metadata and controls
68 lines (59 loc) · 2.43 KB
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
import os
import platform
import psutil
import json
import csv
from functools import wraps
from typing import Callable
from datetime import datetime
import time
def get_system_info(result_file_path: str):
"""
Get system info and include the path to the result file.
"""
return {
"CPU": platform.processor() or "Unknown",
"RAM_GB": round(psutil.virtual_memory().total / (1024 ** 3), 2),
"OS": f"{platform.system()} {platform.release()}",
"Architecture": platform.machine(),
"Test_Result_File": result_file_path
}
def measure_time_to_csv(n: int, csv_filename: str, folder_name: str = "time_benchmark"):
"""
Decorator to measure execution time, store system info in a JSON file,
and store execution time results in a CSV file.
"""
def decorator(func: Callable):
@wraps(func)
def wrapper(*args, **kwargs):
# Create the directory if it doesn't exist
os.makedirs(folder_name, exist_ok=True)
# Create file paths
result_file_path = os.path.join(folder_name, f"{csv_filename}.csv")
system_info_path = os.path.join(folder_name, "system_info.json")
# Write system info to JSON if the file does not exist
if not os.path.isfile(system_info_path):
system_info = get_system_info(result_file_path)
with open(system_info_path, "w") as json_file:
json.dump(system_info, json_file, indent=4)
# Open the result CSV file and write the data
with open(result_file_path, mode='a', newline='') as result_file:
writer = csv.writer(result_file)
# If file doesn't exist, write the header
if result_file.tell() == 0:
writer.writerow(['timestamp', 'function', 'run', 'execution_time (s)'])
# Run the function n times and log execution time
for i in range(1, n + 1):
start_time = time.time()
result = func(*args, **kwargs)
end_time = time.time()
execution_time = end_time - start_time
writer.writerow([
datetime.now().isoformat(),
func.__name__,
i,
execution_time
])
return result
return wrapper
return decorator