benchmark.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. """
  2. Do benchmarks using pytest-benchmark.
  3. Usage:
  4. py.test --benchmark-only
  5. """
  6. import os
  7. import pytest
  8. from .archiver import changedir, exec_cmd
  9. # TODO: use fixture params to test python code and binary
  10. @pytest.fixture
  11. def cmd():
  12. return exec_cmd
  13. @pytest.yield_fixture
  14. def repo_url(request, tmpdir):
  15. os.environ['BORG_PASSPHRASE'] = '123456'
  16. os.environ['BORG_CHECK_I_KNOW_WHAT_I_AM_DOING'] = '1'
  17. os.environ['BORG_UNKNOWN_UNENCRYPTED_REPO_ACCESS_IS_OK'] = '1'
  18. os.environ['BORG_KEYS_DIR'] = str(tmpdir.join('keys'))
  19. os.environ['BORG_CACHE_DIR'] = str(tmpdir.join('cache'))
  20. yield str(tmpdir.join('repository'))
  21. tmpdir.remove(rec=1)
  22. @pytest.fixture(params=["none", "passphrase"])
  23. def repo(request, cmd, repo_url):
  24. cmd('init', '--encryption', request.param, repo_url)
  25. return repo_url
  26. @pytest.yield_fixture(scope='session', params=["zeros", "random"])
  27. def testdata(request, tmpdir_factory):
  28. count, size = 10, 1000*1000
  29. p = tmpdir_factory.mktemp('data')
  30. data_type = request.param
  31. if data_type == 'zeros':
  32. # do not use a binary zero (\0) to avoid sparse detection
  33. data = lambda: b'0' * size
  34. if data_type == 'random':
  35. rnd = open('/dev/urandom', 'rb')
  36. data = lambda: rnd.read(size)
  37. for i in range(count):
  38. with open(str(p.join(str(i))), "wb") as f:
  39. f.write(data())
  40. if data_type == 'random':
  41. rnd.close()
  42. yield str(p)
  43. p.remove(rec=1)
  44. @pytest.fixture(params=['none', 'lz4'])
  45. def archive(request, cmd, repo, testdata):
  46. archive_url = repo + '::test'
  47. cmd('create', '--compression', request.param, archive_url, testdata)
  48. return archive_url
  49. def test_create_none(benchmark, cmd, repo, testdata):
  50. result, out = benchmark.pedantic(cmd, ('create', '--compression', 'none', repo + '::test', testdata))
  51. assert result == 0
  52. def test_create_lz4(benchmark, cmd, repo, testdata):
  53. result, out = benchmark.pedantic(cmd, ('create', '--compression', 'lz4', repo + '::test', testdata))
  54. assert result == 0
  55. def test_extract(benchmark, cmd, archive, tmpdir):
  56. with changedir(str(tmpdir)):
  57. result, out = benchmark.pedantic(cmd, ('extract', archive))
  58. assert result == 0
  59. def test_delete(benchmark, cmd, archive):
  60. result, out = benchmark.pedantic(cmd, ('delete', archive))
  61. assert result == 0
  62. def test_list(benchmark, cmd, archive):
  63. result, out = benchmark(cmd, 'list', archive)
  64. assert result == 0
  65. def test_info(benchmark, cmd, archive):
  66. result, out = benchmark(cmd, 'info', archive)
  67. assert result == 0
  68. def test_check(benchmark, cmd, archive):
  69. repo = archive.split('::')[0]
  70. result, out = benchmark(cmd, 'check', repo)
  71. assert result == 0
  72. def test_help(benchmark, cmd):
  73. result, out = benchmark(cmd, 'help')
  74. assert result == 0