Skip to content

Instantly share code, notes, and snippets.

@psalz
Created July 11, 2025 07:47
Show Gist options
  • Select an option

  • Save psalz/3e947532a0cb042362414cb851ed9f63 to your computer and use it in GitHub Desktop.

Select an option

Save psalz/3e947532a0cb042362414cb851ed9f63 to your computer and use it in GitHub Desktop.
Measure TCP throughput between a client and server
#!/bin/sh
set -o errexit -o nounset -o noclobber
usage() {
echo 1>&2 "Intercept TCP communication between a client and server to measure throughput"
echo 1>&2 "Usage: $0 <MODE> <LISTEN_PORT> <CONNECT_PORT>"
echo 1>&2 "Where MODE can either be UP or DOWN, for measuring upstream throughput (client to server) or downstream throughput (server to client)"
exit 1
}
MODE=$1; shift || usage
if [ "$MODE" != "UP" ] && [ "$MODE" != "DOWN" ]; then usage; fi
if [ $# -ne 2 ]; then usage; fi
if [ "$1" = "--connect" ]; then
shift;
CONNECT_PORT=$1; shift
if [ "$MODE" = "UP" ]; then
pv -brt | socat - TCP:localhost:"$CONNECT_PORT"
else
socat TCP:localhost:"$CONNECT_PORT" - | pv -brt
fi
else
LISTEN_PORT=$1; shift
CONNECT_PORT=$1; shift
echo "Listening for connections on port $LISTEN_PORT, forwarding to port $CONNECT_PORT"
socat TCP-LISTEN:"$LISTEN_PORT" SYSTEM:"sh $0 $MODE --connect $CONNECT_PORT"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment