Skip to content

Instantly share code, notes, and snippets.

@alichtman
Created July 4, 2026 00:54
Show Gist options
  • Select an option

  • Save alichtman/5accff15ff203591c940a5dc86894197 to your computer and use it in GitHub Desktop.

Select an option

Save alichtman/5accff15ff203591c940a5dc86894197 to your computer and use it in GitHub Desktop.
Benchmark: jrnl CLI startup cost of top-level vs lazy encryption imports (jrnl PR #1)
#!/usr/bin/env bash
# Measures the CLI startup cost added by jrnl/encryption/__init__.py's switch
# from lazy (importlib) imports to top-level imports of the encryption
# classes. See PR #1 review thread on jrnl/encryption/__init__.py.
#
# Compares:
# BASELINE_COMMIT - last commit before the top-level-import refactor
# HEAD - current working tree
#
# Usage: scripts/benchmark_encryption_import_cost.sh [num_trials]
set -euo pipefail
REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
BASELINE_COMMIT="1691013" # "encryption: simplify __init__.py to use direct imports" (parent, lazy imports)
TRIALS="${1:-10}"
SCRATCH_DIR="$(mktemp -d)"
WORKTREE_DIR="$SCRATCH_DIR/baseline-worktree"
VENV_DIR="$SCRATCH_DIR/venv"
cleanup() {
git -C "$REPO_ROOT" worktree remove "$WORKTREE_DIR" --force >/dev/null 2>&1 || true
rm -rf "$SCRATCH_DIR"
}
trap cleanup EXIT
echo "Setting up isolated venv and baseline worktree at $BASELINE_COMMIT..."
python3 -m venv "$VENV_DIR"
# shellcheck disable=SC1091
source "$VENV_DIR/bin/activate"
pip install -q --upgrade pip
pip install -q -e "$REPO_ROOT"
git -C "$REPO_ROOT" worktree add "$WORKTREE_DIR" "$BASELINE_COMMIT" >/dev/null
time_import() {
local dir="$1"
( cd "$dir" && PYTHONPATH="$dir" python3 -c "
import time
t0 = time.perf_counter()
import jrnl.main
print('%.2f' % ((time.perf_counter() - t0) * 1000))
"
)
}
time_cli_help() {
local dir="$1"
( cd "$dir" && PYTHONPATH="$dir" python3 -c "
import subprocess, time, sys
times = []
for _ in range($TRIALS):
t0 = time.perf_counter()
subprocess.run([sys.executable, '-m', 'jrnl', '--help'],
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
times.append((time.perf_counter() - t0) * 1000)
avg = sum(times) / len(times)
print('%.1f' % avg)
"
)
}
modules_loaded() {
local dir="$1"
( cd "$dir" && PYTHONPATH="$dir" python3 -c "
import sys
import jrnl.main
print('cryptography=%s keyring=%s' % ('cryptography' in sys.modules, 'keyring' in sys.modules))
"
)
}
echo
echo "=== import jrnl.main (single-shot, ms) ==="
echo "HEAD (this branch): $(time_import "$REPO_ROOT") ms"
echo "baseline ($BASELINE_COMMIT): $(time_import "$WORKTREE_DIR") ms"
echo
echo "=== python -m jrnl --help, avg over $TRIALS runs (ms) ==="
echo "HEAD (this branch): $(time_cli_help "$REPO_ROOT") ms"
echo "baseline ($BASELINE_COMMIT): $(time_cli_help "$WORKTREE_DIR") ms"
echo
echo "=== modules eagerly loaded by 'import jrnl.main' ==="
echo "HEAD (this branch): $(modules_loaded "$REPO_ROOT")"
echo "baseline ($BASELINE_COMMIT): $(modules_loaded "$WORKTREE_DIR")"
echo
echo "=== import time breakdown for HEAD (jrnl.encryption cumulative cost) ==="
( cd "$REPO_ROOT" && python3 -X importtime -c "import jrnl.main" 2>&1 | grep -E "cryptography$|keyring$|jrnl.encryption$" )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment