Skip to content

Instantly share code, notes, and snippets.

@williamcaban
Created September 17, 2021 12:34
Show Gist options
  • Save williamcaban/d37b9a89481c63e728793c56039b1816 to your computer and use it in GitHub Desktop.
Save williamcaban/d37b9a89481c63e728793c56039b1816 to your computer and use it in GitHub Desktop.
Script to test latency and speed towards OpenShift Kubernetes API Server
# If $USERNAME and $PASSWORD are not defined
# in the environment prompt for them
if [ -z "${USERNAME+x}" ]; then
read -p 'Username: ' USERNAME
fi
if [ -z "${PASSWORD+x}" ]; then
read -sp 'Password: ' PASSWORD
fi
echo -e "\nAttempting to find the K8s API Server and token"
# K8s server API
K8S_API=$( oc whoami --show-server )
# Try to get token from
TOKEN=$( oc whoami --show-token )
# If using kubeadmin default cert authentication we need differnet way to obtain token
if [ $TOKEN == ""]; then
echo "Obtaining token by authentication instead"
# Identify API endpoint for the OAUTH authorization server
AUTH_ENDPOINT=$( oc get --raw '/.well-known/oauth-authorization-server' | jq -r .authorization_endpoint )
# Do user/pass authentication to obtain token
URL_WTOKEN=$( curl -ks -u $USERNAME:$PASSWORD -o /dev/null -w "%{redirect_url}\n" "$AUTH_ENDPOINT?client_id=openshift-challenging-client&response_type=token" )
# extract token from URL
TOKEN=$( echo $URL_WTOKEN | sed "s/^.*access_token=//" | sed "s/&.*//" )
fi
for a in {1..100}; do
# endpoint to test -- Update as needed
TEST_ENDPOINT="$K8S_API/api/v1/nodes"
#
curl -k -H "Authorization: Bearer $TOKEN" -so /dev/null \
-w "HTTP:%{http_code} DNS:%{time_namelookup}s CONNECT:%{time_connect}s START:%{time_starttransfer}s TOTAL:%{time_total}s SPEED:%{speed_download}Bps\n" \
"$TEST_ENDPOINT";
sleep 1 ; done
@jrosal06
Copy link

jrosal06 commented Nov 9, 2021

Hi, this script looks good. Excuse the question, but how do I interpret the following result:

HTTP:200 DNS:0.002376s CONNECT:0.003067s START:1.210093s TOTAL:1.218507s SPEED:314605.000Bps

I know that HTTP: 200 is fine. This protocol is up but the others I do not know how to interpret. I mean, what I understand is that from my notebook it makes queries but DNS, CONNECT, START, TOTAL and SPEED through the apiserver service. Thanks in advance.

@williamcaban
Copy link
Author

williamcaban commented Nov 10, 2021

The results are like this: <http-code>,<how long it took to do the dns resolution>,<how long it took to establish the connection>,<after the connection was stablished when it started the transfer>,<total time to complete transfer>,<speed in Bps experience during the transfer>

@williamcaban
Copy link
Author

For more details refer to the variables available for the -w of the curl command in the man page https://curl.se/docs/manpage.html

@jrosal06
Copy link

Amazing! Thanks a lot for you answer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment