#!/usr/bin/env bash # Start / probe / stop helpers shared by the measurement scripts. source "$(dirname "${BASH_SOURCE[0]}")/env.sh" PORT=18110 BODY='{"customer":"asha@example.com","sku":"ABC-1234","quantity":3}' now_ms() { echo $(( $(date +%s%N) / 1000000 )); } # start_app WORKDIR LOGFILE cmd args... -> sets APP_PID (the java/native process itself, thanks to exec) start_app() { local wd=$1 log=$2; shift 2 ( cd "$wd" && exec "$@" ) > "$log" 2>&1 & APP_PID=$! } # wait_ready -> 0 when GET /ping answers 200 within 60 s wait_ready() { local i for i in $(seq 1 3000); do curl -sf -o /dev/null --max-time 2 "localhost:$PORT/ping" && return 0 kill -0 "$APP_PID" 2>/dev/null || return 1 sleep 0.02 done return 1 } stop_app() { kill "$APP_PID" 2>/dev/null wait "$APP_PID" 2>/dev/null return 0 } # ms_of SECONDS -> milliseconds with one decimal ms_of() { awk -v s="$1" 'BEGIN{printf "%.1f", s*1000}'; } # post_order -> prints curl's time_total in ms post_order() { ms_of "$(curl -s -o /dev/null -w '%{time_total}' -H 'Content-Type: application/json' -d "$BODY" "localhost:$PORT/orders")" } get_order() { ms_of "$(curl -s -o /dev/null -w '%{time_total}' "localhost:$PORT/orders/1")"; } rss_mb() { awk '/VmRSS/ {printf "%d", $2/1024}' "/proc/$APP_PID/status"; } median() { sort -n | awk '{a[NR]=$1} END{ if (NR==0) {print "n/a"; exit} if (NR%2) printf "%s", a[(NR+1)/2]; else printf "%.1f", (a[NR/2]+a[NR/2+1])/2 }'; } minmax() { sort -n | awk 'NR==1{lo=$1} {hi=$1} END{printf "%s..%s", lo, hi}'; }