summaryrefslogtreecommitdiff
path: root/programs/report.py
blob: d5e4425ae728f3f76f6547e89df3a2a9e7e9ec95 (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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#!/usr/bin/env python3
#
# Copyright © 2012 Intel Corporation
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice (including the next
# paragraph) shall be included in all copies or substantial portions of the
# Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.
#

from argparse import ArgumentParser
import os
import os.path as path
import re
import sys

from framework.database import ResultDatabase

def readfile(filename):
    with open(filename) as f:
        return f.read()

def writefile(filename, text):
    with open(filename, "w") as f:
        f.write(text)

templateDir = path.join(path.dirname(path.realpath(__file__)), 'templates')
templates = {
    'index': readfile(path.join(templateDir, 'index.html'))
}

#############################################################################
##### Vector indicating the number of subtests that have passed/failed/etc.
#############################################################################
class PassVector:
    def __init__(self, p, f, s, c, t, h):
        self.passnr    = p
        self.failnr    = f
        self.skipnr    = s
        self.crashnr   = c
        self.timeoutnr = t
        self.hangnr    = h

    def add(self, o):
        self.passnr    += o.passnr
        self.failnr    += o.failnr
        self.skipnr    += o.skipnr
        self.crashnr   += o.crashnr
        self.timeoutnr += o.timeoutnr
        self.hangnr    += o.hangnr

    # Do not count skips
    def totalRun(self):
        return self.passnr + self.failnr + self.crashnr + self.timeoutnr + self.hangnr

def toPassVector(status):
    vectormap = {
        'pass':    PassVector(1,0,0,0,0,0),
        'fail':    PassVector(0,1,0,0,0,0),
        'skip':    PassVector(0,0,1,0,0,0),
        'crash':   PassVector(0,0,0,1,0,0),
        'timeout': PassVector(0,0,0,0,1,0),
        'hang':    PassVector(0,0,0,0,0,1)
    }
    return vectormap[status]

#############################################################################
##### Helper functions
#############################################################################

filename_char_re = re.compile(r'[^a-zA-Z0-9_]+')
def escape(s):
        return filename_char_re.sub('', s.replace('/', '__'))

#############################################################################
##### Summary page generation
#############################################################################

def testResult(run_name, full_name, status):
    html = '<a class="%(status)s" href="%(link)s">%(status)s</a>' % {
        'status': status,
        'link': path.join(run_name, escape(full_name) + '.html')
    }
    return html

class StackEntry:
    def __init__(self, num_runs, group_name):
        self.name = group_name
        self.results = [PassVector(0,0,0,0,0,0) for i in range(num_runs)]
        self.name_html = ''
        self.column_html = ['' for i in range(num_runs)]

def buildGroupResultHeader(p):
    if p.hangnr > 0:
        status = 'hang'
    elif p.timeoutnr > 0:
        status = 'timeout'
    elif p.crashnr > 0:
        status = 'crash'
    elif p.failnr > 0:
        status = 'fail'
    elif p.passnr > 0:
        status = 'pass'
    else:
        status = 'skip'

    totalnr = p.totalRun()
    passnr = p.passnr

    return '<div class="head %(status)s">%(pass)d/%(total)d</div>' % {
           'status': status, 'total': p.totalRun(), 'pass': p.passnr }

def buildTable(run_names, results):
    # If the test list is empty, just return now.
    if not results:
        return ('', [''])

    num_runs = len(run_names)

    last_group = ''
    stack = []

    def openGroup(name):
        stack.append(StackEntry(num_runs, name))

    def closeGroup():
        group = stack.pop()

        stack[-1].name_html += ''.join(['<div class="group"><div class="head">', group.name, '</div><div class="groupbody">', group.name_html, '</div></div>'])

        for i in range(num_runs):
            stack[-1].results[i].add(group.results[i])
            stack[-1].column_html[i] += ''.join(['<div class="group">', buildGroupResultHeader(group.results[i]), group.column_html[i], '</div>'])

    openGroup('fake')
    openGroup('All')

    for full_test in sorted(results.keys()):
        group, test = path.split(full_test) # or full_test.rpartition('/')

        if group != last_group:
            # We're in a different group now.  Close the old ones
            # and open the new ones.
            for x in path.relpath(group, last_group).split('/'):
                if x == '..':
                    closeGroup()
                else:
                    openGroup(x)

            last_group = group

        # Add the current test
        stack[-1].name_html += '<div>' + test + '</div>\n';
        for i in range(num_runs):
            passv = toPassVector(results[full_test][i])
            html = testResult(run_names[i], full_test, results[full_test][i])
            stack[-1].results[i].add(passv)
            stack[-1].column_html[i] += html

    # Close any remaining groups
    while len(stack) > 1:
        closeGroup()

    assert(len(stack) == 1)

    return (stack[0].name_html, stack[0].column_html)

def writeSummaryHtml(run_names, results, reportDir):
    names, columns = buildTable(run_names, results)

    def makeColumn(name, contents):
        return ''.join(['<div class="resultColumn"><a class="title" href="%s/index.html">%s</a>' % (escape(name), name), contents, '</div>'])

    column_html = ''.join([makeColumn(name, contents) for name, contents in zip(run_names, columns)])
    group = '<div class="nameColumn"><a class="title" href="%s/index.html">%(name)s</a>' + names + '</div>'
    writefile(path.join(reportDir, 'index.html'), templates['index'] % {
        'page': 'Your face',
        'showlinks': 'Navbar',
        'group': group,
        'columns': column_html
    })

#############################################################################
##### Main program
#############################################################################

def parseArguments(argv, config):
    p = ArgumentParser(prog='robyn report', description='A GPU test runner')
    p.add_argument('-o', '--output', default='summary',
                   metavar='<directory to write HTML reports to>')
    p.add_argument('runs', nargs='+', metavar='<run name>')

    # XXX: alternate database (pending refactoring)
    return p.parse_args(argv)

def main(argv, config):
    args = parseArguments(argv, config)

    db = ResultDatabase(config)

    reportDir = args.output
    if not path.exists(reportDir):
        os.makedirs(reportDir)

    run_names = list(args.runs)
    results = db.getResults(run_names)

    #print(results)

    # XXX: write detail pages

    os.link(path.join(templateDir, 'index.css'),
            path.join(reportDir, 'index.css'))
    writeSummaryHtml(run_names, results, reportDir)


if __name__ == "__main__":
    main()