test_perf_gate.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. """Tests of scripts/perf_gate.py (round perfgate190id): python3 -m unittest discover -s tests/perf -p 'test_*.py'."""
  2. import json
  3. import os
  4. import subprocess
  5. import sys
  6. import tempfile
  7. import unittest
  8. HERE = os.path.dirname(os.path.abspath(__file__))
  9. SCRIPT = os.path.join(HERE, "..", "..", "scripts", "perf_gate.py")
  10. sys.path.insert(0, os.path.dirname(SCRIPT))
  11. import perf_gate # noqa: E402
  12. def write(d, name, text):
  13. with open(os.path.join(d, name), "w") as f:
  14. f.write(text)
  15. def bench_line(cpu, rss=100.0, instr=-1, build=10.0):
  16. return ("GATE scenario=big200 block=256 threads=0 seconds=8.00 cpu_pct=%.3f ns_per_sample=1 instr_per_sample=%d import_ms=2 "
  17. "build_ms=%.2f rebuild_ms=5 rss_mb=%.1f hash=00ff hash_stable=1\n" % (cpu, instr, build, rss))
  18. def paint_report(ms, instr=None):
  19. return ("open: 500.0 ms wall, 450.0 ms message thread CPU, working set 300 MB\nmixer, whole frame: median %.3f ms, p90 1 ms%s\nresult=OK\n"
  20. % (ms, "" if instr is None else ", wall median 1 ms, pixels 00ff, instructions median %d" % instr))
  21. class PerfGateTests(unittest.TestCase):
  22. def results(self, cpu=50.0, calib=10.0, rss=100.0, paint=2.0, instr=None):
  23. d = tempfile.mkdtemp()
  24. write(d, "calib-1-a.txt", "GATE scenario=calibrate calib_ms=%.3f\n" % calib)
  25. write(d, "bench-big200@256-1.txt", "load 1\n" + bench_line(cpu, rss))
  26. write(d, "paint-big200-1.txt", paint_report(paint, instr))
  27. return d
  28. def baseline(self, d):
  29. path = os.path.join(d, "baseline.json")
  30. perf_gate.update(d, path)
  31. return path
  32. def test_same_numbers_pass_and_the_metrics_are_normalised(self):
  33. d = self.results()
  34. b = self.baseline(d)
  35. self.assertEqual(perf_gate.compare(d, b), 0)
  36. m = json.load(open(b))["metrics"]
  37. self.assertAlmostEqual(m["bench/big200@256/cpu_norm"], 5.0)
  38. self.assertAlmostEqual(m["bench/big200@256/rss_mb"], 100.0)
  39. self.assertAlmostEqual(m["paint/big200/mixer/paint_norm"], 0.2)
  40. self.assertAlmostEqual(m["app/big200/open_rss_mb"], 300.0)
  41. self.assertNotIn("bench/big200@256/instr_per_sample", m) # (no CPU counter: -1 is not a value)
  42. def test_a_slower_machine_is_not_a_regression(self):
  43. b = self.baseline(self.results())
  44. d = self.results(cpu=80.0, calib=16.0, paint=3.2) # everything 60 % slower, the calibration too
  45. self.assertEqual(perf_gate.compare(d, b), 0)
  46. def test_a_cpu_regression_fails_and_names_the_cell(self):
  47. b = self.baseline(self.results())
  48. d = self.results(cpu=65.0) # +30 %
  49. self.assertEqual(perf_gate.compare(d, b), 1)
  50. self.assertIn("bench:big200@256", open(os.path.join(d, "retry.txt")).read())
  51. self.assertIn("FAIL", open(os.path.join(d, "summary.md")).read())
  52. def test_the_best_run_counts(self):
  53. b = self.baseline(self.results())
  54. d = self.results(cpu=65.0)
  55. write(d, "bench-big200@256-2.txt", bench_line(51.0)) # the retry: a busy moment, not a regression
  56. self.assertEqual(perf_gate.compare(d, b), 0)
  57. def test_small_absolute_changes_stay_under_the_floor(self):
  58. b = self.baseline(self.results(rss=20.0))
  59. d = self.results(rss=28.0) # +40 % but only 8 MB (floor 12 MB)
  60. self.assertEqual(perf_gate.compare(d, b), 0)
  61. def test_a_missing_metric_fails(self):
  62. b = self.baseline(self.results())
  63. d = self.results()
  64. os.remove(os.path.join(d, "paint-big200-1.txt"))
  65. self.assertEqual(perf_gate.compare(d, b), 1)
  66. self.assertIn("paint:big200", open(os.path.join(d, "retry.txt")).read())
  67. def test_a_project_left_out_is_skipped(self):
  68. d0 = self.results()
  69. write(d0, "bench-thief@256-1.txt", bench_line(50.0))
  70. b = self.baseline(d0)
  71. self.assertEqual(perf_gate.compare(self.results(), b), 0) # (no thief at all in this run: FL=0)
  72. def test_baseline_thresholds_override_the_defaults(self):
  73. b = self.baseline(self.results())
  74. base = json.load(open(b))
  75. base["thresholds"] = {"cpu_norm": [0.5, 0.0]}
  76. json.dump(base, open(b, "w"))
  77. self.assertEqual(perf_gate.compare(self.results(cpu=65.0), b), 0)
  78. def test_another_cpu_model_gets_wider_thresholds_and_its_own_numbers_once_added(self):
  79. b = self.baseline(self.results())
  80. d = self.results(cpu=65.0) # +30 %: a regression on this model...
  81. write(d, "machine.txt", "Linux x86_64, Other CPU, 4 threads, abc\n")
  82. self.assertEqual(perf_gate.compare(d, b), 0) # ...within 20 % x 1.5 on another one
  83. os.environ["PERF_GATE_ADD_MACHINE"] = "1"
  84. try:
  85. perf_gate.update(d, b)
  86. finally:
  87. del os.environ["PERF_GATE_ADD_MACHINE"]
  88. base = json.load(open(b))
  89. self.assertIn("Other CPU", base["machines"])
  90. self.assertAlmostEqual(base["metrics"]["bench/big200@256/cpu_norm"], 5.0) # (the default set stays)
  91. d2 = self.results(cpu=90.0)
  92. write(d2, "machine.txt", "Linux x86_64, Other CPU, 4 threads, def\n")
  93. self.assertEqual(perf_gate.compare(d2, b), 1) # +38 % on its own numbers
  94. def test_the_command_line(self):
  95. d = self.results()
  96. b = os.path.join(d, "b.json")
  97. self.assertEqual(subprocess.call([sys.executable, SCRIPT, "update", d, b], stdout=subprocess.DEVNULL), 0)
  98. self.assertEqual(subprocess.call([sys.executable, SCRIPT, "compare", d, b], stdout=subprocess.DEVNULL), 0)
  99. # rollup 0.1.30 step 9: the paint cells' instruction count (the stable measure)
  100. def test_paint_instructions_are_collected(self):
  101. d = self.results(instr=12345678)
  102. m = perf_gate.collect(d)["metrics"]
  103. self.assertAlmostEqual(m["paint/big200/mixer/paint_instr"], 12345678.0)
  104. self.assertAlmostEqual(m["paint/big200/mixer/paint_norm"], 0.2)
  105. def test_counted_paint_time_alone_does_not_fail(self):
  106. b = self.baseline(self.results(instr=10000000))
  107. d = self.results(paint=3.0, instr=10100000) # time +50 %, the same drawing (+1 % instructions)
  108. self.assertEqual(perf_gate.compare(d, b), 0)
  109. self.assertIn("time only", open(os.path.join(d, "summary.md")).read())
  110. def test_more_paint_instructions_fail(self):
  111. b = self.baseline(self.results(instr=10000000))
  112. d = self.results(instr=11000000) # +10 %
  113. self.assertEqual(perf_gate.compare(d, b), 1)
  114. self.assertIn("paint:big200", open(os.path.join(d, "retry.txt")).read())
  115. def test_paint_time_still_gates_without_a_counted_baseline(self):
  116. b = self.baseline(self.results()) # (a baseline without instruction counts)
  117. d = self.results(paint=3.0, instr=10000000)
  118. self.assertEqual(perf_gate.compare(d, b), 1)
  119. def test_only_named_metrics_are_added_to_a_cpu_models_baseline(self):
  120. b = self.baseline(self.results(cpu=50.0))
  121. d = self.results(cpu=70.0, instr=10000000)
  122. os.environ["PERF_GATE_ONLY_METRICS"] = "paint_instr"
  123. try:
  124. perf_gate.update(d, b)
  125. finally:
  126. del os.environ["PERF_GATE_ONLY_METRICS"]
  127. base = json.load(open(b))
  128. own = base["machines"][perf_gate.cpu_model(open(os.path.join(d, "machine.txt")).read().strip())] if os.path.exists(os.path.join(d, "machine.txt")) else None
  129. for numbers in [base["metrics"]] + ([own] if own else []):
  130. self.assertAlmostEqual(numbers["bench/big200@256/cpu_norm"], 5.0) # (kept)
  131. self.assertAlmostEqual(numbers["paint/big200/mixer/paint_instr"], 10000000.0) # (added)
  132. if __name__ == "__main__":
  133. unittest.main()