Skip to content

Instantly share code, notes, and snippets.

@kirklewis
Last active March 22, 2021 14:48
Show Gist options
  • Save kirklewis/3fe1c8db547752743d862c4c52b29334 to your computer and use it in GitHub Desktop.
Save kirklewis/3fe1c8db547752743d862c4c52b29334 to your computer and use it in GitHub Desktop.
Generate and send StatsD test metrics
#!/bin/bash
DEFAULT='\033[0m'
HI_CYAN='\033[0;96m'
HI_WHITE='\033[0;93m'
STATSD_HOST=127.0.0.1
STATSD_PORT=8125
api_names=(orders invoice user)
max_index=$((${#api_names[@]} - 1))
num_metrics_per_second=4 # per second
requests_count=0
# send StatsD metrics over UDP localhost port 8125
while true;
do
# get a random api name and milliseconds between 50-150
rand_index=$(shuf -i 0-$max_index -n 1)
rand_ms=$(shuf -i 50-150 -n 1)
rand_api_name=${api_names[$rand_index]}
# build metric - format: test_api_call.[api_name].timer.[api_endpoint]
metric="test_api_call.$rand_api_name-api.timer./v1/$rand_api_name:$rand_ms|ms"
# send metric
echo $metric | nc -w 0 -u $STATSD_HOST $STATSD_PORT
# view generated metric
printf "${HI_WHITE}echo ${HI_CYAN}$metric${DEFAULT} - ${rand_ms}ms\n"
# control how many metric lines are emitted per second
requests_count=$(($requests_count+1))
if [ $requests_count == $num_metrics_per_second ]; then
echo "sent $num_metrics_per_second metrics - $(date +'%T')"
requests_count=0
sleep 1
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment