Skip to content

Instantly share code, notes, and snippets.

@jurv
Last active November 17, 2022 09:52
Show Gist options
  • Save jurv/538249bdfd733c0c008f67119ef7aef1 to your computer and use it in GitHub Desktop.
Save jurv/538249bdfd733c0c008f67119ef7aef1 to your computer and use it in GitHub Desktop.
Bunch of tools
#!/bin/bash
# Curl a URL and display the different times of the request
curl -H 'Cache-Control: no-cache, no-store' -w @- -o /dev/null -s "$@" <<'EOF'
time_namelookup: %{time_namelookup}\n
time_connect: %{time_connect}\n
time_appconnect: %{time_appconnect}\n
time_pretransfer: %{time_pretransfer}\n
time_redirect: %{time_redirect}\n
time_starttransfer: %{time_starttransfer}\n
----------\n
time_total: %{time_total}\n
EOF
#!/bin/bash
# remove exited containers
docker ps --filter status=dead --filter status=exited -aq | xargs -r docker rm -v
# remove unused images
docker images --no-trunc | grep '<none>' | awk '{ print $3 }' | xargs -r docker rmi
# remove unused volumes (folders shared by containers)
docker volume ls -qf dangling=true | xargs -r docker volume rm
# remove networks
docker network rm $(docker network ls | grep "bridge" | awk '/ / { print $1 }')
#!/bin/bash
free_data="$(free)"
mem_data="$(echo "$free_data" | grep 'Mem:')"
free_mem="$(echo "$mem_data" | awk '{print $4}')"
buffers="$(echo "$mem_data" | awk '{print $6}')"
cache="$(echo "$mem_data" | awk '{print $7}')"
total_free=$((free_mem + buffers + cache))
used_swap="$(echo "$free_data" | grep 'Swap:' | awk '{print $3}')"
echo -e "Free memory:\t$total_free kB ($((total_free / 1024)) MB)\nUsed swap:\t$used_swap kB ($((used_swap / 1024)) MB)"
if [[ $used_swap -eq 0 ]]; then
echo "Congratulations! No swap is in use."
elif [[ $used_swap -lt $total_free ]]; then
echo "Freeing swap..."
sudo swapoff -a
sudo swapon -a
else
echo "Not enough free memory. Exiting."
exit 1
fi
#!/bin/bash
# Remove local branches that were once linked to remote branches, but the remote branches are now missing
git fetch -p && for branch in $(git for-each-ref --format '%(refname) %(upstream:track)' refs/heads | awk '$2 == "[gone]" {sub("refs/heads/", "", $1); print $1}'); do git branch -D $branch; done
#!/bin/bash
# Script comming from https://github.com/rodrigorodrigues/quarkus-vs-springboot-reactive-rest-api/blob/master/naive-stress-test.sh
#### Default Configuration
trap "kill 0" SIGINT
CONCURRENCY=1
REQUESTS=10
ADDRESS="http://localhost:8080/"
HTTP_METHOD=""
BODY=""
HEADERS=""
CURL_PARAMETERS=""
show_help() {
cat << EOF
Naive Stress Test with cURL.
Usage: ./stress-test.sh [-a ADDRESS] [-c CONCURRENCY] [-r REQUESTS] [-X HTTP_METHOD] [-d BODY] [-H HEADERS]
Params:
-a address to be tested.
Defaults to localhost:8080
-c concurrency: how many process to spawn
Defaults to 1
-r number of requests per process
Defaults to 10
-X http method
Defaults to GET
-d body
Defaults to NULL
-H headers
Defaults to NULL
-h show this help text
Example:
$ ./stress-test.sh -c 4 -p 100 (400 requests to localhost:8080)
EOF
}
#### CLI
while getopts ":a:c:r:h:X:d:H:" opt; do
case $opt in
a)
ADDRESS=$OPTARG
;;
c)
CONCURRENCY=$OPTARG
;;
r)
REQUESTS=$OPTARG
;;
X)
HTTP_METHOD=$OPTARG
CURL_PARAMETERS+=" -X $HTTP_METHOD $ADDRESS?[1-$REQUESTS]"
;;
d)
BODY=$OPTARG
CURL_PARAMETERS+=" -d '$BODY'"
;;
H)
HEADERS=$OPTARG
CURL_PARAMETERS+=" -H \"$HEADERS\" "
;;
h)
show_help
exit 0
;;
\?)
show_help >&2
echo "Invalid argument: $OPTARG" &2
exit 1
;;
esac
done
shift $((OPTIND-1))
#### Main
if [ -n "$CURL_PARAMETERS" ]; then
if [ -z "$HTTP_METHOD" ]; then
CURL_PARAMETERS="$ADDRESS?[1-$REQUESTS] $CURL_PARAMETERS"
fi
else
CURL_PARAMETERS="$ADDRESS?[1-$REQUESTS]"
fi
CURL_COMMAND="curl -s $CURL_PARAMETERS"
echo "curl command: $CURL_COMMAND"
for i in `seq 1 $CONCURRENCY`; do
if [[ "$CURL_COMMAND" == *"#INCREMENT_TIMESTAMP"* ]]; then
timestamp="$(date +%s%N)"
CURL_COMMAND="${CURL_COMMAND//#INCREMENT_TIMESTAMP/$timestamp}"
fi
eval "$CURL_COMMAND" & pidlist="$pidlist $!"
done
# Execute and wait
FAIL=0
for job in $pidlist; do
echo $job
wait $job || let "FAIL += 1"
done
# Verify if any failed
if [ "$FAIL" -eq 0 ]; then
echo "SUCCESS!"
else
echo "Failed Requests: ($FAIL)"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment