#!/usr/bin/env bash # # measure-rejected-pushes.sh: several agents push to one branch at the same moment. # Counts the pushes that were rejected and how long it took until every change was on the branch. # # Usage: bash measure-rejected-pushes.sh [agents] (agents: 2 to 50, default 5) # Needs bash 5 or newer and git 2.23 or newer. Use a repository you can push to. # # It talks only to the repository you give it, with your own git and your own sign-in. # It sends nothing anywhere else. It works on a throwaway branch (never main) and deletes # that branch and its temporary folder at the end, also when it stops early. set -euo pipefail die() { printf 'measure: %s\n' "$*" >&2; exit 2; } [ -n "${EPOCHREALTIME:-}" ] || die "needs bash 5 or newer (on macOS: brew install bash, then run it with that bash)" [ $# -ge 1 ] && [ -n "$1" ] || die "usage: bash measure-rejected-pushes.sh [agents]" URL="$1"; N="${2:-5}" [[ "$N" =~ ^[0-9]+$ ]] && [ "$N" -ge 2 ] && [ "$N" -le 50 ] || die "agents must be a number from 2 to 50" command -v git >/dev/null || die "git is not installed" WORK="$(mktemp -d)"; BR="measure-race-$$-$RANDOM"; PUSHED=0 cleanup() { local pids pids="$(jobs -p)"; [ -z "$pids" ] || kill $pids 2>/dev/null || true if [ "$PUSHED" = 1 ]; then git -C "$WORK/base" push -q origin --delete "$BR" 2>/dev/null || echo "measure: could not delete the branch $BR; delete it by hand" >&2; fi rm -rf "$WORK" } trap cleanup EXIT trap 'exit 130' INT TERM echo "Copying $URL to a temporary folder..." git clone -q "$URL" "$WORK/base" git -C "$WORK/base" switch -q -c "$BR" START_COMMIT="$(git -C "$WORK/base" rev-parse HEAD)" git -C "$WORK/base" push -q -u origin "$BR"; PUSHED=1 for i in $(seq 1 "$N"); do git clone -q --reference "$WORK/base" --branch "$BR" "$URL" "$WORK/a$i" git -C "$WORK/a$i" config user.name "agent $i" git -C "$WORK/a$i" config user.email "agent-$i@example.invalid" git -C "$WORK/a$i" config core.autocrlf false echo "agent $i" > "$WORK/a$i/agent-$i.txt" git -C "$WORK/a$i" add "agent-$i.txt" git -C "$WORK/a$i" commit -q -m "agent $i: add agent-$i.txt" done # One agent: push; when the push is rejected, catch up (pull, rebase) and push again. agent() { local i="$1" rejected=0 until git -C "$WORK/a$i" push -q origin "HEAD:$BR" 2>"$WORK/push-error-$i"; do rejected=$((rejected + 1)) if [ "$rejected" -gt $((N + 5)) ]; then echo "measure: agent $i gave up after $rejected rejected pushes:" >&2; cat "$WORK/push-error-$i" >&2; return 1 fi git -C "$WORK/a$i" pull -q --rebase origin "$BR" done echo "$rejected" > "$WORK/rejected-$i" } echo "$N agents push at the same moment..." start=$EPOCHREALTIME pids=() for i in $(seq 1 "$N"); do agent "$i" & pids+=("$!"); done failed=0 for p in "${pids[@]}"; do wait "$p" || failed=1; done end=$EPOCHREALTIME [ "$failed" = 0 ] || die "an agent could not push; nothing was measured" total=$(cat "$WORK"/rejected-* | awk '{s += $1} END {print s}') git -C "$WORK/base" fetch -q origin "$BR" landed=$(git -C "$WORK/base" log --oneline "$START_COMMIT..FETCH_HEAD" | grep -c "agent [0-9]*: add" || true) awk -v n="$N" -v r="$total" -v l="$landed" -v a="$start" -v b="$end" 'BEGIN { printf "%d agents pushed at the same moment: %d rejected pushes, %d of %d changes on the branch, %.2f s.\n", n, r, l, n, b - a printf "agents=%d rejected_pushes=%d changes_on_branch=%d seconds=%.2f\n", n, r, l, b - a }'