plot_benchmarks.py 3.2 KB

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