summary.py 9.7 KB
Newer Older
1 2
#!/usr/bin/env python

3
import testlog_parser, sys, os, xml, glob, re
4 5 6
from table_formatter import *
from optparse import OptionParser

7 8 9 10 11 12 13
numeric_re = re.compile("(\d+)")
cvtype_re = re.compile("(8U|8S|16U|16S|32S|32F|64F)C(\d{1,3})")
cvtypes = { '8U': 0, '8S': 1, '16U': 2, '16S': 3, '32S': 4, '32F': 5, '64F': 6 }

convert = lambda text: int(text) if text.isdigit() else text
keyselector = lambda a: cvtype_re.sub(lambda match: " " + str(cvtypes.get(match.group(1), 7) + (int(match.group(2))-1) * 8) + " ", a)
alphanum_keyselector = lambda key: [ convert(c) for c in numeric_re.split(keyselector(key)) ]
14

15
def getSetName(tset, idx, columns, short = True):
A
Andrey Kamaev 已提交
16
    if columns and len(columns) > idx:
17 18 19 20 21 22 23
        prefix = columns[idx]
    else:
        prefix = None
    if short and prefix:
        return prefix
    name = tset[0].replace(".xml","").replace("_", "\n")
    if prefix:
24
        return prefix + "\n" + ("-"*int(len(max(prefix.split("\n"), key=len))*1.5)) + "\n" + name
25 26
    return name

27 28 29 30
if __name__ == "__main__":
    if len(sys.argv) < 2:
        print >> sys.stderr, "Usage:\n", os.path.basename(sys.argv[0]), "<log_name1>.xml [<log_name2>.xml ...]"
        exit(0)
31

32 33 34 35 36
    parser = OptionParser()
    parser.add_option("-o", "--output", dest="format", help="output results in text format (can be 'txt', 'html' or 'auto' - default)", metavar="FMT", default="auto")
    parser.add_option("-m", "--metric", dest="metric", help="output metric", metavar="NAME", default="gmean")
    parser.add_option("-u", "--units", dest="units", help="units for output values (s, ms (default), mks, ns or ticks)", metavar="UNITS", default="ms")
    parser.add_option("-f", "--filter", dest="filter", help="regex to filter tests", metavar="REGEX", default=None)
37 38
    parser.add_option("", "--module", dest="module", default=None, metavar="NAME", help="module prefix for test names")
    parser.add_option("", "--columns", dest="columns", default=None, metavar="NAMES", help="comma-separated list of column aliases")
39
    parser.add_option("", "--no-relatives", action="store_false", dest="calc_relatives", default=True, help="do not output relative values")
40
    parser.add_option("", "--with-cycles-reduction", action="store_true", dest="calc_cr", default=False, help="alos output cycle reduction percentages")
41
    parser.add_option("", "--show-all", action="store_true", dest="showall", default=False, help="also include empty and \"notrun\" lines")
42 43
    parser.add_option("", "--match", dest="match", default=None)
    parser.add_option("", "--match-replace", dest="match_replace", default="")
44
    parser.add_option("", "--regressions-only", dest="regressionsOnly", default=None, metavar="X-FACTOR", help="show only tests with performance regressions not")
45
    (options, args) = parser.parse_args()
46

47 48 49
    options.generateHtml = detectHtmlOutputType(options.format)
    if options.metric not in metrix_table:
        options.metric = "gmean"
50
    if options.metric.endswith("%") or options.metric.endswith("$"):
51
        options.calc_relatives = False
52 53
        options.calc_cr = False
    if options.columns:
54
        options.columns = [s.strip().replace("\\n", "\n") for s in options.columns.split(",")]
55

56
    # expand wildcards and filter duplicates
57 58
    files = []
    seen = set()
59 60
    for arg in args:
        if ("*" in arg) or ("?" in arg):
61 62 63
            flist = [os.path.abspath(f) for f in glob.glob(arg)]
            flist = sorted(flist, key= lambda text: str(text).replace("M", "_"))
            files.extend([ x for x in flist if x not in seen and not seen.add(x)])
64
        else:
65
            fname = os.path.abspath(arg)
A
Andrey Kamaev 已提交
66
            if fname not in seen and not seen.add(fname):
67
                files.append(fname)
68

69 70 71 72 73 74 75
    # read all passed files
    test_sets = []
    for arg in files:
        try:
            tests = testlog_parser.parseLogFile(arg)
            if options.filter:
                expr = re.compile(options.filter)
76 77 78
                tests = [t for t in tests if expr.search(str(t))]
            if options.match:
                tests = [t for t in tests if t.get("status") != "notrun"]
79 80 81 82 83 84
            if tests:
                test_sets.append((os.path.basename(arg), tests))
        except IOError as err:
            sys.stderr.write("IOError reading \"" + arg + "\" - " + str(err) + os.linesep)
        except xml.parsers.expat.ExpatError as err:
            sys.stderr.write("ExpatError reading \"" + arg + "\" - " + str(err) + os.linesep)
85

86 87 88
    if not test_sets:
        sys.stderr.write("Error: no test data found" + os.linesep)
        quit()
89

90 91 92
    # find matches
    setsCount = len(test_sets)
    test_cases = {}
93

94 95 96 97
    name_extractor = lambda name: str(name)
    if options.match:
        reg = re.compile(options.match)
        name_extractor = lambda name: reg.sub(options.match_replace, str(name))
98

99 100
    for i in range(setsCount):
        for case in test_sets[i][1]:
101
            name = name_extractor(case)
102 103
            if options.module:
                name = options.module + "::" + name
104 105 106
            if name not in test_cases:
                test_cases[name] = [None] * setsCount
            test_cases[name][i] = case
107

108 109 110 111
    # build table
    getter = metrix_table[options.metric][1]
    if options.calc_relatives:
        getter_p = metrix_table[options.metric + "%"][1]
112 113
    if options.calc_cr:
        getter_cr = metrix_table[options.metric + "$"][1]
114
    tbl = table(metrix_table[options.metric][0])
115

116
    # header
117
    tbl.newColumn("name", "Name of Test", align = "left", cssclass = "col_name")
118 119
    i = 0
    for set in test_sets:
120
        tbl.newColumn(str(i), getSetName(set, i, options.columns, False), align = "center")
121
        i += 1
122 123 124 125 126 127
    metric_sets = test_sets[1:]
    if options.calc_cr:
        i = 1
        for set in metric_sets:
            tbl.newColumn(str(i) + "$", getSetName(set, i, options.columns) + "\nvs\n" + getSetName(test_sets[0], 0, options.columns) + "\n(cycles reduction)", align = "center", cssclass = "col_cr")
            i += 1
128 129
    if options.calc_relatives:
        i = 1
130 131
        for set in metric_sets:
            tbl.newColumn(str(i) + "%", getSetName(set, i, options.columns) + "\nvs\n" + getSetName(test_sets[0], 0, options.columns) + "\n(x-factor)", align = "center", cssclass = "col_rel")
132
            i += 1
133

134
    # rows
135
    prevGroupName = None
136
    needNewRow = True
137
    lastRow = None
138
    for name in sorted(test_cases.iterkeys(), key=alphanum_keyselector):
139 140
        cases = test_cases[name]
        if needNewRow:
141
            lastRow = tbl.newRow()
142 143 144
            if not options.showall:
                needNewRow = False
        tbl.newCell("name", name)
145

146 147 148 149 150 151 152
        groupName = next(c for c in cases if c).shortName()
        if groupName != prevGroupName:
            prop = lastRow.props.get("cssclass", "")
            if "firstingroup" not in prop:
                lastRow.props["cssclass"] = prop + " firstingroup"
            prevGroupName = groupName

153 154 155 156 157 158
        for i in range(setsCount):
            case = cases[i]
            if case is None:
                tbl.newCell(str(i), "-")
                if options.calc_relatives and i > 0:
                    tbl.newCell(str(i) + "%", "-")
159 160
                if options.calc_cr and i > 0:
                    tbl.newCell(str(i) + "$", "-")
161 162 163 164 165 166 167 168
            else:
                status = case.get("status")
                if status != "run":
                    tbl.newCell(str(i), status, color = "red")
                    if status != "notrun":
                        needNewRow = True
                    if options.calc_relatives and i > 0:
                        tbl.newCell(str(i) + "%", "-", color = "red")
169 170
                    if options.calc_cr and i > 0:
                        tbl.newCell(str(i) + "$", "-", color = "red")
171 172 173 174 175 176
                else:
                    val = getter(case, cases[0], options.units)
                    if options.calc_relatives and i > 0 and val:
                        valp = getter_p(case, cases[0], options.units)
                    else:
                        valp = None
177 178 179 180
                    if options.calc_cr and i > 0 and val:
                        valcr = getter_cr(case, cases[0], options.units)
                    else:
                        valcr = None
181 182 183 184
                    if not valp or i == 0:
                        color = None
                    elif valp > 1.05:
                        color = "green"
185 186
                    elif valp < 0.95:
                        color = "red"
187 188 189 190
                    else:
                        color = None
                    if val:
                        needNewRow = True
191
                    tbl.newCell(str(i), formatValue(val, options.metric, options.units), val, color = color)
192
                    if options.calc_relatives and i > 0:
193 194 195
                        tbl.newCell(str(i) + "%", formatValue(valp, "%"), valp, color = color, bold = color)
                    if options.calc_cr and i > 0:
                        tbl.newCell(str(i) + "$", formatValue(valcr, "$"), valcr, color = color, bold = color)
196 197 198
    if not needNewRow:
        tbl.trimLastRow()

199 200 201 202 203 204 205 206 207 208 209 210
    if options.regressionsOnly:
        for r in reversed(range(len(tbl.rows))):
            delete = True
            i = 1
            for set in metric_sets:
                val = tbl.rows[r].cells[len(tbl.rows[r].cells)-i].value
                if val is not None and val < float(options.regressionsOnly):
                    delete = False
                i += 1
            if (delete):
                tbl.rows.pop(r)

211 212
    # output table
    if options.generateHtml:
213 214 215 216 217 218
        if options.format == "moinwiki":
            tbl.htmlPrintTable(sys.stdout, True)
        else:
            htmlPrintHeader(sys.stdout, "Summary report for %s tests from %s test logs" % (len(test_cases), setsCount))
            tbl.htmlPrintTable(sys.stdout)
            htmlPrintFooter(sys.stdout)
219 220
    else:
        tbl.consolePrintTable(sys.stdout)
221 222 223

    if options.regressionsOnly:
        sys.exit(len(tbl.rows))