2
0

bench_write_req.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. #!/usr/bin/env python3
  2. #
  3. # Test to compare performance of write requests for two qemu-img binary files.
  4. #
  5. # The idea of the test comes from intention to check the benefit of c8bb23cbdbe
  6. # "qcow2: skip writing zero buffers to empty COW areas".
  7. #
  8. # Copyright (c) 2020 Virtuozzo International GmbH.
  9. #
  10. # This program is free software; you can redistribute it and/or modify
  11. # it under the terms of the GNU General Public License as published by
  12. # the Free Software Foundation; either version 2 of the License, or
  13. # (at your option) any later version.
  14. #
  15. # This program is distributed in the hope that it will be useful,
  16. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. # GNU General Public License for more details.
  19. #
  20. # You should have received a copy of the GNU General Public License
  21. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. #
  23. import sys
  24. import os
  25. import subprocess
  26. import simplebench
  27. from results_to_text import results_to_text
  28. def bench_func(env, case):
  29. """ Handle one "cell" of benchmarking table. """
  30. return bench_write_req(env['qemu_img'], env['image_name'],
  31. case['block_size'], case['block_offset'],
  32. case['cluster_size'])
  33. def qemu_img_pipe(*args):
  34. '''Run qemu-img and return its output'''
  35. subp = subprocess.Popen(list(args),
  36. stdout=subprocess.PIPE,
  37. stderr=subprocess.STDOUT,
  38. universal_newlines=True)
  39. exitcode = subp.wait()
  40. if exitcode < 0:
  41. sys.stderr.write('qemu-img received signal %i: %s\n'
  42. % (-exitcode, ' '.join(list(args))))
  43. return subp.communicate()[0]
  44. def bench_write_req(qemu_img, image_name, block_size, block_offset,
  45. cluster_size):
  46. """Benchmark write requests
  47. The function creates a QCOW2 image with the given path/name. Then it runs
  48. the 'qemu-img bench' command and makes series of write requests on the
  49. image clusters. Finally, it returns the total time of the write operations
  50. on the disk.
  51. qemu_img -- path to qemu_img executable file
  52. image_name -- QCOW2 image name to create
  53. block_size -- size of a block to write to clusters
  54. block_offset -- offset of the block in clusters
  55. cluster_size -- size of the image cluster
  56. Returns {'seconds': int} on success and {'error': str} on failure.
  57. Return value is compatible with simplebench lib.
  58. """
  59. if not os.path.isfile(qemu_img):
  60. print(f'File not found: {qemu_img}')
  61. sys.exit(1)
  62. image_dir = os.path.dirname(os.path.abspath(image_name))
  63. if not os.path.isdir(image_dir):
  64. print(f'Path not found: {image_name}')
  65. sys.exit(1)
  66. image_size = 1024 * 1024 * 1024
  67. args_create = [qemu_img, 'create', '-f', 'qcow2', '-o',
  68. f'cluster_size={cluster_size}',
  69. image_name, str(image_size)]
  70. count = int(image_size / cluster_size) - 1
  71. step = str(cluster_size)
  72. args_bench = [qemu_img, 'bench', '-w', '-n', '-t', 'none', '-c',
  73. str(count), '-s', f'{block_size}', '-o', str(block_offset),
  74. '-S', step, '-f', 'qcow2', image_name]
  75. try:
  76. qemu_img_pipe(*args_create)
  77. except OSError as e:
  78. os.remove(image_name)
  79. return {'error': 'qemu_img create failed: ' + str(e)}
  80. try:
  81. ret = qemu_img_pipe(*args_bench)
  82. except OSError as e:
  83. os.remove(image_name)
  84. return {'error': 'qemu_img bench failed: ' + str(e)}
  85. os.remove(image_name)
  86. if 'seconds' in ret:
  87. ret_list = ret.split()
  88. index = ret_list.index('seconds.')
  89. return {'seconds': float(ret_list[index-1])}
  90. else:
  91. return {'error': 'qemu_img bench failed: ' + ret}
  92. if __name__ == '__main__':
  93. if len(sys.argv) < 4:
  94. program = os.path.basename(sys.argv[0])
  95. print(f'USAGE: {program} <path to qemu-img binary file> '
  96. '<path to another qemu-img to compare performance with> '
  97. '<full or relative name for QCOW2 image to create>')
  98. exit(1)
  99. # Test-cases are "rows" in benchmark resulting table, 'id' is a caption
  100. # for the row, other fields are handled by bench_func.
  101. test_cases = [
  102. {
  103. 'id': '<cluster front>',
  104. 'block_size': 4096,
  105. 'block_offset': 0,
  106. 'cluster_size': 1048576
  107. },
  108. {
  109. 'id': '<cluster middle>',
  110. 'block_size': 4096,
  111. 'block_offset': 524288,
  112. 'cluster_size': 1048576
  113. },
  114. {
  115. 'id': '<cross cluster>',
  116. 'block_size': 1048576,
  117. 'block_offset': 4096,
  118. 'cluster_size': 1048576
  119. },
  120. {
  121. 'id': '<cluster 64K>',
  122. 'block_size': 4096,
  123. 'block_offset': 0,
  124. 'cluster_size': 65536
  125. },
  126. ]
  127. # Test-envs are "columns" in benchmark resulting table, 'id is a caption
  128. # for the column, other fields are handled by bench_func.
  129. # Set the paths below to desired values
  130. test_envs = [
  131. {
  132. 'id': '<qemu-img binary 1>',
  133. 'qemu_img': f'{sys.argv[1]}',
  134. 'image_name': f'{sys.argv[3]}'
  135. },
  136. {
  137. 'id': '<qemu-img binary 2>',
  138. 'qemu_img': f'{sys.argv[2]}',
  139. 'image_name': f'{sys.argv[3]}'
  140. },
  141. ]
  142. result = simplebench.bench(bench_func, test_envs, test_cases, count=3,
  143. initial_run=False)
  144. print(results_to_text(result))