summaryrefslogtreecommitdiff
path: root/test-persistence
blob: 0b87efe3cdc3514d11318c009b3b4ed87c3afff7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/usr/bin/env python3
# Copyright (C) 2018  Lars Wirzenius
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


import cProfile
import os
import sys
import time
import uuid


import muck


def create_id():
    return str(uuid.uuid4())


def changes(n):
    res = {
        'foo': 'bar',
    }
    
    for i in range(n):
        meta = {
            'id': create_id(),
            'rev': create_id(),
        }
        chg = muck.CreateChange(meta=meta, res=res)
        yield chg


def compare_dicts(d1, d2):
    for key in d1:
        if key not in d2:
            return 'Missing key: {}'.format(key)

    for key in d2:
        if key not in d1:
            return 'Extra key: {}'.format(key)

    for key in d1:
        v1 = d1[key]
        v2 = d2[key]
        if v1 != v2:
            return 'Different values: {}: {} vs {}'.format(key, v1, v2)

    return None


def main():
    store_dir = sys.argv[1]
    n = int(sys.argv[2])
    check = sys.argv[4] == 'check'

    if not os.path.exists(store_dir):
        os.mkdir(store_dir)

    started = time.time()
    st = muck.Store(store_dir)
    elapsed = time.time() - started
    sys.stdout.write('Store load time: {:.1f} s\n'.format(elapsed))

    started = time.time()
    prev_progress = started
    for i, chg in enumerate(changes(n)):
        st.change(chg)

        if check:
            st2 = muck.Store(store_dir)
            ms = st.get_memory_store()
            ms2 = st2.get_memory_store()
            err = compare_dicts(ms.as_dict(), ms2.as_dict())
            if err:
                sys.stderr.write('ERROR: {}: {}\n'.format(i+1, err))
                sys.exit(1)

        done = i + 1
        now = time.time()
        progress_elapsed = now - prev_progress
        if progress_elapsed > 10:
            prev_progress = now
            elapsed = now - started
            speed = done / elapsed
            remain = n - done
            ts = time.strftime('%H:%M:%S', time.localtime(now))

            sys.stdout.write(
                '{} Made {} changes, {} remain, speed {:.1f}/s\n'.format(
                    ts, done, remain, speed))
            sys.stdout.flush()

    elapsed = time.time() - started
    speed = done / elapsed
    sys.stdout.write(
        'OK, created {} resources in {:.1f} s at {:.1f}/s\n'.format(
            n, elapsed, speed))


profile = sys.argv[3]

cProfile.run('main()', filename=profile)