xref: /qemu/scripts/simplebench/bench-backup.py (revision b355f08a)
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        elif args.qcow2_sources:
62            source = drv_qcow2(drv_file(dirs[src] + '/test-source.qcow2'))
63        else:
64            source = drv_file(dirs[src] + '/test-source')
65
66        if dst == 'nbd':
67            test_cases.append({'id': t, 'source': source, 'target': nbd_drv})
68            continue
69
70        if args.target_cache == 'both':
71            target_caches = ['direct', 'cached']
72        else:
73            target_caches = [args.target_cache]
74
75        for c in target_caches:
76            o_direct = c == 'direct'
77            fname = dirs[dst] + '/test-target'
78            if args.compressed:
79                fname += '.qcow2'
80            target = drv_file(fname, o_direct=o_direct)
81            if args.compressed:
82                target = drv_qcow2(target)
83
84            test_id = t
85            if args.target_cache == 'both':
86                test_id += f'({c})'
87
88            test_cases.append({'id': test_id, 'source': source,
89                               'target': target})
90
91    binaries = []  # list of (<label>, <path>, [<options>])
92    for i, q in enumerate(args.env):
93        name_path = q.split(':')
94        if len(name_path) == 1:
95            label = f'q{i}'
96            path_opts = name_path[0].split(',')
97        else:
98            assert len(name_path) == 2  # paths with colon not supported
99            label = name_path[0]
100            path_opts = name_path[1].split(',')
101
102        binaries.append((label, path_opts[0], path_opts[1:]))
103
104    test_envs = []
105
106    bin_paths = {}
107    for i, q in enumerate(args.env):
108        opts = q.split(',')
109        label_path = opts[0]
110        opts = opts[1:]
111
112        if ':' in label_path:
113            # path with colon inside is not supported
114            label, path = label_path.split(':')
115            bin_paths[label] = path
116        elif label_path in bin_paths:
117            label = label_path
118            path = bin_paths[label]
119        else:
120            path = label_path
121            label = f'q{i}'
122            bin_paths[label] = path
123
124        x_perf = {}
125        is_mirror = False
126        for opt in opts:
127            if opt == 'mirror':
128                is_mirror = True
129            elif opt == 'copy-range=on':
130                x_perf['use-copy-range'] = True
131            elif opt == 'copy-range=off':
132                x_perf['use-copy-range'] = False
133            elif opt.startswith('max-workers='):
134                x_perf['max-workers'] = int(opt.split('=')[1])
135
136        backup_options = {}
137        if x_perf:
138            backup_options['x-perf'] = x_perf
139
140        if args.compressed:
141            backup_options['compress'] = True
142
143        if is_mirror:
144            assert not x_perf
145            test_envs.append({
146                    'id': f'mirror({label})',
147                    'cmd': 'blockdev-mirror',
148                    'qemu-binary': path
149                })
150        else:
151            test_envs.append({
152                'id': f'backup({label})\n' + '\n'.join(opts),
153                'cmd': 'blockdev-backup',
154                'cmd-options': backup_options,
155                'qemu-binary': path
156            })
157
158    result = simplebench.bench(bench_func, test_envs, test_cases,
159                               count=args.count, initial_run=args.initial_run,
160                               drop_caches=args.drop_caches)
161    with open('results.json', 'w') as f:
162        json.dump(result, f, indent=4)
163    print(results_to_text(result))
164
165
166class ExtendAction(argparse.Action):
167    def __call__(self, parser, namespace, values, option_string=None):
168        items = getattr(namespace, self.dest) or []
169        items.extend(values)
170        setattr(namespace, self.dest, items)
171
172
173if __name__ == '__main__':
174    p = argparse.ArgumentParser('Backup benchmark', epilog='''
175ENV format
176
177    (LABEL:PATH|LABEL|PATH)[,max-workers=N][,use-copy-range=(on|off)][,mirror]
178
179    LABEL                short name for the binary
180    PATH                 path to the binary
181    max-workers          set x-perf.max-workers of backup job
182    use-copy-range       set x-perf.use-copy-range of backup job
183    mirror               use mirror job instead of backup''',
184                                formatter_class=argparse.RawTextHelpFormatter)
185    p.add_argument('--env', nargs='+', help='''\
186Qemu binaries with labels and options, see below
187"ENV format" section''',
188                   action=ExtendAction)
189    p.add_argument('--dir', nargs='+', help='''\
190Directories, each containing "test-source" and/or
191"test-target" files, raw images to used in
192benchmarking. File path with label, like
193label:/path/to/directory''',
194                   action=ExtendAction)
195    p.add_argument('--nbd', help='''\
196host:port for remote NBD image, (or just host, for
197default port 10809). Use it in tests, label is "nbd"
198(but you cannot create test nbd:nbd).''')
199    p.add_argument('--test', nargs='+', help='''\
200Tests, in form source-dir-label:target-dir-label''',
201                   action=ExtendAction)
202    p.add_argument('--compressed', help='''\
203Use compressed backup. It automatically means
204automatically creating qcow2 target with
205lazy_refcounts for each test run''', action='store_true')
206    p.add_argument('--qcow2-sources', help='''\
207Use test-source.qcow2 images as sources instead of
208test-source raw images''', action='store_true')
209    p.add_argument('--target-cache', help='''\
210Setup cache for target nodes. Options:
211   direct: default, use O_DIRECT and aio=native
212   cached: use system cache (Qemu default) and aio=threads (Qemu default)
213   both: generate two test cases for each src:dst pair''',
214                   default='direct', choices=('direct', 'cached', 'both'))
215
216    p.add_argument('--count', type=int, default=3, help='''\
217Number of test runs per table cell''')
218
219    # BooleanOptionalAction helps to support --no-initial-run option
220    p.add_argument('--initial-run', action=argparse.BooleanOptionalAction,
221                   help='''\
222Do additional initial run per cell which doesn't count in result,
223default true''')
224
225    p.add_argument('--drop-caches', action='store_true', help='''\
226Do "sync; echo 3 > /proc/sys/vm/drop_caches" before each test run''')
227
228    bench(p.parse_args())
229