-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbenchmark_datreant.py
162 lines (126 loc) · 5.37 KB
/
benchmark_datreant.py
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# Copyright 2017 The Regents of the University of Michigan
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of
# this software and associated documentation files (the "Software"), to deal in
# the Software without restriction, including without limitation the rights to
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
# of the Software, and to permit persons to whom the Software is furnished to do
# so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import os
import six
import random
import string
import timeit
import logging
import warnings
from multiprocessing import Pool
from collections import OrderedDict
from contextlib import contextmanager
from tqdm import tqdm
from signac.contrib.hashing import calc_id
import datreant.core as dtr
if six.PY2:
from tempdir import TemporaryDirectory
else:
from tempfile import TemporaryDirectory
logger = logging.getLogger(__name__)
class Timer(timeit.Timer):
def timeit(self, number=10):
return number, super().timeit(number=number)
def repeat(self, repeat=3, number=10):
return super().repeat(repeat=repeat, number=number)
def setup(N, root=None):
if root is None:
root = '.'
for i in tqdm(range(N)):
sp = dict(a=i)
s = dtr.Treant(os.path.join(root, 'workspace', calc_id(sp)))
for key, value in sp.items():
s.categories[key] = value
def determine_bundle_size(root):
bundle = dtr.Bundle(os.path.join(root, 'workspace', '*'))
size = 0
for t in bundle:
for c in t.children:
size += os.path.getsize(str(c))
return {
'N': len(bundle),
'total': size,
}
def _random_str(size):
return ''.join(random.choice(string.ascii_lowercase) for _ in range(size))
def _make_doc(i, num_keys=1, data_size=0):
assert num_keys >= 1
assert data_size >= 0
doc = {'b_{}'.format(j): _random_str(data_size) for j in range(num_keys - 1)}
doc['a'] = '{}{}'.format(i, _random_str(max(0, data_size - len(str(i)))))
return doc
def _make_treant(root, num_keys, num_doc_keys, data_size, data_std, i):
size = max(0, int(random.gauss(data_size, data_std)))
sp = _make_doc(i, num_keys, size)
_id = calc_id(sp)
t = dtr.Treant(os.path.join(root, 'workspace', _id))
t.categories = sp
assert t.categories == sp
def generate_random_data(root, N_sp, num_keys=1, num_doc_keys=0,
data_size=0, data_std=0, parallel=True):
if six.PY2:
if parallel:
warnings.warn("Function 'generate_random_data()' not parallelized for Python 2.")
parallel = False
if parallel:
with Pool() as pool:
p = [(root, num_keys, num_doc_keys, data_size, data_std, i) for i in range(N_sp)]
list(pool.starmap(_make_treant, tqdm(p, desc='init random project data')))
else:
from functools import partial
make = partial(_make_treant, root, num_keys, num_doc_keys, data_size, data_std)
list(map(make, tqdm(range(N_sp), desc='init random project data')))
@contextmanager
def setup_random_bundle(N, num_keys=1, num_doc_keys=0,
data_size=0, data_std=0, seed=0, root=None):
random.seed(seed)
if not isinstance(N, int):
raise TypeError("N must be an integer!")
with TemporaryDirectory(dir=root) as tmp:
generate_random_data(tmp, N, num_keys, num_doc_keys, data_size, data_std)
yield tmp
def benchmark_bundle(root, keys=None, skip_rich_filter=False):
setup = "import datreant.core as dtr; bundle = dtr.Bundle('{}/workspace/*');".format(root)
setup += "import random;"
data = OrderedDict()
def run(key, timer, repeat=3, number=10):
if keys is None or key in keys:
logger.info("Run '{}'...".format(key))
data[key] = timer.repeat(repeat=repeat, number=number)
run('determine_len', Timer('len(bundle)', setup=setup))
run('select_by_id', Timer(
stmt="dtr.Treant(path).categories",
setup=setup + "path = random.choice(bundle)"))
run('iterate', Timer(
stmt='[dtr.Treant(b).categories for b in bundle]',
setup=setup))
run('iterate_single_pass', Timer(
stmt='[dtr.Treant(b).categories for b in bundle]',
setup=setup), number=1)
run('search_lean_filter', Timer(
stmt="bundle.categories.groupby(k)[v]",
setup=setup + 'import random; sp = dtr.Treant(random.choice(bundle)).categories;'
'k, v = dict(sp).popitem();'))
if not skip_rich_filter:
run('search_rich_filter', Timer(
stmt="bundle.categories.groupby(keys)[tuple(values)];",
setup=setup + "sp = dict(dtr.Treant(random.choice(bundle)).categories);"
"keys = list(sp); values = [sp[k] for k in keys];"))
return data