summaryrefslogtreecommitdiff
path: root/obbenchlib/htmlgen.py
blob: 778be9cdd5b97932c77da504d47855363e6d52a6 (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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
# Copyright 2015  Lars Wirzenius
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# =*= License: GPL-3+ =*=


import glob
import os

import jinja2
import markdown
import yaml

import obbenchlib


class HtmlGenerator(object):

    def __init__(self):
        self.statedir = None
        self.resultdir = None
        self.spec = None

    def generate_html(self):
        env = jinja2.Environment(
            loader=jinja2.PackageLoader('obbenchlib'),
            autoescape=lambda foo: True,
            extensions=['jinja2.ext.autoescape'])

        self.create_html_dir()
        page_classes = [
            FrontPage,
            BenchmarkPage,
            ProfileData,
            LogFile,
            CssFile,
        ]
        pages = [page_class() for page_class in page_classes]
        for page in pages:
            page.env = env
            page.spec = self.spec

        for result in self.load_results():
            for page in pages:
                for filename, data in page.collect(result):
                    self.write_file(filename, data)

        for page in pages:
            for filename, data in page.generate():
                self.write_file(filename, data)

    @property
    def htmldir(self):
        return os.path.join(self.statedir, 'html')

    def create_html_dir(self):
        if not os.path.exists(self.htmldir):
            os.mkdir(self.htmldir)

    def load_results(self):
        filenames = list(glob.glob(os.path.join(self.resultdir, '*.yaml')))
        for i, filename in enumerate(filenames):
            print 'Loading', filename, i+1, 'of', len(filenames)
            with open(filename) as f:
                yield yaml.load(f, Loader=yaml.CSafeLoader)

    def write_file(self, relative_path, text):
        filename = os.path.join(self.htmldir, relative_path)
        with open(filename, 'w') as f:
            f.write(text)


class HtmlPage(object):

    def __init__(self):
        self.env = None
        self.results = []
        self.spec = None

    def collect(self, result):
        raise NotImplementedError()

    def format_markdown(self, text):
        return markdown.markdown(text)

    def get_step_names(self, benchmark):
        return [step['obnam'] for step in benchmark['steps']]

    def generate(self):
        raise NotImplementedError()

    def render(self, template_name, variables):
        template = self.env.get_template(template_name)
        return template.render(**variables)

    def deep_copy(self, item, copy):
        if isinstance(item, dict):
            return {
                key: self.deep_copy(value, copy)
                for key, value in item.items()
            }
        elif isinstance(item, list):
            return [
                self.deep_copy(value, copy)
                for value in item
            ]
        else:
            return copy(item)

    def copy(self, thing):
        if isinstance(thing, str) and len(thing) >= 1024:
            return None
        if hasattr(thing, 'copy'):
            return thing.copy()
        return thing


class FrontPage(HtmlPage):

    def collect(self, result):
        self.results.append(self.deep_copy(result, self.copy))
        return []

    def generate(self):
        variables = {
            'description': self.format_markdown(self.spec['description']),
            'benchmark_names': [
                benchmark['name']
                for benchmark in sorted(self.spec['benchmarks'])
            ],
            'results_table': self.results_table(),
            'spec': yaml.safe_dump(
                self.spec, indent=4, default_flow_style=False)
        }
        yield 'index.html', self.render('index.j2', variables)

    def results_table(self):
        table = {}
        for result in self.results:
            key = '{commit_timestamp} {commit_id} {run_timestamp}'.format(
                **result)
            if key not in table:
                table[key] = {
                    'commit_id': result['commit_id'],
                    'commit_date': result['commit_date'],
                }
            table[key][result['benchmark_name']] = self.duration(result)

        return [table[key] for key in sorted(table.keys())]

    def duration(self, result):
        total = 0
        for step in result['steps']:
            for key in step:
                if key != 'live':
                    total += step[key].get('duration', 0)
        return total


class BenchmarkPage(HtmlPage):

    def collect(self, result):
        self.results.append(self.deep_copy(result, self.copy))
        return []

    def generate(self):
        benchmark_names = [
            benchmark['name']
            for benchmark in self.spec['benchmarks']
        ]

        for benchmark_name in benchmark_names:
            yield self.generate_benchmark_page(benchmark_name)

    def generate_benchmark_page(self, benchmark_name):
        benchmark = self.find_benchmark(benchmark_name)
        table_rows = self.table_rows(benchmark)

        variables = {
            'benchmark_name': benchmark_name,
            'description': self.format_markdown(
                benchmark.get('description', '')),
            'table_rows': table_rows,
            'step_names': self.get_step_names(benchmark),
            'spec': yaml.safe_dump(
                benchmark, indent=4, default_flow_style=False)
        }

        return (
            '{}.html'.format(benchmark_name),
            self.render('benchmark.j2', variables)
        )

    def find_benchmark(self, benchmark_name):
        for benchmark in self.spec['benchmarks']:
            if benchmark['name'] == benchmark_name:
                return benchmark
        return {}

    def table_rows(self, benchmark):
        results = self.get_results_for_benchmark(benchmark)
        step_names = self.get_step_names(benchmark)
        rows = []
        for result in results:
            rows.append(self.table_row(result, step_names))
        return sorted(rows, key=lambda row: row['commit_timestamp'])

    def get_results_for_benchmark(self, benchmark):
        return [
            result
            for result in self.results
            if result['benchmark_name'] == benchmark['name']
        ]

    def table_row(self, result, step_names):
        row = {
            'result_id': result['result_id'],
            'commit_timestamp': result['commit_timestamp'],
            'commit_date': result['commit_date'],
            'commit_id': result['commit_id'],
            'total': 0,
            'vmrss_max': 0,
            'steps': [],
        }
        for i, step in enumerate(result['steps']):
            for step_name in step_names:
                if step_name in step:
                    vmrss = step[step_name].get('vmrss', 0) / 1024 / 1024
                    row['steps'].append({
                        'filename_txt': '{}_{}.txt'.format(
                            result['result_id'], i),
                        'filename_prof': '{}_{}.prof'.format(
                            result['result_id'], i),
                        'filename_log': '{}_{}.log'.format(
                            result['result_id'], i),
                        'duration': step[step_name]['duration'],
                        'vmrss': vmrss,
                    })
                    row['total'] += row['steps'][-1]['duration']
                    row['vmrss_max'] = max(row['vmrss_max'], vmrss)
                    break
        return row


class ProfileData(HtmlPage):

    def collect(self, result):
        for i, step in enumerate(result['steps']):
            for operation in step:
                if 'profile' in step[operation]:
                    yield self.generate_profile_data(
                        result, step, i, operation)
                    yield self.generate_profile_text(
                        result, step, i, operation)

    def generate_profile_data(self, result, step, i, operation):
        filename = '{}_{}.prof'.format(result['result_id'], i)
        return filename, step[operation]['profile']

    def generate_profile_text(self, result, step, i, operation):
        filename = '{}_{}.txt'.format(result['result_id'], i)
        return filename, step[operation]['profile-text']

    def generate(self):
        return []


class LogFile(HtmlPage):

    def collect(self, result):
        for i, step in enumerate(result['steps']):
            for operation in step:
                if 'log' in step[operation]:
                    yield self.generate_log_file(
                        result, step, i, operation)

    def generate_log_file(self, result, step, i, operation):
        filename = '{}_{}.log'.format(result['result_id'], i)
        return filename, step[operation]['log']

    def generate(self):
        return []


class CssFile(object):

    def collect(self, result):
        return []

    def generate(self):
        filename = os.path.join(
            os.path.dirname(obbenchlib.__file__), 'obbench.css')
        with open(filename) as f:
            data = f.read()
        yield 'obbench.css', data