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