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/>.
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)
38 m
= re
.search(r
'Run completed in (\d+.\d+) seconds.', p
.stdout
)
39 return {'seconds': float(m
.group(1))}
41 return {'error': f
'failed to parse qemu-img output: {p.stdout}'}
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"
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']
60 args
+= ['--image-opts',
61 'driver=qcow2,file.driver=preallocate,file.file.driver=file,'
62 f
'file.file.filename={fname}']
64 args
+= ['-f', 'qcow2', fname
]
66 return qemu_img_bench(args
)
69 def auto_count_bench_func(env
, case
):
72 res
= bench_func(env
, case
)
76 if res
['seconds'] >= 1:
81 if res
['seconds'] < 5:
82 case
['count'] = round(case
['count'] * 5 / res
['seconds'])
83 res
= bench_func(env
, case
)
87 res
['iops'] = case
['count'] / res
['seconds']
91 if __name__
== '__main__':
93 print(f
'USAGE: {sys.argv[0]} <qemu-img binary> '
94 'DISK_NAME:DIR_PATH ...')
97 qemu_img
= sys
.argv
[1]
102 'qemu-img-binary': qemu_img
,
107 'qemu-img-binary': qemu_img
,
115 for disk
in sys
.argv
[2:]:
116 name
, path
= disk
.split(':')
117 aligned_cases
.append({
118 'id': f
'{name}, aligned sequential 16k',
122 unaligned_cases
.append({
123 'id': f
'{name}, unaligned sequential 64k',
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)