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

from getopt import getopt, GetoptError
import re
import sys, os
import subprocess

def usage():
    USAGE = """\
Usage: %(progName)s <before> <after>

Options:
  -h, --help                Show this message
"""
    print(USAGE % {'progName': sys.argv[0]})
    sys.exit(1)

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

    results = {}

    re_match = re.compile("(\S*)\s*(\S*)\s*:\s*(\S*)")
    for line in lines:
        match = re.search(re_match, line)
        if match == 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():
    try:
        option_list = [
            "help",
            ]
        options, args = getopt(sys.argv[1:], "h", option_list)
    except GetoptError:
        usage()

    for name, value in options:
        if name in ('-h', '--help'):
            usage()

    if len(args) != 2:
        usage()

    before = get_results(args[0])
    after = get_results(args[1])

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

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

        if after.get(p) != None:
            after_count = 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 after:
        if (before.get(p) == 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(after[k] - before[k]) / before[k])
    for p in hurt:
        namestr = p[0] + " " + p[1]
        print("HURT:   " + get_result_string(namestr, before[p], 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: " + change(total_before, total_after))
    print("instructions in affected programs:     " + change(affected_before, affected_after))
    print("GAINED:                                {0}".format(len(gained)))
    print("LOST:                                  {0}".format(len(lost)))
if __name__ == "__main__":
	main()