summaryrefslogtreecommitdiff
path: root/obbenchlib/result.py
blob: d78268054a3b033f859b591c341ce5fe5b3270e1 (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
# 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 random

import yaml


class Result(object):

    def __init__(self):
        self.benchmark_name = None
        self.run_timestamp = None
        self.commit_date = None
        self.commit_timestamp = None
        self.commit_id = None
        self._result_id = random.randint(0, 2**64-1)
        self._steps = []

    def start_step(self):
        self._steps.append({})

    def set_value(self, operation, kind, value):
        step = self._steps[-1]
        if operation not in step:
            step[operation] = {}
        step[operation][kind] = value

    def save_in_dir(self, dirname):
        o = {
            'result_id': self._result_id,
            'benchmark_name': self.benchmark_name,
            'run_timestamp': self.run_timestamp,
            'commit_date': self.commit_date,
            'commit_timestamp': self.commit_timestamp,
            'commit_id': self.commit_id,
            'steps': self._steps,
        }
        filename = os.path.join(dirname, '{}.yaml'.format(self._result_id))
        with open(filename, 'w') as f:
            yaml.dump(o, stream=f, Dumper=yaml.CSafeDumper)