Skip to content

Commit a2f609c

Browse files
Cache java cli.sh help probes to cut JVM startup overhead
Running 'java -jar cmdline.jar help' on every encrypt/decrypt paid 150-500ms of JVM startup per probe (kas-allowlist and --verbose checks). Add a jar_help() helper that caches help output to a tmpfile keyed by the jar's mtime, and discards stderr to keep JVM warnings out of test logs.
1 parent 467c3c6 commit a2f609c

1 file changed

Lines changed: 21 additions & 2 deletions

File tree

xtest/sdk/java/cli.sh

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,25 @@ else
4242
exit 1
4343
fi
4444

45+
# Cache `java -jar cmdline.jar help [...]` output to avoid paying JVM startup
46+
# (typically 150-500ms) for the capability probes on every encrypt/decrypt.
47+
# Keyed by the jar's mtime so a reinstall invalidates the cache. stderr is
48+
# discarded to keep JVM warnings (reflective-access, agent notices) out of logs.
49+
jar_help() {
50+
local jar="$SCRIPT_DIR/cmdline.jar"
51+
local mtime
52+
mtime=$(stat -c %Y "$jar" 2>/dev/null || stat -f %m "$jar" 2>/dev/null || echo 0)
53+
local key
54+
key=$(printf '%s' "$*" | tr -c 'a-zA-Z0-9' '_')
55+
local uid
56+
uid=$(id -u 2>/dev/null || echo default)
57+
local cache="${TMPDIR:-/tmp}/xtest-java-help-${uid}-${mtime}-${key}"
58+
if [ ! -f "$cache" ]; then
59+
java -jar "$jar" help "$@" >"$cache" 2>/dev/null
60+
fi
61+
cat "$cache"
62+
}
63+
4564
if [ "$1" == "supports" ]; then
4665
case "$2" in
4766
autoconfigure | ns_grants)
@@ -130,7 +149,7 @@ args=(
130149
)
131150

132151
# when we added support for KAS allowlist, we changed the platform endpoint format to require scheme
133-
if java -jar "$SCRIPT_DIR"/cmdline.jar help decrypt | grep kas-allowlist; then
152+
if jar_help decrypt | grep -q kas-allowlist; then
134153
args+=("--platform-endpoint=$PLATFORMURL")
135154
else
136155
args+=("--platform-endpoint=$PLATFORMENDPOINT")
@@ -192,7 +211,7 @@ if [ -n "$XT_WITH_TARGET_MODE" ]; then
192211
args+=(--with-target-mode "$XT_WITH_TARGET_MODE")
193212
fi
194213

195-
if java -jar "$SCRIPT_DIR"/cmdline.jar help | grep -q -- '--verbose'; then
214+
if jar_help | grep -q -- '--verbose'; then
196215
args+=(--verbose)
197216
fi
198217

0 commit comments

Comments
 (0)