summaryrefslogtreecommitdiff
path: root/report.py
blob: 69fa9ecf6d63868983dbc3d31b8ebe87db8f8ae2 (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
#!/usr/bin/env python

import re
import argparse


def get_results(filename):
    file = open(filename, "r")
    lines = file.read().split('\n')

    results = {}

    re_match = re.compile(r"(\S*)\s*(\S*)\s*:\s*(\S*)")
    for line in lines:
        match = re.search(re_match, line)
        if match is None:
            continue

        groups = match.groups()
        count = int(groups[2])
        if count != 0:
            results[(groups[0], groups[1])] = count

    return results


def get_delta(b, a):
    if b != 0 and a != 0:
        frac = float(a) / float(b) - 1.0
        return ' ({:.2f}%)'.format(frac * 100.0)
    else:
        return ''


def change(b, a):
    return str(b) + " -> " + str(a) + get_delta(b, a)


def get_result_string(p, b, a):
    p = p + ": "
    while len(p) < 50:
        p = p + ' '
    return p + change(b, a)


def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("before", help="the output of the original code")
    parser.add_argument("after", help="the output of the new code")
    args = parser.parse_args()

    total_before = 0
    total_after = 0
    affected_before = 0
    affected_after = 0

    helped = []
    hurt = []
    lost = []
    gained = []
    for p in args.before:
        (name, type) = p
        namestr = name + " " + type
        before_count = args.before[p]

        if args.after.get(p) is not None:
            after_count = args.after[p]

            total_before += before_count
            total_after += after_count

            if before_count != after_count:
                affected_before += before_count
                affected_after += after_count

                result = get_result_string(namestr, before_count, after_count)
                if after_count > before_count:
                    hurt.append(p)
                else:
                    helped.append(result)
        else:
            lost.append(namestr)

    for p in args.after:
        if args.before.get(p) is None:
            gained.append(p[0] + " " + p[1])

    helped.sort()
    for r in helped:
        print("helped: " + r)
    if len(helped) > 0:
        print("")

    hurt.sort(
        key=lambda k: float(args.after[k] - args.before[k]) / args.before[k])
    for p in hurt:
        namestr = p[0] + " " + p[1]
        print("HURT:   " + get_result_string(
            namestr, args.before[p], args.after[p]))
    if len(hurt) > 0:
        print("")

    lost.sort()
    for p in lost:
        print("LOST:   " + p)
    if len(lost) > 0:
        print("")

    gained.sort()
    for p in gained:
        print("GAINED: " + p)
    if len(gained) > 0:
        print("")

    print("total instructions in shared programs: {0}\n"
          "instructions in affected programs:     {1}\n"
          "GAINED:                                {2}\n"
          "LOST:                                  {3}".format(
              change(total_before, total_after),
              change(affected_before, affected_after),
              len(gained),
              len(lost)))


if __name__ == "__main__":
    main()