-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbenchmark.py
84 lines (70 loc) · 2.77 KB
/
benchmark.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
# /usr/bin/env python3
# A simple scrip to benchmark
import subprocess
import time
import os
import platform
from datetime import datetime
def measure_command_time(command):
start_time = time.time()
result = subprocess.run(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
end_time = time.time()
elapsed_time = end_time - start_time
# print(f"Command Output:\n{result.stdout.decode()}")
if result.stderr:
return ("Error", f"{command} Error:\n{result.stderr.decode()}")
return ("Ok", round(elapsed_time, 4))
# Bytes -> MB
def file_size_mb(file_path):
return round(os.stat(file_path).st_size / (1024 * 1024), 4)
# Speed
def calculate_speed(file_size_MB, time_Second):
if time_Second == "Error":
return "Error"
else:
return round(file_size_MB / time_Second, 4)
def get_git_commit_hash():
try:
result = subprocess.run(
['git', 'rev-parse', '--short', 'HEAD'],
capture_output=True,
text=True,
check=True
)
return result.stdout.strip()
except subprocess.CalledProcessError as e:
print("Error running git command:", e)
return None
if __name__ == "__main__":
file_to_test = "tests/image.jpg"
commands = {
"sha256sum": "sha256sum " + file_to_test,
"ezcheck(ring)": " target/release/ezcheck calculate sha256 -f " + file_to_test
# "ezcheck(hashes)": " target/release/ezcheck calculate sha256 -f win98.iso"
}
file_size_MB = file_size_mb(file_to_test)
git_hash = get_git_commit_hash()
# It runs slowly at the first time after being compiled.
measure_command_time("target/release/ezcheck -V")
results = []
for name, command in commands.items():
r = measure_command_time(command)
if r[0] == 'Ok':
results.append([name, r[1]])
else:
print(r[1])
results.append([name, "Error"])
sorted_results = sorted(results, key=lambda x: (x[1] == 'Error', x[1]))
print("+{:-^53}+".format("BENCHMARK-RESULTS"))
print("| Test file: {: <40}|".format(file_to_test))
print("| File size: {: <40}|".format(str(file_size_MB) + " MB"))
print("| Git hash: {: <40}|".format(git_hash))
print("+-----------------+-----------------+-----------------+")
print("|{: ^17}|{: ^17}|{: ^17}|".format("Command", "Speed(MB/s)", "Time(s)"))
print("+-----------------+-----------------+-----------------+")
for r in sorted_results:
print("|{: ^17}|{: ^17}|{: ^17}|".format(r[0], calculate_speed(file_size_MB, r[1]), r[1]))
print("+-----------------+-----------------+-----------------+")
print()
print("Platform version: " + platform.version())
print("Run time: " + datetime.now().strftime("%Y-%m-%d %H:%M:%S"))