summaryrefslogtreecommitdiff
path: root/obbench
blob: 2c203c55141ac8efba211840cbdae8f48fd907bd (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
#!/usr/bin/python
# 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 os
import tempfile

import cliapp
import ttystatus
import yaml

import obbenchlib


class ObnamBenchmarker(cliapp.Application):

    def add_settings(self):
        self.settings.string(
            ['state'],
            'keep state in DIR between runs',
            metavar='DIR',
            default='.')
        self.settings.string(
            ['tmpdir'],
            'use DIR for temporary files',
            metavar='DIR',
            default=tempfile.gettempdir())
        self.settings.string(
            ['publish-command'],
            'after a successful run, run COMMAND',
            metavar='COMMAND')

    def process_args(self, args):
        if self.settings['tmpdir']:
            tempfile.tempdir = self.settings['tmpdir']

        spec = self.read_benchmark_spec(args[0])
        statedir = self.create_state_directory()
        self.clone_or_update_git(statedir, spec)
        self.run_benchmarks(statedir, spec, args[1:])
        self.dump_memory_profile('before producing HTML')
        self.produce_html(statedir, spec)
        self.dump_memory_profile('after producing HTML')
        self.publish(statedir)

    def read_benchmark_spec(self, filename):
        with open(filename) as f:
            return yaml.safe_load(f)

    def create_state_directory(self):
        statedir = self.settings['state']
        if not os.path.exists(statedir):
            os.mkdir(statedir)
        return statedir

    def clone_or_update_git(self, statedir, spec):
        gitdir = self.gitdir(statedir)
        if os.path.exists(gitdir):
            cliapp.runcmd(['git', 'pull'], cwd=gitdir)
        else:
            cliapp.runcmd(
                ['git', 'clone', '-b', 'master', spec['git'],
                 gitdir])

    def gitdir(self, statedir):
        return os.path.join(statedir, 'git')

    def run_benchmarks(self, statedir, spec, refs):
        benchmarker = obbenchlib.Benchmarker()
        benchmarker.statedir = statedir
        benchmarker.gitdir = self.gitdir(statedir)
        benchmarker.resultdir = self.resultdir(statedir)
        benchmarker.spec = spec
        for ref in refs:
            benchmarker.run_benchmarks(ref)

    def resultdir(self, statedir):
        return os.path.join(statedir, 'results')

    def produce_html(self, statedir, spec):
        print 'Producing HTML'
        gen = obbenchlib.HtmlGenerator()
        gen.statedir = statedir
        gen.resultdir = self.resultdir(statedir)
        gen.gitdir = self.gitdir(statedir)
        gen.spec = spec
        gen.generate_html()

    def publish(self, statedir):
        command = self.settings['publish-command']
        if command:
            print 'Publishing using {}'.format(command)
            cliapp.runcmd(['sh', '-c', command], cwd=statedir)


if __name__ == '__main__':
    ObnamBenchmarker(version=obbenchlib.__version__).run()