benchmark.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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, cmd
  9. @pytest.yield_fixture
  10. def repo_url(request, tmpdir):
  11. os.environ['BORG_PASSPHRASE'] = '123456'
  12. os.environ['BORG_CHECK_I_KNOW_WHAT_I_AM_DOING'] = 'YES'
  13. os.environ['BORG_DELETE_I_KNOW_WHAT_I_AM_DOING'] = 'YES'
  14. os.environ['BORG_UNKNOWN_UNENCRYPTED_REPO_ACCESS_IS_OK'] = 'yes'
  15. os.environ['BORG_KEYS_DIR'] = str(tmpdir.join('keys'))
  16. os.environ['BORG_CACHE_DIR'] = str(tmpdir.join('cache'))
  17. yield str(tmpdir.join('repository'))
  18. tmpdir.remove(rec=1)
  19. @pytest.fixture(params=["none", "repokey"])
  20. def repo(request, cmd, repo_url):
  21. cmd('init', '--encryption', request.param, repo_url)
  22. return repo_url
  23. @pytest.yield_fixture(scope='session', params=["zeros", "random"])
  24. def testdata(request, tmpdir_factory):
  25. count, size = 10, 1000*1000
  26. p = tmpdir_factory.mktemp('data')
  27. data_type = request.param
  28. if data_type == 'zeros':
  29. # do not use a binary zero (\0) to avoid sparse detection
  30. def data(size):
  31. return b'0' * size
  32. if data_type == 'random':
  33. def data(size):
  34. return os.urandom(size)
  35. for i in range(count):
  36. with open(str(p.join(str(i))), "wb") as f:
  37. f.write(data(size))
  38. yield str(p)
  39. p.remove(rec=1)
  40. @pytest.fixture(params=['none', 'lz4'])
  41. def archive(request, cmd, repo, testdata):
  42. archive_url = repo + '::test'
  43. cmd('create', '--compression', request.param, archive_url, testdata)
  44. return archive_url
  45. def test_create_none(benchmark, cmd, repo, testdata):
  46. result, out = benchmark.pedantic(cmd, ('create', '--compression', 'none', repo + '::test', testdata))
  47. assert result == 0
  48. def test_create_lz4(benchmark, cmd, repo, testdata):
  49. result, out = benchmark.pedantic(cmd, ('create', '--compression', 'lz4', repo + '::test', testdata))
  50. assert result == 0
  51. def test_extract(benchmark, cmd, archive, tmpdir):
  52. with changedir(str(tmpdir)):
  53. result, out = benchmark.pedantic(cmd, ('extract', archive))
  54. assert result == 0
  55. def test_delete(benchmark, cmd, archive):
  56. result, out = benchmark.pedantic(cmd, ('delete', archive))
  57. assert result == 0
  58. def test_list(benchmark, cmd, archive):
  59. result, out = benchmark(cmd, 'list', archive)
  60. assert result == 0
  61. def test_info(benchmark, cmd, archive):
  62. result, out = benchmark(cmd, 'info', archive)
  63. assert result == 0
  64. def test_check(benchmark, cmd, archive):
  65. repo = archive.split('::')[0]
  66. result, out = benchmark(cmd, 'check', repo)
  67. assert result == 0
  68. def test_help(benchmark, cmd):
  69. result, out = benchmark(cmd, 'help')
  70. assert result == 0