# Shared helpers of the experiment scripts; sourced, not run. # The generator and the knobs the experiments vary live in compose.bench.yaml. export COMPOSE_PATH_SEPARATOR=: COMPOSE_FILE=compose.yaml:compose.bench.yaml # The balancer logs errors and state changes only, not every request. export LB_QUIET=1 # abspath prints a directory in the form the Docker CLI mounts (a Windows path # under Git Bash). abspath() { (cd "$1" && (pwd -W 2>/dev/null || pwd)) } # meta writes what ran, when, on which commit and host: file, then key=value. meta() { file=$1 shift { printf '{\n' printf ' "date_utc": "%s",\n' "$(date -u +%Y-%m-%dT%H:%M:%SZ)" printf ' "commit": "%s",\n' "$(git rev-parse --short HEAD)" printf ' "branch": "%s",\n' "$(git branch --show-current)" printf ' "docker": "%s",\n' "$(docker version --format '{{.Server.Version}}')" printf ' "host_cpus": "%s",\n' "$(docker info --format '{{.NCPU}}')" printf ' "host_memory_bytes": "%s",\n' "$(docker info --format '{{.MemTotal}}')" for pair in "$@"; do printf ' "%s": "%s",\n' "${pair%%=*}" "${pair#*=}" done printf ' "go": "%s"\n' "$(go env GOVERSION 2>/dev/null || echo unknown)" printf '}\n' } >"$file" } # host_probe prints the seconds a fixed CPU loop takes in a one-CPU container: # the same work every time, so a slow spell of the host shows in the results. host_probe() { docker run --rm --cpus 1 --entrypoint sh mtusi-messenger:lab5 \ -c 'time -p awk "BEGIN{for(i=0;i<5000000;i++)s+=i}"' 2>&1 | awk '/^real/ { print $2 }' } # state_log keeps from `compose logs` (stdin) what explains a run: the # balancer's server state changes and the replicas' start and stop. An # overloaded run logs every failed request, gigabytes over a series; those # lines are only counted. state_log() { awk '/is DOWN|is UP|UP\/READY|maintenance|"msg":"(starting|shutdown started|shutdown completed)"/ { print; next } { dropped++ } END { printf "# %d other lines dropped\n", dropped }' } # sample_docker_stats appends the CPU and memory of every container until killed. sample_docker_stats() { while :; do docker stats --no-stream --format "{\"t\":$(date +%s),\"name\":\"{{.Name}}\",\"cpu\":\"{{.CPUPerc}}\",\"mem\":\"{{.MemUsage}}\"}" \ $(docker ps -q --filter label=com.docker.compose.project=lab5) 2>/dev/null || true done >>"$1" } # sample_postgres appends, every second, the database counters and what the # active sessions wait for ("cpu": nothing). sample_postgres() { while :; do docker compose exec -T postgres psql -U messenger -d messenger -XAtq -c " SELECT json_build_object( 't', extract(epoch FROM now()), 'xact_commit', xact_commit, 'xact_rollback', xact_rollback, 'tup_inserted', tup_inserted, 'blks_read', blks_read, 'blks_hit', blks_hit, 'sessions', sessions, 'sessions_abandoned', sessions_abandoned, 'active', (SELECT count(*) FROM pg_stat_activity WHERE state = 'active' AND backend_type = 'client backend' AND pid <> pg_backend_pid()), 'waits', (SELECT coalesce(json_object_agg(w, n), '{}') FROM ( SELECT coalesce(wait_event_type || ':' || wait_event, 'cpu') AS w, count(*) AS n FROM pg_stat_activity WHERE state = 'active' AND backend_type = 'client backend' AND pid <> pg_backend_pid() GROUP BY 1) AS x)) FROM pg_stat_database WHERE datname = 'messenger'" 2>/dev/null || true sleep 1 done >>"$1" }