Skip to content

Instantly share code, notes, and snippets.

@thecodeassassin
Last active October 28, 2020 14:36
Show Gist options
  • Save thecodeassassin/a9b1367eee8ddbf3e343c7c22fdb8887 to your computer and use it in GitHub Desktop.
Save thecodeassassin/a9b1367eee8ddbf3e343c7c22fdb8887 to your computer and use it in GitHub Desktop.
Test script to compare origins / CDNs for HLS streams
#!/bin/bash
export path=$1
export quality=$2
export base_url_1=https://some-base.url
export base_url_2=https://some-base.url
rand_file=$(openssl rand -hex 6)
current_segment=0
fetch_playlist() {
playlist_url="$1/${path}/${quality}/playlist.m3u8"
segments_url="$1/${path}/${quality}"
echo "=> Starting test [$1]"
echo "=> Using playlist: ${playlist_url}"
if [ ! $? = 0 ]; then
echo "=> Forced exit: Cannot curl ${playlist}"
exit 1
fi
local count=0
local sum='0.000'
until [ $count -eq 20 ]; do
playlist=$(curl -s -D /tmp/${rand_file}_headers ${playlist_url})
last_segment=$(echo ${playlist} | tail -n 1 | sed -n 's/.*\_\([0-9]*\)\.ts/\1/p')
if [[ $last_segment == *ENDLIST* ]]; then
echo "===> Fatal: Playlist not live!"
exit 1
fi
echo "=> Last segment: ${last_segment}"
req_time_r=$(curl -o /dev/null -s -w '%{time_total}\n' ${segments_url}/${last_segment}.ts)
req_time=$(printf '%0.3f' $req_time_r)
if [ -z "${last_segment}" ]; then
echo "---> last headers: $(cat /tmp/${rand_file}_headers)"
echo "===> Fatal: Playlist not live!"
exit 1
fi
if [ "${current_segment}" -gt "${last_segment}" ]; then
echo "===> WARNING!!! incorrect playlist detected!!! Previous segment: ${current_segment} is higher than ${last_segment}"
fi
let count+=1
echo "==> Request ${count}/20"
echo "==> Total transfer time: ${req_time}"
sum=`echo | awk " { printf \"%0.2f\n\", (${sum} + ${req_time}); } "`
sleep 2
done
avg=`echo "$sum / $count" | bc -l`
printf "\n\nAverage response time ${1}: %0.3f s" "$avg"
}
fetch_playlist $base_url_1 &
fetch_playlist $base_url_2 &
wait
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment