Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 8abe5e0

Browse files
LebedevRIdmah42
authored andcommittedJun 3, 2021
compare.py: sort the results (#1168)
Currently, the tooling just keeps the whatever benchmark order that was present, and this is fine nowadays, but once the benchmarks will be optionally run interleaved, that will be rather suboptimal. So, now that i have introduced family index and per-family instance index, we can define an order for the benchmarks, and sort them accordingly. There is a caveat with aggregates, we assume that they are in-order, and hopefully we won't mess that order up..
1 parent 64a1a02 commit 8abe5e0

File tree

4 files changed

+164
-5
lines changed

4 files changed

+164
-5
lines changed
 

‎tools/compare.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -238,10 +238,10 @@ def main():
238238
options_contender = ['--benchmark_filter=%s' % filter_contender]
239239

240240
# Run the benchmarks and report the results
241-
json1 = json1_orig = gbench.util.run_or_load_benchmark(
242-
test_baseline, benchmark_options + options_baseline)
243-
json2 = json2_orig = gbench.util.run_or_load_benchmark(
244-
test_contender, benchmark_options + options_contender)
241+
json1 = json1_orig = gbench.util.sort_benchmark_results(gbench.util.run_or_load_benchmark(
242+
test_baseline, benchmark_options + options_baseline))
243+
json2 = json2_orig = gbench.util.sort_benchmark_results(gbench.util.run_or_load_benchmark(
244+
test_contender, benchmark_options + options_contender))
245245

246246
# Now, filter the benchmarks so that the difference report can work
247247
if filter_baseline and filter_contender:

‎tools/gbench/Inputs/test4_run.json

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
{
2+
"benchmarks": [
3+
{
4+
"name": "99 family 0 instance 0 repetition 0",
5+
"run_type": "iteration",
6+
"family_index": 0,
7+
"per_family_instance_index": 0,
8+
"repetition_index": 0
9+
},
10+
{
11+
"name": "98 family 0 instance 0 repetition 1",
12+
"run_type": "iteration",
13+
"family_index": 0,
14+
"per_family_instance_index": 0,
15+
"repetition_index": 1
16+
},
17+
{
18+
"name": "97 family 0 instance 0 aggregate",
19+
"run_type": "aggregate",
20+
"family_index": 0,
21+
"per_family_instance_index": 0,
22+
"aggregate_name": "9 aggregate"
23+
},
24+
25+
26+
{
27+
"name": "96 family 0 instance 1 repetition 0",
28+
"run_type": "iteration",
29+
"family_index": 0,
30+
"per_family_instance_index": 1,
31+
"repetition_index": 0
32+
},
33+
{
34+
"name": "95 family 0 instance 1 repetition 1",
35+
"run_type": "iteration",
36+
"family_index": 0,
37+
"per_family_instance_index": 1,
38+
"repetition_index": 1
39+
},
40+
{
41+
"name": "94 family 0 instance 1 aggregate",
42+
"run_type": "aggregate",
43+
"family_index": 0,
44+
"per_family_instance_index": 1,
45+
"aggregate_name": "9 aggregate"
46+
},
47+
48+
49+
50+
51+
{
52+
"name": "93 family 1 instance 0 repetition 0",
53+
"run_type": "iteration",
54+
"family_index": 1,
55+
"per_family_instance_index": 0,
56+
"repetition_index": 0
57+
},
58+
{
59+
"name": "92 family 1 instance 0 repetition 1",
60+
"run_type": "iteration",
61+
"family_index": 1,
62+
"per_family_instance_index": 0,
63+
"repetition_index": 1
64+
},
65+
{
66+
"name": "91 family 1 instance 0 aggregate",
67+
"run_type": "aggregate",
68+
"family_index": 1,
69+
"per_family_instance_index": 0,
70+
"aggregate_name": "9 aggregate"
71+
},
72+
73+
74+
{
75+
"name": "90 family 1 instance 1 repetition 0",
76+
"run_type": "iteration",
77+
"family_index": 1,
78+
"per_family_instance_index": 1,
79+
"repetition_index": 0
80+
},
81+
{
82+
"name": "89 family 1 instance 1 repetition 1",
83+
"run_type": "iteration",
84+
"family_index": 1,
85+
"per_family_instance_index": 1,
86+
"repetition_index": 1
87+
},
88+
{
89+
"name": "88 family 1 instance 1 aggregate",
90+
"run_type": "aggregate",
91+
"family_index": 1,
92+
"per_family_instance_index": 1,
93+
"aggregate_name": "9 aggregate"
94+
}
95+
]
96+
}

‎tools/gbench/report.py

+46-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
import unittest
21
"""report.py - Utilities for reporting statistics about benchmark results
32
"""
3+
4+
import unittest
45
import os
56
import re
67
import copy
8+
import random
79

810
from scipy.stats import mannwhitneyu
911

@@ -912,6 +914,49 @@ def test_json_diff_report(self):
912914
assert_measurements(self, out, expected)
913915

914916

917+
class TestReportSorting(unittest.TestCase):
918+
@classmethod
919+
def setUpClass(cls):
920+
def load_result():
921+
import json
922+
testInputs = os.path.join(
923+
os.path.dirname(
924+
os.path.realpath(__file__)),
925+
'Inputs')
926+
testOutput = os.path.join(testInputs, 'test4_run.json')
927+
with open(testOutput, 'r') as f:
928+
json = json.load(f)
929+
return json
930+
931+
cls.json = load_result()
932+
933+
def test_json_diff_report_pretty_printing(self):
934+
import util
935+
936+
expected_names = [
937+
"99 family 0 instance 0 repetition 0",
938+
"98 family 0 instance 0 repetition 1",
939+
"97 family 0 instance 0 aggregate",
940+
"96 family 0 instance 1 repetition 0",
941+
"95 family 0 instance 1 repetition 1",
942+
"94 family 0 instance 1 aggregate",
943+
"93 family 1 instance 0 repetition 0",
944+
"92 family 1 instance 0 repetition 1",
945+
"91 family 1 instance 0 aggregate",
946+
"90 family 1 instance 1 repetition 0",
947+
"89 family 1 instance 1 repetition 1",
948+
"88 family 1 instance 1 aggregate"
949+
]
950+
951+
for n in range(len(self.json['benchmarks']) ** 2):
952+
random.shuffle(self.json['benchmarks'])
953+
sorted_benchmarks = util.sort_benchmark_results(self.json)[
954+
'benchmarks']
955+
self.assertEqual(len(expected_names), len(sorted_benchmarks))
956+
for out, expected in zip(sorted_benchmarks, expected_names):
957+
self.assertEqual(out['name'], expected)
958+
959+
915960
def assert_utest(unittest_instance, lhs, rhs):
916961
if lhs['utest']:
917962
unittest_instance.assertAlmostEqual(

‎tools/gbench/util.py

+18
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import tempfile
66
import subprocess
77
import sys
8+
import functools
89

910
# Input file type enumeration
1011
IT_Invalid = 0
@@ -119,6 +120,23 @@ def load_benchmark_results(fname):
119120
return json.load(f)
120121

121122

123+
def sort_benchmark_results(result):
124+
benchmarks = result['benchmarks']
125+
126+
# From inner key to the outer key!
127+
benchmarks = sorted(
128+
benchmarks, key=lambda benchmark: benchmark['repetition_index'] if 'repetition_index' in benchmark else -1)
129+
benchmarks = sorted(
130+
benchmarks, key=lambda benchmark: 1 if 'run_type' in benchmark and benchmark['run_type'] == "aggregate" else 0)
131+
benchmarks = sorted(
132+
benchmarks, key=lambda benchmark: benchmark['per_family_instance_index'] if 'per_family_instance_index' in benchmark else -1)
133+
benchmarks = sorted(
134+
benchmarks, key=lambda benchmark: benchmark['family_index'] if 'family_index' in benchmark else -1)
135+
136+
result['benchmarks'] = benchmarks
137+
return result
138+
139+
122140
def run_benchmark(exe_name, benchmark_flags):
123141
"""
124142
Run a benchmark specified by 'exe_name' with the specified

0 commit comments

Comments
 (0)
Please sign in to comment.