-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbench
More file actions
executable file
·167 lines (149 loc) · 4.59 KB
/
Copy pathbench
File metadata and controls
executable file
·167 lines (149 loc) · 4.59 KB
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
#!/usr/bin/env bash
set -euo pipefail
source "$HOME/dotfiles/bin/src/capture.bash"
root_dir="$(pwd)"
par2_cli="${PAR2_CLI_BIN:-$root_dir/zig-out/bin/par2z-cli}"
other_cli="${PAR2_OTHER_BIN:-par2}"
other_name="${PAR2_OTHER_NAME:-par2cmdline}"
bench_log="${PAR2_BENCH_LOG:-bench-results.tsv}"
prng_gen="${PAR2_PRNG_GEN:-$root_dir/zig-out/par2z/bin/prng-gen}"
bench_optimize="${PAR2_BENCH_OPTIMIZE:-ReleaseFast}"
bench_build="${PAR2_BENCH_BUILD:-1}"
bench_size="${PAR2_BENCH_SIZE:-33554432}"
block_size="${PAR2_BENCH_BLOCK_SIZE:-4096}"
redundancy="${PAR2_BENCH_REDUNDANCY:-10}"
iters="${PAR2_BENCH_ITERS:-2}"
corrupt_bytes="${PAR2_BENCH_CORRUPT_BYTES:-4096}"
bench_seed="${PAR2_BENCH_SEED:-1}"
bench_seq="${PAR2_BENCH_SEQ:-1}"
out=""
err=""
rc=0
if command -v mktmp >/dev/null 2>&1; then
tmpdir="$(mktmp --tmpdir)"
else
tmpdir="$(mktemp -d "${TMPDIR:-/tmp}/par2-bench.XXXXXX")"
echo "warning: mktmp not found; using mktemp" >&2
fi
cleanup() {
if [ -n "${tmpdir-}" ] && [ -d "$tmpdir" ]; then
rm -rf "$tmpdir"
fi
}
trap cleanup EXIT
now_ms() {
perl -MTime::HiRes -e 'printf "%.0f\n", Time::HiRes::time()*1000'
}
run_timed() {
local tool="$1"
local phase="$2"
shift 2
local start end elapsed rate
start="$(now_ms)"
capture -- "$@"
end="$(now_ms)"
elapsed=$((end - start))
if [ "$rc" -ne 0 ]; then
echo "$tool $phase failed: $err" >&2
exit 1
fi
rate="$(awk -v size="$bench_size" -v ms="$elapsed" 'BEGIN { if (ms == 0) { print "inf"; } else { printf "%.2f", (size/1048576)/(ms/1000) } }')"
printf '%-24s %8s ms %8s MiB/s\n' "$tool $phase" "$elapsed" "$rate"
log_result "$tool" "$phase" "$elapsed" "$rate"
}
gen_data() {
local path="$1"
if [ -x "$prng_gen" ]; then
capture "$prng_gen" "$path" "$bench_size" "$bench_seed" "$bench_seq"
if [ "$rc" -ne 0 ]; then
echo "prng-gen failed: $err" >&2
exit 1
fi
else
capture dd if=/dev/urandom of="$path" bs=1 count="$bench_size" status=none
if [ "$rc" -ne 0 ]; then
echo "dd failed: $err" >&2
exit 1
fi
fi
}
corrupt_file() {
local path="$1"
capture dd if=/dev/urandom of="$path" bs=1 count="$corrupt_bytes" seek=4096 conv=notrunc status=none
if [ "$rc" -ne 0 ]; then
echo "dd corrupt failed: $err" >&2
exit 1
fi
}
bench_tool() {
local name="$1"
local kind="$2"
local dir="$3"
local data="$4"
mkdir -p "$dir"
cp "$data" "$dir/data.bin"
cp "$data" "$dir/orig.bin"
if [ "$kind" = "par2" ]; then
local par2_path="$dir/data.par2"
run_timed "$name" "create" "$other_cli" create "-s$block_size" "-r$redundancy" "$par2_path" "$dir/data.bin"
run_timed "$name" "verify" "$other_cli" verify "$par2_path" "$dir/data.bin"
corrupt_file "$dir/data.bin"
run_timed "$name" "repair" "$other_cli" repair -q "$par2_path" "$dir/data.bin"
else
local par2_path="$dir/data.par2"
PAR2_MUTE_DEFAULTS=1 run_timed "$name" "create" "$par2_cli" create --block-size "$block_size" --redundancy-percent "$redundancy" "$par2_path" "$dir/data.bin"
run_timed "$name" "verify" "$par2_cli" verify "$par2_path" "$dir/data.bin"
corrupt_file "$dir/data.bin"
run_timed "$name" "recover" "$par2_cli" recover -o "$dir/out" "$par2_path" "$dir/data.bin"
fi
}
if [ "$bench_build" -ne 0 ]; then
capture zig build -Doptimize="$bench_optimize"
if [ "$rc" -ne 0 ]; then
echo "zig build failed: $err" >&2
exit 1
fi
par2_cli="$root_dir/zig-out/bin/par2z-cli"
fi
capture "$par2_cli" -h
if [ "$rc" -ne 0 ]; then
echo "par2z-cli not runnable at $par2_cli" >&2
exit 1
fi
capture "$other_cli" -V
if [ "$rc" -ne 0 ]; then
echo "other par2 not runnable at $other_cli" >&2
exit 1
fi
bench_head="$(git rev-parse HEAD 2>/dev/null || true)"
if [ -z "$bench_head" ]; then
bench_head="$(jj log --ignore-working-copy -n1 --template 'commit_id' 2>/dev/null || true)"
fi
if [ -z "$bench_head" ]; then
bench_head="unknown"
fi
if [ ! -f "$bench_log" ]; then
printf "timestamp\tcommit\ttool\tphase\telapsed_ms\trate_mib_s\tbench_size\tblock_size\tredundancy\titers\tother_cli\tpar2_cli\n" >> "$bench_log"
fi
log_result() {
local tool="$1"
local phase="$2"
local elapsed="$3"
local rate="$4"
local ts
ts="$(date -u +"%Y-%m-%dT%H:%M:%SZ")"
printf "%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n" \
"$ts" "$bench_head" "$tool" "$phase" "$elapsed" "$rate" \
"$bench_size" "$block_size" "$redundancy" "$iters" \
"$other_cli" "$par2_cli" >> "$bench_log"
}
echo "bench size=$bench_size block_size=$block_size redundancy=$redundancy iters=$iters"
data="$tmpdir/data.bin"
gen_data "$data"
iter=1
while [ "$iter" -le "$iters" ]; do
echo "iteration $iter/$iters"
bench_tool "$other_name" "par2" "$tmpdir/par2-$iter" "$data"
bench_tool "par2z-cli" "ours" "$tmpdir/ours-$iter" "$data"
iter=$((iter + 1))
done