Files
strans/bench.sh

111 lines
2.1 KiB
Bash
Executable File

#!/bin/sh
set -eu
cd "$(dirname "$0")" || exit 1
case ${XDG_RUNTIME_DIR-} in
/) ipc=/strans.sock ;;
/*/) ipc=${XDG_RUNTIME_DIR}strans.sock ;;
/*) ipc=${XDG_RUNTIME_DIR}/strans.sock ;;
*) ipc=/tmp/strans.$(id -u) ;;
esac
if test -n "${XDG_CONFIG_HOME-}"; then
busdir=$XDG_CONFIG_HOME/ibus/bus
elif test "${HOME+x}" = x; then
busdir=$HOME/.config/ibus/bus
else
echo "bench.sh: HOME or XDG_CONFIG_HOME is required for IBus discovery" >&2
exit 1
fi
strans_pid=
perf_pid=
cleanup()
{
status=$?
trap - 0 1 2 15
if test -n "$perf_pid"; then
kill -INT "$perf_pid" 2>/dev/null || :
wait "$perf_pid" 2>/dev/null || :
perf_pid=
fi
if test -n "$strans_pid"; then
kill "$strans_pid" 2>/dev/null || :
wait "$strans_pid" 2>/dev/null || :
strans_pid=
fi
exit "$status"
}
ibusready()
{
for address in "$busdir"/*; do
test -f "$address" || continue
foundpid=
foundaddress=
while IFS= read -r line; do
case $line in
IBUS_DAEMON_PID="$strans_pid") foundpid=1 ;;
IBUS_ADDRESS=unix:abstract=strans-"$strans_pid",*) foundaddress=1 ;;
esac
done <"$address"
if test -n "$foundpid" && test -n "$foundaddress"; then
return 0
fi
done
return 1
}
trap cleanup 0
trap 'exit 129' 1
trap 'exit 130' 2
trap 'exit 143' 15
./strans map "$@" &
strans_pid=$!
attempt=0
while test "$attempt" -lt 10; do
if test -S "$ipc" && ibusready; then
break
fi
if ! kill -0 "$strans_pid" 2>/dev/null; then
if wait "$strans_pid"; then
status=0
else
status=$?
fi
strans_pid=
if test "$status" -eq 0; then
status=1
fi
echo "bench.sh: daemon exited before publishing IPC and IBus endpoints" >&2
exit "$status"
fi
attempt=$((attempt + 1))
if test "$attempt" -lt 10; then
sleep 1
fi
done
if test "$attempt" -eq 10; then
echo "bench.sh: daemon did not publish IPC and IBus endpoints within 10 seconds" >&2
exit 1
fi
# warm up the renderer
./bench/bench bench/bench.keys 100
echo "renderer warmed up"
perf record -g -o bench/perf.data -p "$strans_pid" &
perf_pid=$!
sleep 1
kill -0 "$perf_pid"
./bench/bench bench/bench.keys 100
kill -INT "$perf_pid"
wait "$perf_pid" 2>/dev/null || :
perf_pid=