2
0

bench_block_job.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #!/usr/bin/env python
  2. #
  3. # Benchmark block jobs
  4. #
  5. # Copyright (c) 2019 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. import sys
  21. import os
  22. import socket
  23. import json
  24. sys.path.append(os.path.join(os.path.dirname(__file__), '..', '..', 'python'))
  25. from qemu.machine import QEMUMachine
  26. from qemu.qmp import QMPConnectError
  27. def bench_block_job(cmd, cmd_args, qemu_args):
  28. """Benchmark block-job
  29. cmd -- qmp command to run block-job (like blockdev-backup)
  30. cmd_args -- dict of qmp command arguments
  31. qemu_args -- list of Qemu command line arguments, including path to Qemu
  32. binary
  33. Returns {'seconds': int} on success and {'error': str} on failure, dict may
  34. contain addional 'vm-log' field. Return value is compatible with
  35. simplebench lib.
  36. """
  37. vm = QEMUMachine(qemu_args[0], args=qemu_args[1:])
  38. try:
  39. vm.launch()
  40. except OSError as e:
  41. return {'error': 'popen failed: ' + str(e)}
  42. except (QMPConnectError, socket.timeout):
  43. return {'error': 'qemu failed: ' + str(vm.get_log())}
  44. try:
  45. res = vm.qmp(cmd, **cmd_args)
  46. if res != {'return': {}}:
  47. vm.shutdown()
  48. return {'error': '"{}" command failed: {}'.format(cmd, str(res))}
  49. e = vm.event_wait('JOB_STATUS_CHANGE')
  50. assert e['data']['status'] == 'created'
  51. start_ms = e['timestamp']['seconds'] * 1000000 + \
  52. e['timestamp']['microseconds']
  53. e = vm.events_wait((('BLOCK_JOB_READY', None),
  54. ('BLOCK_JOB_COMPLETED', None),
  55. ('BLOCK_JOB_FAILED', None)), timeout=True)
  56. if e['event'] not in ('BLOCK_JOB_READY', 'BLOCK_JOB_COMPLETED'):
  57. vm.shutdown()
  58. return {'error': 'block-job failed: ' + str(e),
  59. 'vm-log': vm.get_log()}
  60. end_ms = e['timestamp']['seconds'] * 1000000 + \
  61. e['timestamp']['microseconds']
  62. finally:
  63. vm.shutdown()
  64. return {'seconds': (end_ms - start_ms) / 1000000.0}
  65. # Bench backup or mirror
  66. def bench_block_copy(qemu_binary, cmd, source, target):
  67. """Helper to run bench_block_job() for mirror or backup"""
  68. assert cmd in ('blockdev-backup', 'blockdev-mirror')
  69. source['node-name'] = 'source'
  70. target['node-name'] = 'target'
  71. return bench_block_job(cmd,
  72. {'job-id': 'job0', 'device': 'source',
  73. 'target': 'target', 'sync': 'full'},
  74. [qemu_binary,
  75. '-blockdev', json.dumps(source),
  76. '-blockdev', json.dumps(target)])
  77. def drv_file(filename):
  78. return {'driver': 'file', 'filename': filename,
  79. 'cache': {'direct': True}, 'aio': 'native'}
  80. def drv_nbd(host, port):
  81. return {'driver': 'nbd',
  82. 'server': {'type': 'inet', 'host': host, 'port': port}}
  83. if __name__ == '__main__':
  84. import sys
  85. if len(sys.argv) < 4:
  86. print('USAGE: {} <qmp block-job command name> '
  87. '<json string of arguments for the command> '
  88. '<qemu binary path and arguments>'.format(sys.argv[0]))
  89. exit(1)
  90. res = bench_block_job(sys.argv[1], json.loads(sys.argv[2]), sys.argv[3:])
  91. if 'seconds' in res:
  92. print('{:.2f}'.format(res['seconds']))
  93. else:
  94. print(res)