Skip to content

Instantly share code, notes, and snippets.

@rolandkakonyi
Last active November 4, 2022 10:12
Show Gist options
  • Save rolandkakonyi/e06727b95d666aaa53b97891d3a82d91 to your computer and use it in GitHub Desktop.
Save rolandkakonyi/e06727b95d666aaa53b97891d3a82d91 to your computer and use it in GitHub Desktop.
#!/bin/bash
if ! [ -x "$(command -v gdate)" ]; then
echo 'Error: git is not installed. Run "brew install coreutils"' >&2
exit 1
fi
if ! [ -x "$(command -v gh)" ]; then
echo 'Error: git is not installed. Run "brew install gh"' >&2
exit 1
fi
[ "$DEBUG" = "1" ] && set -x
set -e
set -o pipefail
export GH_PAGER="less -FX"
WORKFLOW_ID="$1"
LIMIT=${2:-100}
OWNER="$(gh repo view --json owner -q ".owner.login")"
REPO="$(gh repo view --json name -q ".name")"
# GitHub CLI api
# https://cli.github.com/manual/gh_api
echo "Downloading list of the last ${LIMIT} workflow runs for '${WORKFLOW_ID}'..."
IFS=$'\n' read -r -d '' -a RUN_IDS < <( gh api \
-H "Accept: application/vnd.github+json" \
"/repos/${OWNER}/${REPO}/actions/workflows/${WORKFLOW_ID}/runs?status=success&per_page=${LIMIT}" \
-q ".workflow_runs[].id" && printf '\0')
echo "Found ${#RUN_IDS[@]} runs to check."
mkdir -p logs
for RUN_ID in "${RUN_IDS[@]}"
do
if [ -d "logs/${RUN_ID}" ]; then
echo "Logs for Run with ID '${RUN_ID}' are already downloaded. Skipping download."
else
echo "Downloading logs for Run with ID '${RUN_ID}'..."
gh api \
-H "Accept: application/vnd.github+json" \
"/repos/${OWNER}/${REPO}/actions/runs/${RUN_ID}/logs" > "logs/$RUN_ID.zip"
unzip "logs/$RUN_ID.zip" -d "logs/$RUN_ID" && rm "logs/$RUN_ID.zip"
echo "Logs are downloaded."
fi
done
echo "Analyze logs..."
IFS=$'\n' read -r -d '' -a FILES_TO_ANALYZE < <( find logs -type f -print0 | xargs -0 grep -l "Job is about to start running on the runner: ios-mac" && printf '\0' )
TOTAL_WAIT_DIFF=0
MAX_DIFF=0
echo "Found ${#FILES_TO_ANALYZE[@]} files to analyze."
for LOG_FILE in "${FILES_TO_ANALYZE[@]}"
do
WAIT_START=$(grep -m 1 "Waiting for a runner to pick up this job..." "$LOG_FILE" | awk '{print $1}')
# WAIT_END=$(grep "Job is about to start running on the runner" "$LOG_FILE" | awk '{print $1}')
WAIT_END=$(grep -m 1 "Current runner version" "$LOG_FILE" | awk '{print $1}')
WAIT_START_TIMESTAMP=$(gdate -d "$WAIT_START" +%s%3N) # in milliseconds
WAIT_END_TIMESTAMP=$(gdate -d "$WAIT_END" +%s%3N) # in milliseconds
WAIT_DIFF=$((WAIT_END_TIMESTAMP - WAIT_START_TIMESTAMP))
TOTAL_WAIT_DIFF=$((TOTAL_WAIT_DIFF + WAIT_DIFF))
if [ "$WAIT_DIFF" -gt "$MAX_DIFF" ]; then
MAX_DIFF=$WAIT_DIFF
fi
done
AVG_WAIT_DIFF=$((TOTAL_WAIT_DIFF / ${#FILES_TO_ANALYZE[@]}))
printf "Results:\n"
printf "AVG WAIT DIFF (s): %s.%s\n" $((AVG_WAIT_DIFF / 1000)) $((AVG_WAIT_DIFF % 1000))
printf "MAX WAIT DIFF (s): %s.%s\n" $((MAX_DIFF / 1000)) $((MAX_DIFF % 1000))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment