Debian Codegraph @

benchmarks/gen_images.py

raw
1 """
2 module to generate benchmark graphs from textul result file
3 """
4
5 from typing import Literal
6
7 import matplotlib.pyplot as plt
8 import numpy as np
9
10
11 def generate_graphs(result: str, topic: str, aspect: Literal["ram", "time"], for_doc: bool = False) -> None:
12 """genrate graphs from result file
13
14 Parameters
15 ----------
16 result : str
17 path to result file
18 topic : str
19 benchmark topic; for example "Open file" or "Save file"
20 aspect : str
21 performance indiitemsor; can be "ram" (RAM memory usage) or "time" (elapsed time)
22 for_doc : bool
23 wether the source code is used inside the documentation
24
25 """
26 with open(result) as f:
27 lines = f.readlines()
28
29 platform = "x86" if "32 bit" in lines[2] else "x64"
30
31 idx = [i for i, line in enumerate(lines) if line.startswith("==")]
32
33 table_spans = {
34 "open": [idx[1] + 1, idx[2]],
35 "save": [idx[4] + 1, idx[5]],
36 "get": [idx[7] + 1, idx[8]],
37 "convert": [idx[10] + 1, idx[11]],
38 "merge": [idx[13] + 1, idx[14]],
39 }
40
41 start, stop = table_spans[topic.lower()]
42
43 items = [l[:50].strip(" \t\r\n\0*") for l in lines[start:stop]]
44 time = np.array([int(l[50:61].strip(" \t\r\n\0*")) for l in lines[start:stop]])
45 ram = np.array([int(l[61:].strip(" \t\r\n\0*")) for l in lines[start:stop]])
46
47 if aspect == "ram":
48 array = ram
49 else:
50 array = time
51
52 y_pos = list(range(len(items)))
53
54 fig, ax = plt.subplots()
55 fig.set_size_inches(15, 3.8 / 12 * len(items) + 1.2)
56
57 asam_pos = [i for i, c in enumerate(items) if c.startswith("asam")]
58 mdfreader_pos = [i for i, c in enumerate(items) if c.startswith("mdfreader")]
59
60 ax.barh(asam_pos, array[asam_pos], color="green", ecolor="green")
61 ax.barh(mdfreader_pos, array[mdfreader_pos], color="blue", ecolor="black")
62 ax.set_yticks(y_pos)
63 ax.set_yticklabels(items)
64 ax.invert_yaxis()
65 ax.set_xlabel("Time [ms]" if aspect == "time" else "RAM [MB]")
66 if topic == "Get":
67 ax.set_title("Get all channels (36424 calls) - {}".format("time" if aspect == "time" else "ram usage"))
68 else:
69 ax.set_title("{} test file - {}".format(topic, "time" if aspect == "time" else "ram usage"))
70 ax.xaxis.grid()
71
72 fig.subplots_adjust(
73 bottom=0.72 / fig.get_figheight(),
74 top=1 - 0.48 / fig.get_figheight(),
75 left=0.4,
76 right=0.9,
77 )
78
79 if aspect == "time":
80 if topic == "Get":
81 name = f"{platform}_get_all_channels.png"
82 else:
83 name = f"{platform}_{topic.lower()}.png"
84 else:
85 if topic == "Get":
86 name = f"{platform}_get_all_channels_ram_usage.png"
87 else:
88 name = f"{platform}_{topic.lower()}_ram_usage.png"
89
90 if for_doc:
91 plt.show()
92 else:
93 plt.savefig(name, dpi=300)
94
95
96 files = (
97 r"e:\02__PythonWorkspace\asammdf\benchmarks\results\x64_asammdf_2.7.0_mdfreader_0.2.7.txt",
98 r"e:\02__PythonWorkspace\asammdf\benchmarks\results\x86_asammdf_2.7.0_mdfreader_0.2.7.txt",
99 )
100
101 for file in files:
102 for topic in ("Open", "Save", "Get", "Convert", "Merge"):
103 for aspect in ("time", "ram"):
104 generate_graphs(file, topic, aspect)