Skip to content

Commit 72aad08

Browse files
committed
Add plug loading time telemetry
This change adds loading time telemetry to `plug` to print out duration in support of troubleshooting. This change address zap-zsh#189
1 parent d8e74d3 commit 72aad08

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,8 @@ When not currently active, execute the following:
138138
```zsh
139139
rm -rf "${XDG_DATA_HOME:-$HOME/.local/share}/zap"
140140
```
141+
## TROUBLESHOOTING
142+
If you are attempting to troubleshoot slow shell loading that may be due to slow plugins, add `export zap_print_times=1` to your zshrc file before you call `plug` and it will print out the load time for each plugin or sourced file.
141143

142144
## Notes
143145

zap.zsh

+36-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,36 @@ fpath+="$ZAP_DIR/completion"
88

99
function plug() {
1010

11+
function print_elapsed() {
12+
if [[ -v zap_print_times && $timer ]]; then
13+
local now=$(date +%s%3)
14+
local d_ms=$(($now-$timer))
15+
local d_s=$((d_ms / 1000))
16+
local ms=$((d_ms % 1000))
17+
local s=$((d_s % 60))
18+
local m=$(((d_s / 60) % 60))
19+
local h=$((d_s / 3600))
20+
21+
if ((h > 0)); then
22+
elapsed=${h}h${m}m
23+
elif ((m > 0)); then
24+
elapsed=${m}m${s}s
25+
elif ((s >= 10)); then
26+
elapsed=${s}.$((ms / 100))s
27+
elif ((s > 0)); then
28+
elapsed=${s}.$((ms / 10))s
29+
else
30+
elapsed=${ms}ms
31+
fi
32+
if [[ -v plugin_name ]]; then
33+
echo "Loading $plugin_name, Execution time: $elapsed"
34+
else
35+
echo "Loading $plugin_absolute, Execution time: $elapsed"
36+
fi
37+
unset timer
38+
fi
39+
}
40+
1141
function _try_source() {
1242
if [[ "$plugin_name" == "*" ]]; then
1343
# Treat * as a glob pattern
@@ -27,6 +57,10 @@ function plug() {
2757
fi
2858
}
2959

60+
# Start timer for tracking execution time
61+
typeset -g timer=$(date +%s%3)
62+
63+
3064
# If the absolute is a directory then source as a local plugin
3165
pushd -q "$ZAP_DIR"
3266
local plugin_absolute="${1:A}"
@@ -42,6 +76,7 @@ function plug() {
4276
# If the basename directory exists, then local source only
4377
if [ -d "${plugin_absolute:h}" ]; then
4478
[[ -f "${plugin_absolute}" ]] && source "${plugin_absolute}"
79+
print_elapsed
4580
return
4681
fi
4782

@@ -61,7 +96,7 @@ function plug() {
6196
git -C "$plugin_dir" pull --unshallow > /dev/null 2>&1
6297
git -C "$plugin_dir" checkout "$git_ref" > /dev/null 2>&1 || { echo "❌ Failed to checkout $git_ref"; return 13 }
6398
}
64-
_try_source && { ZAP_INSTALLED_PLUGINS+="$plugin_name" && return 0 } || echo "$plugin_name not activated" && return 1
99+
_try_source && print_elapsed && { ZAP_INSTALLED_PLUGINS+="$plugin_name" && return 0 } || echo "$plugin_name not activated" && return 1
65100
}
66101

67102
function _pull() {

0 commit comments

Comments
 (0)