migration: simplify exec migration functions
[qemu/armbru.git] / scripts / simplebench / bench_prealloc.py
blob85f588c597ad20f37a331f34ad52fd055af0a2c3
1 #!/usr/bin/env python3
3 # Benchmark preallocate filter
5 # Copyright (c) 2020 Virtuozzo International GmbH.
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.
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.
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/>.
22 import sys
23 import os
24 import subprocess
25 import re
26 import json
28 import simplebench
29 from results_to_text import results_to_text
32 def qemu_img_bench(args):
33 p = subprocess.run(args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
34 universal_newlines=True)
36 if p.returncode == 0:
37 try:
38 m = re.search(r'Run completed in (\d+.\d+) seconds.', p.stdout)
39 return {'seconds': float(m.group(1))}
40 except Exception:
41 return {'error': f'failed to parse qemu-img output: {p.stdout}'}
42 else:
43 return {'error': f'qemu-img failed: {p.returncode}: {p.stdout}'}
46 def bench_func(env, case):
47 fname = f"{case['dir']}/prealloc-test.qcow2"
48 try:
49 os.remove(fname)
50 except OSError:
51 pass
53 subprocess.run([env['qemu-img-binary'], 'create', '-f', 'qcow2', fname,
54 '16G'], stdout=subprocess.DEVNULL,
55 stderr=subprocess.DEVNULL, check=True)
57 args = [env['qemu-img-binary'], 'bench', '-c', str(case['count']),
58 '-d', '64', '-s', case['block-size'], '-t', 'none', '-n', '-w']
59 if env['prealloc']:
60 args += ['--image-opts',
61 'driver=qcow2,file.driver=preallocate,file.file.driver=file,'
62 f'file.file.filename={fname}']
63 else:
64 args += ['-f', 'qcow2', fname]
66 return qemu_img_bench(args)
69 def auto_count_bench_func(env, case):
70 case['count'] = 100
71 while True:
72 res = bench_func(env, case)
73 if 'error' in res:
74 return res
76 if res['seconds'] >= 1:
77 break
79 case['count'] *= 10
81 if res['seconds'] < 5:
82 case['count'] = round(case['count'] * 5 / res['seconds'])
83 res = bench_func(env, case)
84 if 'error' in res:
85 return res
87 res['iops'] = case['count'] / res['seconds']
88 return res
91 if __name__ == '__main__':
92 if len(sys.argv) < 2:
93 print(f'USAGE: {sys.argv[0]} <qemu-img binary> '
94 'DISK_NAME:DIR_PATH ...')
95 exit(1)
97 qemu_img = sys.argv[1]
99 envs = [
101 'id': 'no-prealloc',
102 'qemu-img-binary': qemu_img,
103 'prealloc': False
106 'id': 'prealloc',
107 'qemu-img-binary': qemu_img,
108 'prealloc': True
112 aligned_cases = []
113 unaligned_cases = []
115 for disk in sys.argv[2:]:
116 name, path = disk.split(':')
117 aligned_cases.append({
118 'id': f'{name}, aligned sequential 16k',
119 'block-size': '16k',
120 'dir': path
122 unaligned_cases.append({
123 'id': f'{name}, unaligned sequential 64k',
124 'block-size': '16k',
125 'dir': path
128 result = simplebench.bench(auto_count_bench_func, envs,
129 aligned_cases + unaligned_cases, count=5)
130 print(results_to_text(result))
131 with open('results.json', 'w') as f:
132 json.dump(result, f, indent=4)