summaryrefslogtreecommitdiff
path: root/obnam-benchmark-summary
blob: b3c408c32c66e4c2ff713830947ec684f9c8ecb0 (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
#!/usr/bin/env python
#
# Copyright 2014  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/>.


import json
import os

import cliapp


MiB = 2**20
GiB = 2**30


class ObnamBenchmarkSummary(cliapp.Application):

    columns = (
        ('version', 'version'),
        ('ef-speed', 'EF files/s'),
        ('ef-repo-size', 'EF repo (MiB)'),
        ('lf-speed', 'LF MiB/s'),
        ('lf-repo-size', 'LF repo (MiB)'),
        )

    def process_args(self, args):
        summaries = []
        for dirname in args:
            summary = self.summarise_directory(dirname)
            summaries.append(summary)
        self.show_summaries(summaries)

    def summarise_directory(self, dirname):
        filename = os.path.join(dirname, 'benchmark.json')
        with open(filename) as f:
            obj = json.load(f)

        return {
            'version':
                self.get_obnam_version(obj),
            'ef-speed':
                '%.0f' % self.get_empty_files_speed(obj),
            'ef-files':
                self.get_empty_files_count(obj),
            'ef-repo-size':
                self.format_size(self.get_empty_files_repo_size(obj), MiB),
            'lf-speed':
                self.format_size(self.get_large_file_speed(obj), MiB),
            'lf-size':
                self.format_size(self.get_large_file_size(obj), GiB),
            'lf-repo-size':
                self.format_size(self.get_large_file_repo_size(obj), MiB),
            }

    def get_obnam_version(self, obj):
        return obj['versions']['obnam-version']

    def get_empty_files_speed(self, obj):
        count = self.get_empty_files_count(obj)
        step = self.find_step(obj, 'EmptyFiles', 'initial-backup')
        return count / step['duration']

    def get_empty_files_count(self, obj):
        step = self.find_step(obj, 'EmptyFiles', 'create-live-data')
        return step['empty-files-count']

    def get_empty_files_repo_size(self, obj):
        step = self.find_step(obj, 'EmptyFiles', 'initial-backup')
        return step['repo-size']

    def get_large_file_speed(self, obj):
        file_size = self.get_large_file_size(obj)
        step = self.find_step(obj, 'SingleLargeFile', 'initial-backup')
        return file_size / step['duration']

    def get_large_file_size(self, obj):
        step = self.find_step(obj, 'SingleLargeFile', 'create-live-data')
        return step['single-large-file-size']

    def get_large_file_repo_size(self, obj):
        step = self.find_step(obj, 'SingleLargeFile', 'initial-backup')
        return step['repo-size']

    def find_step(self, obj, benchmark_name, step_name):
        for step in obj['benchmarks'][benchmark_name]['steps']:
            if step['step'] == step_name:
                return step
        raise Exception('step %s not found' % step)

    def format_size(self, size, unit):
        return '%.0f' % (size / unit)

    def show_summaries(self, summaries):
        lines = [[title for key, title in self.columns]]

        for s in summaries:
            line = [str(s[key]) for key, title in self.columns]
            lines.append(line)

        widths = self.compute_column_widths(lines)

        titles = lines[0]
        results = sorted(lines[1:])
        for line in [titles] + results:
            cells = []
            for i, cell in enumerate(line):
                cells.append('%*s' % (widths[i], cell))
            self.output.write(' | '.join(cells))
            self.output.write('\n')

    def compute_column_widths(self, lines):
        widths = []
        n = len(lines[0])
        for col in range(n):
            width = 0
            for line in lines:
                width = max(width, len(line[col]))
            widths.append(width)
        return widths


if __name__ == '__main__':
    ObnamBenchmarkSummary().run()