GNU/Linux, Open Source, Cloud Computing, DevOps and more...

Script to automatically monitor website Google PageSpeed Insights score

2 comments

Logo Google Pagespeed

One of the most targeted goals when optimizing a website for page load speed is to get a good score on Google PageSpeed Insights test. But it is not enough to work hard on optimization, achieve a good score and go to sleep. It is essential to periodically monitor score changes as a website evolves and undergoes changes over time which affect this metric.

Here’s a little script that will allow you to automatically track Google PageSpeed Insight score and be alerted if it falls below a custom value.

Psi tool

The script is based on a small but very useful Javascript tool called psi developed by Addy Osmani, so you need first to install it using the Npm package manager of Node.js:

$ npm install --save psi

Once installed, this program shows on the command line the same information we can get through the official Google web application:

Salida del comando /usr/bin/psi

The desktop-pagespeed-score-watchdog.sh script

This is a small Bash script that filters the information provided by the psi command and dumps the result to a log file that records score changes over time. It can also be processed by a monitoring system which generates some kind of alert when the website’s score falls below a certain value.

#! /bin/bash 
 
scoreLimitDesktop="90"
website="www.daniloaz.com"
error=0 
 
log () 
{ 
        logFileName=`echo $0 | egrep -o "[^\/]+$"` 
        logStatusFile=$logFileName.status 
        logFile=$logFileName.log 
 
        echo "$1|$2" > /var/log/$logStatusFile 
        echo `date +%Y-%m-%d\ %H:%M:%S`"|$1|$2" >> /var/log/$logFile 
} 
 
if [ ! -f /usr/bin/psi ];then 
    echo "Error: /usr/bin/psi command is not installed." 
    exit 1 
fi 
 
scoreDesktop=`/usr/bin/psi --strategy=desktop "$website" | grep 'Speed:' | awk '{print $2}'` 
 
if [ $scoreDesktop -lt $scoreLimitDesktop ];then 
    msg="ERROR: the desktop PageSpeed score of $website fell to ${scoreDesktop}/100."  
    echo $msg 
    log 2 "$msg"
    exit 1 
else 
    msg="OK: the desktop PageSpeed score is equal to or greater than the limit: ${scoreDesktop}/100." 
    echo $msg 
    log 0 "$msg" 
    exit 0 
fi

This example script gets the score for desktop, but could equally get it for mobile by just changing the –strategy=desktop parameter by –strategy=mobile.

Resultado PageSpeed www.daniloaz.comThe script can be executed as often as you want by adding a cron task:

# Monitor every 15 minutes the www.daniloaz.com site score in Google PageSpeed Insights
0,15,30,45      *       *       *       *       /bin/bash /usr/local/scripts/desktop-pagespeed-score-watchdog.sh

Finally, this is the look of the log that periodically records the evolution of PageSpeed score:

$ cat desktop-pagespeed-score-watchdog.sh.log
2017-05-26 12:00:04|0|OK: the desktop PageSpeed score is equal to or greater than the limit: 91/100. 
2017-05-26 12:15:03|0|OK: the desktop PageSpeed score is equal to or greater than the limit: 91/100. 
2017-05-26 12:30:03|0|OK: the desktop PageSpeed score is equal to or greater than the limit: 90/100. 
2017-05-26 12:45:03|0|OK: the desktop PageSpeed score is equal to or greater than the limit: 90/100. 
2017-05-26 13:00:03|0|OK: the desktop PageSpeed score is equal to or greater than the limit: 91/100. 
2017-05-26 13:15:03|0|OK: the desktop PageSpeed score is equal to or greater than the limit: 91/100. 
2017-05-26 13:30:04|0|OK: the desktop PageSpeed score is equal to or greater than the limit: 91/100. 
2017-05-26 13:45:03|0|OK: the desktop PageSpeed score is equal to or greater than the limit: 91/100. 
2017-05-26 14:00:03|0|OK: the desktop PageSpeed score is equal to or greater than the limit: 91/100. 
2017-05-26 14:15:04|0|OK: the desktop PageSpeed score is equal to or greater than the limit: 91/100. 
2017-05-26 14:30:04|0|OK: the desktop PageSpeed score is equal to or greater than the limit: 91/100. 
2017-05-26 14:45:04|0|OK: the desktop PageSpeed score is equal to or greater than the limit: 91/100. 
2017-05-26 15:00:04|0|OK: the desktop PageSpeed score is equal to or greater than the limit: 91/100. 
2017-05-26 15:15:04|0|OK: the desktop PageSpeed score is equal to or greater than the limit: 91/100. 
2017-05-26 15:30:04|0|OK: the desktop PageSpeed score is equal to or greater than the limit: 91/100. 
2017-05-26 15:45:04|0|OK: the desktop PageSpeed score is equal to or greater than the limit: 91/100.
2017-05-26 16:00:04|2|ERROR: the desktop PageSpeed score of www.daniloaz.com fell to 89/100.
2017-05-26 16:15:03|2|ERROR: the desktop PageSpeed score of www.daniloaz.com fell to 89/100.


 

About the author

Daniel López Azaña
Freelance AWS Cloud Solution Architect & Linux Sysadmin

Entrepreneur, a generator of ideas and restless mind. Passionate about new technologies, especially Linux systems and Open Source Software. I also like to write about Technology News, Cloud Computing, AWS, DevOps, DevSecOps, System Security, Web Development and Programming, SEO, Science, Innovation, Entrepreneurship, etc.

DanielScript to automatically monitor website Google PageSpeed Insights score

Related Posts

2 comments

Join the conversation

Leave a Reply

Your email address will not be published.