11#! /bin/bash
22
33# This script prints the sccache hit rate between two calls to sccache --show-stats.
4- # It should be sourced in your script before and after the operations you want to profile,
5- # with the 'start' or 'end' argument respectively.
4+ # It must be sourced. Exits with an error if executed directly.
5+
6+ if [[ " ${BASH_SOURCE[0]} " == " ${0} " ]]; then
7+ echo " Error: ci/sccache_stats.sh must be sourced, not executed."
8+ echo " Usage: source ci/sccache_stats.sh {start|end}"
9+ exit 2
10+ fi
611
712mode=$1
813
914if [[ " $mode " != " start" && " $mode " != " end" ]]; then
1015 echo " Invalid mode: $mode "
11- echo " Usage: $0 {start|end}"
12- exit 1
16+ echo " Usage: source ${BASH_SOURCE[0]} {start|end}"
17+ return 1
1318fi
1419
1520# Check if sccache is available
1621if ! command -v sccache & > /dev/null; then
1722 echo " Notice: sccache is not available. Skipping..."
18- exit 0
23+ return 0
1924fi
2025
2126case $mode in
@@ -26,7 +31,7 @@ case $mode in
2631 end)
2732 if [[ -z ${SCCACHE_START_HITS+x} || -z ${SCCACHE_START_MISSES+x} ]]; then
2833 echo " Error: start stats not collected. Did you call this script with 'start' before your operations?"
29- exit 1
34+ return 1
3035 fi
3136
3237 final_hits=$( sccache --show-stats | awk ' /^[ \t]*Cache hits[ \t]+[0-9]+/ {print $3}' )
@@ -35,16 +40,11 @@ case $mode in
3540 misses=$(( final_misses - SCCACHE_START_MISSES))
3641 total=$(( hits + misses))
3742
38- prefix=" "
39- if [ ${GITHUB_ACTIONS:- false} = " true" ]; then
40- prefix=" ::notice::"
41- fi
42-
4343 if (( total > 0 )) ; then
4444 hit_rate=$( awk -v hits=" $hits " -v total=" $total " ' BEGIN { printf "%.2f", (hits / total) * 100 }' )
45- echo ${prefix} " sccache hits: $hits | misses: $misses | hit rate: $hit_rate %"
45+ echo " sccache hits: $hits | misses: $misses | hit rate: $hit_rate %"
4646 else
47- echo ${prefix} " sccache stats: N/A No new compilation requests"
47+ echo " sccache stats: N/A No new compilation requests"
4848 fi
4949 unset SCCACHE_START_HITS
5050 unset SCCACHE_START_MISSES
0 commit comments