xref: /qemu/scripts/simplebench/bench-backup.py (revision af2ac8514f57fc690849369ad1d6f9d65b1e9437)
1#!/usr/bin/env python3
2#
3# Bench backup block-job
4#
5# Copyright (c) 2020 Virtuozzo International GmbH.
6#
7# This program is free software; you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation; either version 2 of the License, or
10# (at your option) any later version.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with this program.  If not, see <http://www.gnu.org/licenses/>.
19#
20
21import argparse
22import json
23
24import simplebench
25from results_to_text import results_to_text
26from bench_block_job import bench_block_copy, drv_file, drv_nbd, drv_qcow2
27
28
29def bench_func(env, case):
30    """ Handle one "cell" of benchmarking table. """
31    cmd_options = env['cmd-options'] if 'cmd-options' in env else {}
32    return bench_block_copy(env['qemu-binary'], env['cmd'],
33                            cmd_options,
34                            case['source'], case['target'])
35
36
37def bench(args):
38    test_cases = []
39
40    # paths with colon not supported, so we just split by ':'
41    dirs = dict(d.split(':') for d in args.dir)
42
43    nbd_drv = None
44    if args.nbd:
45        nbd = args.nbd.split(':')
46        host = nbd[0]
47        port = '10809' if len(nbd) == 1 else nbd[1]
48        nbd_drv = drv_nbd(host, port)
49
50    for t in args.test:
51        src, dst = t.split(':')
52
53        if src == 'nbd' and dst == 'nbd':
54            raise ValueError("Can't use 'nbd' label for both src and dst")
55
56        if (src == 'nbd' or dst == 'nbd') and not nbd_drv:
57            raise ValueError("'nbd' label used but --nbd is not given")
58
59        if src == 'nbd':
60            source = nbd_drv
61        else:
62            source = drv_file(dirs[src] + '/test-source')
63
64        if dst == 'nbd':
65            test_cases.append({'id': t, 'source': source, 'target': nbd_drv})
66            continue
67
68        fname = dirs[dst] + '/test-target'
69        if args.compressed:
70            fname += '.qcow2'
71        target = drv_file(fname)
72        if args.compressed:
73            target = drv_qcow2(target)
74        test_cases.append({'id': t, 'source': source, 'target': target})
75
76    binaries = []  # list of (<label>, <path>, [<options>])
77    for i, q in enumerate(args.env):
78        name_path = q.split(':')
79        if len(name_path) == 1:
80            label = f'q{i}'
81            path_opts = name_path[0].split(',')
82        else:
83            assert len(name_path) == 2  # paths with colon not supported
84            label = name_path[0]
85            path_opts = name_path[1].split(',')
86
87        binaries.append((label, path_opts[0], path_opts[1:]))
88
89    test_envs = []
90
91    bin_paths = {}
92    for i, q in enumerate(args.env):
93        opts = q.split(',')
94        label_path = opts[0]
95        opts = opts[1:]
96
97        if ':' in label_path:
98            # path with colon inside is not supported
99            label, path = label_path.split(':')
100            bin_paths[label] = path
101        elif label_path in bin_paths:
102            label = label_path
103            path = bin_paths[label]
104        else:
105            path = label_path
106            label = f'q{i}'
107            bin_paths[label] = path
108
109        x_perf = {}
110        is_mirror = False
111        for opt in opts:
112            if opt == 'mirror':
113                is_mirror = True
114            elif opt == 'copy-range=on':
115                x_perf['use-copy-range'] = True
116            elif opt == 'copy-range=off':
117                x_perf['use-copy-range'] = False
118            elif opt.startswith('max-workers='):
119                x_perf['max-workers'] = int(opt.split('=')[1])
120
121        backup_options = {}
122        if x_perf:
123            backup_options['x-perf'] = x_perf
124
125        if args.compressed:
126            backup_options['compress'] = True
127
128        if is_mirror:
129            assert not x_perf
130            test_envs.append({
131                    'id': f'mirror({label})',
132                    'cmd': 'blockdev-mirror',
133                    'qemu-binary': path
134                })
135        else:
136            test_envs.append({
137                'id': f'backup({label})\n' + '\n'.join(opts),
138                'cmd': 'blockdev-backup',
139                'cmd-options': backup_options,
140                'qemu-binary': path
141            })
142
143    result = simplebench.bench(bench_func, test_envs, test_cases, count=3)
144    with open('results.json', 'w') as f:
145        json.dump(result, f, indent=4)
146    print(results_to_text(result))
147
148
149class ExtendAction(argparse.Action):
150    def __call__(self, parser, namespace, values, option_string=None):
151        items = getattr(namespace, self.dest) or []
152        items.extend(values)
153        setattr(namespace, self.dest, items)
154
155
156if __name__ == '__main__':
157    p = argparse.ArgumentParser('Backup benchmark', epilog='''
158ENV format
159
160    (LABEL:PATH|LABEL|PATH)[,max-workers=N][,use-copy-range=(on|off)][,mirror]
161
162    LABEL                short name for the binary
163    PATH                 path to the binary
164    max-workers          set x-perf.max-workers of backup job
165    use-copy-range       set x-perf.use-copy-range of backup job
166    mirror               use mirror job instead of backup''',
167                                formatter_class=argparse.RawTextHelpFormatter)
168    p.add_argument('--env', nargs='+', help='''\
169Qemu binaries with labels and options, see below
170"ENV format" section''',
171                   action=ExtendAction)
172    p.add_argument('--dir', nargs='+', help='''\
173Directories, each containing "test-source" and/or
174"test-target" files, raw images to used in
175benchmarking. File path with label, like
176label:/path/to/directory''',
177                   action=ExtendAction)
178    p.add_argument('--nbd', help='''\
179host:port for remote NBD image, (or just host, for
180default port 10809). Use it in tests, label is "nbd"
181(but you cannot create test nbd:nbd).''')
182    p.add_argument('--test', nargs='+', help='''\
183Tests, in form source-dir-label:target-dir-label''',
184                   action=ExtendAction)
185    p.add_argument('--compressed', help='''\
186Use compressed backup. It automatically means
187automatically creating qcow2 target with
188lazy_refcounts for each test run''', action='store_true')
189
190    bench(p.parse_args())
191