aboutsummaryrefslogtreecommitdiff
path: root/tools/benchmarks/compare.py
blob: 05dfa9f8ac558c20eded8782df204931f3efd491 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#! /usr/bin/env python3

#    Copyright 2015 ARM Limited
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

import argparse
import json
import os
import sys

from collections import OrderedDict

dir_benchs = os.path.dirname(os.path.realpath(__file__))
dir_tools = os.path.join(dir_benchs, '..')
sys.path.insert(0, dir_tools)
import utils
import utils_stats

def BuildOptions():
    parser = argparse.ArgumentParser(
        description = "Compare two results of the `run.py` script.",
        # Print default values.
        formatter_class = argparse.ArgumentDefaultsHelpFormatter)
    utils.AddCommonCompareOptions(parser)
    parser.add_argument('--order-by-diff', '-o',
                        action = 'store_true', default = False,
                        help = 'Show results with bigger differences first.')
    parser.add_argument('--significant-changes', '-s',
                        action = 'store_true', default = False,
                        help = '''Only show significant changes between the two
                        sets of results.''')
    # TODO: The default threshold values below have been calibrated for Nexus9.
    #        Make sure they are relevant for other platforms.
    parser.add_argument('--significant-diff-threshold', '--sdiff',
                        metavar = 'threshold (%)',
                        type = float, default = 0.2,
                        help = '''Results with a difference above this threshold
                        (in %%) will be included in the list of significant
                        results.''')
    parser.add_argument('--significant-deviation-threshold', '--sdev',
                        metavar = 'threshold (%)',
                        type = float, default = 3.0,
                        help = '''Results with a deviation higher than this
                        threshold (in %%) will be included in the significant
                        results even if the difference threshold is not met.''')
    parser.add_argument('-f', '--filter', action = 'append',
                        help='Quoted (benchmark name) filter pattern.')
    parser.add_argument('-F', '--filter-out', action = 'append',
                        help='''Filter out the benchmarks matching this patern
                        from the results.''')
    return parser.parse_args()


# Filter out benchmarks that do not show any significant difference between the
# two set of results.
def FilterSignificantChanges(in_1, in_2, diff_threshold, dev_threshold):
    out_1 = {}
    out_2 = {}
    benchmarks = set(in_1.keys()).intersection(set(in_2.keys()))
    for bench in benchmarks:
        m1, M1, ave1, d1, dp1 = utils_stats.ComputeStats(in_1[bench])
        m2, M2, ave2, d2, dp2 = utils_stats.ComputeStats(in_2[bench])
        diff = utils_stats.GetRelativeDiff(ave1, ave2)
        if abs(diff) >= diff_threshold \
                or dp1 >= dev_threshold \
                or dp2 >= dev_threshold:
            out_1[bench] = in_1[bench]
            out_2[bench] = in_2[bench]
    return out_1, out_2


def OrderResultsByDifference(in_1, in_2):
    regressions = {}
    improvements = {}
    regressions_1 = OrderedDict({})
    regressions_2 = OrderedDict({})
    improvements_1 = OrderedDict({})
    improvements_2 = OrderedDict({})
    benchmarks = set(in_1.keys()).intersection(set(in_2.keys()))
    for bench in benchmarks:
        m1, M1, ave1, d1, dp1 = utils_stats.ComputeStats(in_1[bench])
        m2, M2, ave2, d2, dp2 = utils_stats.ComputeStats(in_2[bench])
        diff = utils_stats.GetRelativeDiff(ave1, ave2)
        if diff > 0:
            regressions[bench] = diff
        else:
            improvements[bench] = diff

    order_regressions = \
        list(bench for bench in \
             sorted(regressions, key = lambda x: -abs(regressions[x])))
    for bench in order_regressions:
        regressions_1[bench] = in_1[bench]
        regressions_2[bench] = in_2[bench]
    order_improvements = \
        list(bench for bench \
             in sorted(improvements, key = lambda x: -abs(improvements[x])))
    for bench in order_improvements:
        improvements_1[bench] = in_1[bench]
        improvements_2[bench] = in_2[bench]

    return (regressions_1, regressions_2), (improvements_1, improvements_2)


def FilterBenchmarks(benchmarks, filters, filters_out):
    # We cannot use dictionary comprehension because need to preserve the order
    # of keys.
    res = benchmarks
    if filters:
        tmp = OrderedDict({})
        for b in res:
            if utils.NameMatchesAnyFilter(b, filters):
                tmp[b] = benchmarks[b]
        res = tmp
    if filters_out:
        tmp = OrderedDict({})
        for b in res:
            if not utils.NameMatchesAnyFilter(b, filters_out):
                tmp[b] = res[b]
        res = tmp
    return res


if __name__ == "__main__":
    args = BuildOptions()
    file_1 = open(args.res_1, 'r')
    file_2 = open(args.res_2, 'r')
    res_1 = json.load(file_1, object_pairs_hook=OrderedDict)
    res_2 = json.load(file_2, object_pairs_hook=OrderedDict)
    res_1 = FilterBenchmarks(res_1, args.filter, args.filter_out)
    res_2 = FilterBenchmarks(res_2, args.filter, args.filter_out)

    if args.significant_changes:
        res_1, res_2 = \
            FilterSignificantChanges(res_1, res_2,
                                     args.significant_diff_threshold,
                                     args.significant_deviation_threshold)
    if args.order_by_diff:
        regressions, improvements = OrderResultsByDifference(res_1, res_2)
        utils_stats.PrintDiff(regressions[0], regressions[1], "REGRESSIONS")
        print("")
        utils_stats.PrintDiff(improvements[0], improvements[1], "IMPROVEMENTS")
    else:
        utils_stats.PrintDiff(res_1, res_2)

    file_1.close()
    file_2.close()