Imported Upstream version 1.72.0
[platform/upstream/boost.git] / libs / histogram / benchmark / plot_benchmarks.py
1 #!/usr/bin/env python3
2
3 # Copyright Hans Dembinski 2019
4 # Distributed under the Boost Software License, Version 1.0.
5 # See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt
6
7 from matplotlib import pyplot as plt, lines
8 import shelve
9 import json
10 import subprocess as subp
11 import sys
12 from collections import defaultdict
13 from run_benchmarks import get_commits, run
14 import numpy as np
15 import threading
16
17 thread = None
18 current_index = 0
19
20 commits, comments = get_commits()
21
22 def get_benchmarks(results):
23     benchmarks = defaultdict(lambda: [])
24     for hash in commits:
25         if hash in results and results[hash] is not None:
26             benchs = results[hash]
27             for b in benchs["benchmarks"]:
28                 name = b["name"]
29                 time = min(b["cpu_time"], b["real_time"])
30                 benchmarks[name].append((commits.index(hash), time))
31     return benchmarks
32
33 with shelve.open("benchmark_results") as results:
34     benchmarks = get_benchmarks(results)
35
36 fig, ax = plt.subplots(4, 1, figsize=(10, 10), sharex=True)
37 plt.subplots_adjust(hspace=0, top=0.98, bottom=0.05, right=0.96)
38
39 plt.sca(ax[0])
40 for name, xy in benchmarks.items():
41     if "uniform" in name: continue
42     if "_1d" in name:
43         x, y = np.transpose(xy)
44         plt.plot(x, y, ".-", label=name)
45 plt.legend(fontsize="xx-small")
46
47 plt.sca(ax[1])
48 for name, xy in benchmarks.items():
49     if "uniform" in name: continue
50     if "_2d" in name:
51         x, y = np.transpose(xy)
52         plt.plot(x, y, ".-", label=name)
53 plt.legend(fontsize="xx-small")
54
55 plt.sca(ax[2])
56 for name, xy in benchmarks.items():
57     if "uniform" in name: continue
58     if "_3d" in name:
59         x, y = np.transpose(xy)
60         plt.plot(x, y, ".-", label=name)
61 plt.legend(fontsize="xx-small")
62
63 plt.sca(ax[3])
64 for name, xy in benchmarks.items():
65     if "uniform" in name: continue
66     if "_6d" in name:
67         x, y = np.transpose(xy)
68         plt.plot(x, y, ".-", label=name)
69 plt.legend(fontsize="xx-small")
70
71 plt.figtext(0.01, 0.5, "time per loop / ns [smaller is better]", rotation=90, va="center")
72
73 def format_coord(x, y):
74     global current_index
75     current_index = max(0, min(int(x + 0.5), len(commits) - 1))
76     hash = commits[current_index]
77     comment = comments[hash]
78     return f"{hash} {comment}"
79
80 for axi in ax.flatten():
81     axi.format_coord = format_coord
82
83 def on_key_press(event):
84     global thread
85     if thread and thread.is_alive(): return
86
87     if event.key != "u": return
88
89     hash = commits[current_index]
90
91     def worker(fig, ax, hash):
92         with shelve.open("benchmark_results") as results:
93             run(results, comments, hash, True)
94             benchmarks = get_benchmarks(results)
95
96         for name in benchmarks:
97             xy = benchmarks[name]
98             x, y = np.transpose(xy)
99             for axi in ax.flatten():
100                 for artist in axi.get_children():
101                     if isinstance(artist, lines.Line2D) and artist.get_label() == name:
102                         artist.set_xdata(x)
103                         artist.set_ydata(y)
104
105         fig.canvas.draw()
106
107     thread = threading.Thread(target=worker, args=(fig, ax, hash))
108     thread.start()
109
110 fig.canvas.mpl_connect('key_press_event', on_key_press)
111
112 plt.show()