-
Notifications
You must be signed in to change notification settings - Fork 4
/
bench.sh
executable file
·403 lines (362 loc) · 11.9 KB
/
bench.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
#!/usr/bin/env bash
# Supported types of plugin managers. ('base' is an empty .zshrc)
PLUGIN_MANAGERS="base antibody antidote antigen sheldon zgen zgenom zinit zplug zpm"
# Prints an error message and exits.
err() {
printf "$@"
exit 1
}
# Prints out the command-line usage.
usage() {
cat 1>&2 <<EOF
Benchmark different plugin managers
USAGE:
bench.sh [FLAGS] [OPTIONS]
Options:
-h, --help Show this message and exit
-k, --kind KIND The kind of plugin manager to benchmark
Commands:
update-plugins Update the plugin manager source from plugins.txt
install Benchmark the 'install' step
load Benchmark the 'load' step
run Open 'zsh' with a particular plugin manager
versions Output the versions of the plugin managers
EOF
}
# Prints out an error message and the usage and exits.
usage_err() {
printf "$@"
usage
exit 1
}
# Outputs the command to use to reset any plugin manager state.
_prepare_install() {
case $1 in
base )
;;
antibody )
echo 'rm -rf /root/.cache/antibody'
;;
antidote )
echo 'rm -rf /root/.cache/antidote /root/.zsh_plugins.zsh'
;;
antigen )
echo 'rm -rf /root/.antigen'
;;
sheldon )
echo 'rm -rf /root/.local/share/sheldon'
;;
zgen )
echo 'git -C /root/.zgen clean -dffx'
;;
zgenom )
echo 'git -C /root/.zgenom clean -dffx; git -C /root/.zgen clean -dffx'
;;
zinit )
echo 'find /root/.zinit -mindepth 1 -maxdepth 1 ! -name "bin" -exec rm -rf {} \;'
;;
zplug )
echo 'rm -rf /root/.zplug/repos'
;;
zpm )
echo 'rm -rf "${TMPDIR:-/tmp}/zsh-${UID:-user}"; rm -rf /root/.zpm/plugins; find /root/.zpm -name "*.zwc" -exec rm -f {} \;'
;;
* )
return 1
esac
}
# Build a Docker container for benchmarking.
_docker_build() {
docker build --tag zsh-plugin-manager-benchmark . >/dev/null
}
# Outputs extra arguments for the Docker run command for the given plugin manager.
_docker_args() {
case $1 in
antibody )
echo "-v $PWD/src/antibody/plugins.txt:/root/.antibody/plugins.txt"
;;
antidote )
echo "-v $PWD/src/antidote/zsh_plugins.txt:/root/.zsh_plugins.txt"
;;
sheldon )
echo "-v $PWD/src/sheldon/plugins.toml:/root/.config/sheldon/plugins.toml"
;;
* )
;;
esac
}
# Runs the given command in Docker with the given plugin manager setup.
_docker_run() {
local kind=$1; shift
local args
args=$(_docker_args "$kind")
test $? -ne 0 && err "Error: failed to get Docker args for %s\n" "$kind"
docker run \
$args \
-v "$PWD/results:/target" \
-v "$PWD/src/$kind/zshrc:/root/.zshrc" \
-it zsh-plugin-manager-benchmark \
"$@"
}
# Updates src/ with the plugins in plugins.txt.
_update_plugins() {
local kind=$1
plugins=$(IFS=$'\n' cat src/plugins.txt)
# Antibody
if [ -z "$kind" ] || [ "$kind" = "antibody" ]; then
echo "" > src/antibody/plugins.txt
for line in $plugins; do
IFS="@" read -r plugin branch <<< "$line"
echo "$plugin" >> src/antibody/plugins.txt
done
fi
# Antidote
if [ -z "$kind" ] || [ "$kind" = "antidote" ]; then
echo "" > src/antidote/zsh_plugins.txt
for line in $plugins; do
IFS="@" read -r plugin branch <<< "$line"
if [ "$plugin" = "wting/autojump" ]; then
echo "$plugin path:bin" >> src/antidote/zsh_plugins.txt
else
echo "$plugin" >> src/antidote/zsh_plugins.txt
fi
done
fi
# Antigen
if [ -z "$kind" ] || [ "$kind" = "antigen" ]; then
echo '#!/usr/bin/env zsh' > src/antigen/zshrc
echo 'ANTIGEN_LOG=/root/antigen.log' > src/antigen/zshrc
echo 'source /root/antigen.zsh' >> src/antigen/zshrc
for line in $plugins; do
echo "antigen bundle \"$line\"" >> src/antigen/zshrc
done
echo "antigen apply" >> src/antigen/zshrc
fi
# Sheldon
if [ -z "$kind" ] || [ "$kind" = "sheldon" ]; then
echo "" > src/sheldon/plugins.toml
for line in $plugins; do
IFS="@" read -r plugin branch <<< "$line"
echo "plugins.'$plugin'.github = '$plugin'" >> src/sheldon/plugins.toml
done
fi
# Zgen
if [ -z "$kind" ] || [ "$kind" = "zgen" ]; then
echo '#!/usr/bin/env zsh' > src/zgen/zshrc
echo 'source "/root/.zgen/zgen.zsh"' >> src/zgen/zshrc
echo 'if ! zgen saved; then' >> src/zgen/zshrc
for line in $plugins; do
IFS="@" read -r plugin branch <<< "$line"
branch=${branch:-master}
echo " zgen load $plugin \"\" $branch" >> src/zgen/zshrc
done
echo ' zgen save' >> src/zgen/zshrc
echo 'fi' >> src/zgen/zshrc
fi
# Zgenom
if [ -z "$kind" ] || [ "$kind" = "zgenom" ]; then
echo '#!/usr/bin/env zsh' > src/zgenom/zshrc
echo 'source "/root/.zgenom/zgenom.zsh"' >> src/zgenom/zshrc
echo 'if ! zgenom saved; then' >> src/zgenom/zshrc
for line in $plugins; do
IFS="@" read -r plugin branch <<< "$line"
echo " zgenom load $plugin" >> src/zgenom/zshrc
done
echo ' zgenom save' >> src/zgenom/zshrc
echo 'fi' >> src/zgenom/zshrc
fi
# Zinit
if [ -z "$kind" ] || [ "$kind" = "zinit" ]; then
echo '#!/usr/bin/env zsh' > src/zinit/zshrc
echo 'source "/root/.zinit/bin/zinit.zsh"' >> src/zinit/zshrc
for line in $plugins; do
IFS="@" read -r plugin branch <<< "$line"
echo "zinit light $plugin" >> src/zinit/zshrc
done
fi
# Zplug
if [ -z "$kind" ] || [ "$kind" = "zplug" ]; then
echo '#!/usr/bin/env zsh' > src/zplug/zshrc
echo 'export ZPLUG_HOME=/root/.zplug' >> src/zplug/zshrc
echo 'source "$ZPLUG_HOME/init.zsh"' >> src/zplug/zshrc
for line in $plugins; do
IFS="@" read -r plugin branch <<< "$line"
echo "zplug \"$plugin\"" >> src/zplug/zshrc
done
echo '! zplug check && zplug install' >> src/zplug/zshrc
echo 'zplug load' >> src/zplug/zshrc
fi
# Zpm
if [ -z "$kind" ] || [ "$kind" = "zpm" ]; then
echo '#!/usr/bin/env zsh' > src/zpm/zshrc
echo 'source "/root/.zpm/zpm.zsh"' >> src/zpm/zshrc
echo 'zpm load \' >> src/zpm/zshrc
for line in $plugins; do
IFS="@" read -r plugin branch <<< "$line"
echo " ${plugin},async \\" >> src/zpm/zshrc
done
fi
}
# Runs the 'update-plugins' command.
command_update_plugins() {
local kind=$1
_update_plugins "$kind"
}
# Runs the 'install' command.
#
# This benchmarks the 'install' step for the given or all plugin managers.
command_install() {
local kind=$1
local prepare
_update_plugins "$kind" || err "Error: failed to update plugins"
_docker_build || err "Error: failed to build docker image"
for k in $PLUGIN_MANAGERS; do
if [ -z "$kind" ] || [ "$k" = "$kind" ]; then
echo -e "\033[1;32mBenchmarking $k\033[0m "
prepare=$(_prepare_install "$k")
test $? -ne 0 && err "Error: failed to get prepare command for %s\n" "$k"
_docker_run "$k" \
hyperfine \
--prepare "$prepare" \
--warmup 3 \
--export-json "/target/install-$k.json" \
'zsh -ic exit'
fi
done
}
# Runs the 'load' command.
#
# This benchmarks the 'load' step for the given or all plugin managers.
command_load() {
local kind=$1
_update_plugins "$kind" || err "Error: failed to update plugins"
_docker_build || err "Error: failed to build docker image"
for k in $PLUGIN_MANAGERS; do
if [ -z "$kind" ] || [ "$k" = "$kind" ]; then
echo -e "\033[1;32mBenchmarking $k\033[0m "
_docker_run "$k" \
hyperfine \
--warmup 3 \
--export-json "/target/load-$k.json" \
'zsh -ic exit'
fi
done
}
# Runs the 'run' command.
#
# This opens 'zsh' setup for the given plugin manager.
command_run() {
local kind=$1
[ -z "$kind" ] && err "Error: --kind is a required option for this command\n"
_update_plugins "$kind" || err "Error: failed to update plugins"
_docker_build || err "Error: failed to build docker image"
_docker_run "$kind" zsh
}
# Runs the 'versions' command.
#
# This outputs the current version for each plugin manager.
command_versions() {
local kind=$1
local version
_docker_build || err "Error: failed to build docker image"
# Antibody
if [ -z "$kind" ] || [ "$kind" = "antibody" ]; then
version=$(_docker_run base antibody --version 2>&1 | awk '{print $3}')
echo "antibody v$version"
fi
# Antidote
if [ -z "$kind" ] || [ "$kind" = "antidote" ]; then
version=$(_docker_run base antidote --version 2>&1 | awk '{print $3}')
echo "antidote v$version"
fi
# Antigen
if [ -z "$kind" ] || [ "$kind" = "antigen" ]; then
version=$(_docker_run base zsh -c 'source /root/antigen.zsh && antigen-version' | awk '{print $2}')
echo "antigen $version"
fi
# Sheldon
if [ -z "$kind" ] || [ "$kind" = "sheldon" ]; then
version=$(_docker_run base sheldon --version | awk '{print $2; exit}')
echo "sheldon v$version"
fi
# Zgen
if [ -z "$kind" ] || [ "$kind" = "zgen" ]; then
version=$(_docker_run base git -C /root/.zgen rev-parse --short HEAD)
echo "zgen master @ $version"
fi
# Zgenom
if [ -z "$kind" ] || [ "$kind" = "zgenom" ]; then
version=$(_docker_run base git -C /root/.zgenom rev-parse --short HEAD)
echo "zgenom main @ $version"
fi
# Zinit
if [ -z "$kind" ] || [ "$kind" = "zinit" ]; then
version=$(_docker_run base git -C /root/.zinit/bin rev-parse --short HEAD)
echo "zinit master @ $version"
fi
# Zplug
if [ -z "$kind" ] || [ "$kind" = "zplug" ]; then
version=$(_docker_run base git -C /root/.zplug rev-parse --short HEAD)
echo "zplug master @ $version"
fi
# Zpm
if [ -z "$kind" ] || [ "$kind" = "zpm" ]; then
version=$(_docker_run base git -C /root/.zpm rev-parse --short HEAD)
echo "zpm master @ $version"
fi
}
main() {
local cmd kind
while test $# -gt 0; do
case $1 in
--help | -h)
usage
exit 0
;;
--kind | -k)
shift
if [ -z "$1" ]; then
usage_err "Error: --kind option requires an argument\n\n"
elif [[ "$PLUGIN_MANAGERS" != *"$1"* ]]; then
err "Error: --kind value should be one of: $PLUGIN_MANAGERS\n"
fi
kind=$1
;;
update-plugins | install | load | run | versions )
if [ -z "$cmd" ]; then
cmd=$1
else
usage_err "Error: unrecognized command line argument '%s'\n\n" "$1"
fi
;;
*)
usage_err "Error: unrecognized command line argument '%s'\n\n" "$1"
;;
esac
shift
done
case $cmd in
"" )
usage
;;
update-plugins )
command_update_plugins "$kind"
;;
install )
command_install "$kind"
;;
load )
command_load "$kind"
;;
run )
command_run "$kind"
;;
versions )
command_versions "$kind"
;;
* )
err "unreachable\n"
;;
esac
}
main "$@"