Created
October 31, 2019 23:22
-
-
Save nexus166/e54369f4f4cc24ab568e1de2ee3fb629 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| AVG_COUNT="${AVG_COUNT:-10}" | |
| FACTOR="${FACTOR:-2}" | |
| LOOPTIME="${LOOPTIME:-1}" | |
| FULL_URL="${1}" | |
| SNI="$(printf '%s' ${FULL_URL} | awk -F[/:] '{print $4}')" | |
| TARGETS=($(dig +short ${SNI})) | |
| export AVG_COUNT FACTOR LOOPTIME TARGETS | |
| set -CETbfupo pipefail | |
| function _timestamp() { | |
| date --utc +%FT%T.%3NZ | |
| } | |
| export -f _timestamp | |
| function w8() { | |
| for p in "${@}"; | |
| do | |
| wait -n "${p}" 2> /dev/null & | |
| done; | |
| wait -n | |
| } | |
| function _j() { | |
| set +u | |
| TODO="${1}"; | |
| TARGETs=(${@:2}); | |
| pids=(); | |
| for j in "${TARGETs[@]}"; | |
| do | |
| pids+=($!); | |
| eval "${TODO} ${j}" & | |
| done; | |
| w8 "${pids[@]}" | |
| set -u | |
| } | |
| function _curl() { | |
| printf '%s|%s\n' $(curl -kso /dev/null --write-out "%{time_connect}|%{time_appconnect}|%{time_pretransfer}|%{time_starttransfer}|%{time_redirect}|%{time_total}" "${1}") "${2}" | |
| } | |
| export -f _curl | |
| function _math() { | |
| printf '%s\n' "$@" | bc | |
| } | |
| function mkAvg() { | |
| local t=0; | |
| local vals=(${@}); | |
| [[ -z ${vals[@]} ]] && vals[0]=1; | |
| for v in ${vals[@]}; | |
| do | |
| t=$((t + v)); | |
| done; | |
| printf '%d' $(_math "$t / ${#vals[@]}") | |
| } | |
| function _main() { | |
| _report_file=$(mktemp); | |
| printf 'tCONNECT\ttAPPCONNECT\ttPRETX\t\ttSTARTTX\ttREDIRECT\ttTOTAL\t\tIP\t\t\t\tHIGH\n' | tee -a "${_report_file}"; | |
| trap 'printf "\n\n"; cat ${_report_file}; mv -vf ${_report_file} ./$(_timestamp).${1}.log' EXIT; | |
| avgCounter=0; | |
| while :; do | |
| _j "_curl ${1}" "${@:2}"; | |
| sleep "${LOOPTIME}"; | |
| done | while IFS="|" read CONNECT APPCONNECT PRETX STARTTX REDIR TOTAL IP; do | |
| out=$(printf '%s\t%s\t%s\t%s\t%s\t%s\t%s' "${CONNECT}" "${APPCONNECT}" "${PRETX}" "${STARTTX}" "${REDIR}" "${TOTAL}" "${IP}"); | |
| printf '%s\n' "${out}" > /dev/stderr; | |
| for val in ${!avgHT[@]}; | |
| do | |
| picoVal=$(eval echo "\${${val}//.}" | sed 's/^0*//'); | |
| picoVal="${picoVal:-0}"; | |
| ((avgCounter++)); | |
| lastX[${avgCounter}]="${picoVal}"; | |
| avgHT[$val]=$(mkAvg "${lastX[@]}"); | |
| oldAvg=$(mkAvg "${lastX[@]:$((avgCounter - 1))}"); | |
| if [[ $(_math "${picoVal} / ${FACTOR:-2}") -gt "${oldAvg}" ]] && [[ "${picoVal}" -gt $(_math "${avgHT[$val]} - ${oldAvg}") ]]; then | |
| printf '%s\t[%s]\t%d\t%s\n' "${out}" "${val}" "${picoVal}" "$(_timestamp)" | tee -a "${_report_file}"; | |
| fi; | |
| done; | |
| [[ "${avgCounter}" -gt "${AVG_COUNT}" ]] && avgCounter=0; | |
| done | |
| } | |
| export -a lastX; | |
| typeset -A avgHT; | |
| avgHT=([CONNECT]=0 [APPCONNECT]=0 [PRETX]=0 [STARTTX]=0 [REDIR]=0 [TOTAL]=0); | |
| export avgHT; | |
| #for a in ${!avgHT[@]}; do | |
| # export -a "last${a}" | |
| #done | |
| _main ${FULL_URL} ${TARGETS[@]} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment