1"""
2Do benchmarks using pytest-benchmark.
3
4Usage:
5
6    py.test --benchmark-only
7"""
8
9import os
10
11import pytest
12
13from .archiver import changedir, cmd
14
15
16@pytest.fixture
17def repo_url(request, tmpdir, monkeypatch):
18    monkeypatch.setenv('BORG_PASSPHRASE', '123456')
19    monkeypatch.setenv('BORG_CHECK_I_KNOW_WHAT_I_AM_DOING', 'YES')
20    monkeypatch.setenv('BORG_DELETE_I_KNOW_WHAT_I_AM_DOING', 'YES')
21    monkeypatch.setenv('BORG_UNKNOWN_UNENCRYPTED_REPO_ACCESS_IS_OK', 'yes')
22    monkeypatch.setenv('BORG_KEYS_DIR', str(tmpdir.join('keys')))
23    monkeypatch.setenv('BORG_CACHE_DIR', str(tmpdir.join('cache')))
24    yield str(tmpdir.join('repository'))
25    tmpdir.remove(rec=1)
26
27
28@pytest.fixture(params=["none", "repokey"])
29def repo(request, cmd, repo_url):
30    cmd('init', '--encryption', request.param, repo_url)
31    return repo_url
32
33
34@pytest.fixture(scope='session', params=["zeros", "random"])
35def testdata(request, tmpdir_factory):
36    count, size = 10, 1000*1000
37    p = tmpdir_factory.mktemp('data')
38    data_type = request.param
39    if data_type == 'zeros':
40        # do not use a binary zero (\0) to avoid sparse detection
41        def data(size):
42            return b'0' * size
43    elif data_type == 'random':
44        def data(size):
45            return os.urandom(size)
46    else:
47        raise ValueError("data_type must be 'random' or 'zeros'.")
48    for i in range(count):
49        with open(str(p.join(str(i))), "wb") as f:
50            f.write(data(size))
51    yield str(p)
52    p.remove(rec=1)
53
54
55@pytest.fixture(params=['none', 'lz4'])
56def archive(request, cmd, repo, testdata):
57    archive_url = repo + '::test'
58    cmd('create', '--compression', request.param, archive_url, testdata)
59    return archive_url
60
61
62def test_create_none(benchmark, cmd, repo, testdata):
63    result, out = benchmark.pedantic(cmd, ('create', '--compression', 'none', repo + '::test', testdata))
64    assert result == 0
65
66
67def test_create_lz4(benchmark, cmd, repo, testdata):
68    result, out = benchmark.pedantic(cmd, ('create', '--compression', 'lz4', repo + '::test', testdata))
69    assert result == 0
70
71
72def test_extract(benchmark, cmd, archive, tmpdir):
73    with changedir(str(tmpdir)):
74        result, out = benchmark.pedantic(cmd, ('extract', archive))
75    assert result == 0
76
77
78def test_delete(benchmark, cmd, archive):
79    result, out = benchmark.pedantic(cmd, ('delete', archive))
80    assert result == 0
81
82
83def test_list(benchmark, cmd, archive):
84    result, out = benchmark(cmd, 'list', archive)
85    assert result == 0
86
87
88def test_info(benchmark, cmd, archive):
89    result, out = benchmark(cmd, 'info', archive)
90    assert result == 0
91
92
93def test_check(benchmark, cmd, archive):
94    repo = archive.split('::')[0]
95    result, out = benchmark(cmd, 'check', repo)
96    assert result == 0
97
98
99def test_help(benchmark, cmd):
100    result, out = benchmark(cmd, 'help')
101    assert result == 0
102