-
Notifications
You must be signed in to change notification settings - Fork 24
/
profile.sh
executable file
·300 lines (252 loc) · 6.86 KB
/
profile.sh
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#!/bin/bash
DEFAULT_ALGORITHM=sha256
DEFAULT_THRESHOLD=128
DEFAULT_CAPACITY=$((1024 ** 3))
DEFAULT_OPTIMIZATIONS=true
DEFAULT_CACHE=true
DEFAULT_DBFILE="./benchmarks/merkle.db"
DEFAULT_ACTION="root"
DEFAULT_SIZE=$((10 ** 6))
DEFAULT_ROUNDS=1
DEFAULT_RANDOMIZE=false
DEFAULT_TIMING=false
DEFAULT_LINE=false
DEFAULT_GRAPH=false
DEFAULT_RESULTS="./profiler/.results"
usage_string="
usage: ./$(basename "$0") [options]
Profile by running the ./profiler script against ${DEFAULT_DBFILE}
Default mode is memory profiling using valgrind. By appropriately combining the
\"--time\" and \"--line\" options, you can achieve any of the following modes:
- vague time profiling using the unix time utility
- line-by-line memory profiling using the memory_profiler python package
- line-by-line time profiling using the line_profiler python package
Results saved in "$DEFAULT_RESULTS"
Tree configuration
--algorithm HASH Hash algorithm used by the tree (default: ${DEFAULT_ALGORITHM})
--threshold WIDTH Cache threshold (default: ${DEFAULT_THRESHOLD})
--capacity MAXSIZE Cache capacity in bytes (default: 1GiB)
--disable-optimizations Use unoptimized version of core operations
--disable-cache Disable caching
Profiler options
--dbfile DB Database to use (default: ${DEFAULT_DBFILE})
--operation OP Operation to profile: root, state, inclusion,
consistency (default: ${DEFAULT_ACTION})
--size SIZE Nr entries to consider (default: ${DEFAULT_SIZE})
--index INDEX Base index for proof operations. If not provided,
it will be set equal to ceil(size/2)
--rounds ROUNDS Nr rounds (default: ${DEFAULT_ROUNDS})
-r, --randomize Randomize function input per round. Useful for
capturing the effect of caching. WARNING:
This will nullify the effect of the index option
-t, --time Profile execution times instead of memory allocations
-l, --line Line-by-line profiling
-g, --graph Create flame graph in case of line-by-line memory
profiling
-h, --help Display help message and exit
"
set -e
usage() { echo -n "$usage_string" 1>&2; }
resolve_action_options() {
if [ -z ${INDEX} ]; then
INDEX=$(($(($SIZE + 1))/2))
fi
case $1 in
root)
options="--start ${INDEX} --limit ${SIZE}"
;;
state)
options="--size ${SIZE}"
;;
inclusion)
options="--index ${INDEX} --size ${SIZE}"
;;
consistency)
options="--size1 ${INDEX} --size2 ${SIZE}"
;;
*)
echo "Invalid operation: ${ACTION}"
usage
exit 1
;;
esac
echo $options
}
resolve_operation() {
script="profiler/__main__.py --dbfile ${DBFILE} --algorithm ${ALGORITHM} \
--capacity ${CAPACITY} --threshold ${THRESHOLD} \
--rounds ${ROUNDS}
"
if [ ${RANDOMIZE} == true ]; then
script+=" --randomize"
fi
if [ ${OPTIMIZATIONS} == false ]; then
script+=" --disable-optimizations"
fi
if [ ${CACHE} == false ]; then
script+=" --disable-cache"
fi
options=$(resolve_action_options "$ACTION")
echo "${script} ${ACTION} ${options}"
}
profile_time_line_by_line() {
outfile="${RESULTS}/ltime.prof"
kernprof \
--view \
--unit 1e-6 \
--line-by-line \
--skip-zero \
--outfile ${outfile} \
$1
}
profile_time() {
$(which time) \
--verbose \
python $1
}
profile_memory_line_by_line() {
outfile="${RESULTS}/lmem.prof"
rm -f ${outfile}
# mprof run \
# --interval 0.1 \
# --backend psutil \
# --output ${outfile} \
# $1
# mprof peak ${outfile}
python -m memory_profiler \
--precision 3 \
$1
if [ ${GRAPH} == true ]; then
mprof plot \
--flame \
--title ${ACTION} \
--output ${RESULTS}/memory.png \
${outfile}
fi
}
profile_memory() {
valgrind \
--tool=massif \
--heap=yes \
--massif-out-file="${RESULTS}/massif.out.%p" \
python $1
infile=$(find ${RESULTS}/massif.out.* \
-printf "%t - %p\n" \
| sort -nr \
| awk 'NR==1 {print $7}')
massif-visualizer "${infile}"&
}
resolve_profiler() {
profiler="profile"
if [ ${TIMING} == true ]; then
profiler+="_time"
else
profiler+="_memory"
fi
if [ ${LINE} == true ]; then
profiler+="_line_by_line"
fi
echo $profiler
}
ALGORITHM="$DEFAULT_ALGORITHM"
THRESHOLD="$DEFAULT_THRESHOLD"
CAPACITY="$DEFAULT_CAPACITY"
OPTIMIZATIONS="$DEFAULT_OPTIMIZATIONS"
CACHE="$DEFAULT_CACHE"
DBFILE="$DEFAULT_DBFILE"
ACTION="$DEFAULT_ACTION"
SIZE="$DEFAULT_SIZE"
ROUNDS="$DEFAULT_ROUNDS"
RANDOMIZE="$DEFAULT_ROUNDS"
MEMORY="$DEFAULT_MEMORY"
TIMING="$DEFAULT_TIMING"
LINE="$DEFAULT_LINE"
GRAPH="$DEFAULT_GRAPH"
RESULTS="$DEFAULT_RESULTS"
while [[ $# -gt 0 ]]
do
arg="$1"
case $arg in
--algorithm)
ALGORITHM="$2"
shift
shift
;;
--threshold)
THRESHOLD="$2"
shift
shift
;;
--capacity)
CAPACITY="$2"
shift
shift
;;
--disable-optimizations)
OPTIMIZATIONS=false
shift
;;
--disable-cache)
CACHE=false
shift
;;
--dbfile)
DBFILE="$2"
if [ ! -f "$DBFILE" ]; then
echo "No database found at ${DBFILE}"
exit 1
fi
shift
shift
;;
--operation)
ACTION="$2"
shift
shift
;;
--size)
SIZE="$2"
shift
shift
;;
--index)
INDEX="$2"
shift
shift
;;
--rounds)
ROUNDS="$2"
shift
shift
;;
-r|--randomize)
RANDOMIZE=true
shift
;;
-t|--time)
TIMING=true
shift
;;
-l|--line)
LINE=true
shift
;;
-g|--graph)
GRAPH=true
shift
;;
-h|--help)
usage
exit 0
;;
*)
echo "Invalid argument: ${arg}"
usage
exit 1
;;
esac
done
mkdir -p ${RESULTS}
operation=$(resolve_operation)
profiler=$(resolve_profiler)
"$profiler" "$operation"