Search by labels

Sunday, January 3, 2021

Automated Test for Measuring a Website's Performance using Google PageSpeed Insights

PageSpeed Insights and Lighthouse are two free Google tools that we can use to measure a website's performance. Although with a common purpose, they are different by the fact that Lighthouse uses lab data only and measures more than performance data, while PageSpeed Insights focuses on performance metrics by analysing both lab and real-world data (the lab data being actually provided by Lighthouse).

This article will present an automated test built with Maven and two Java libraries - JUnit and RestAssured, that audits the performance of websites by using the features of the PageSpeed Insights tool.

First of all, PageSpeed Insights can also be used manually, simply go to its' homepage and check the metrics for your desired website:








The scores come also with a detailed explanation, a summary being:


The metrics scores and the perf score are colored according to these ranges:
  • 0 to 49 (red): Poor
  • 50 to 89 (orange): Needs Improvement
  • 90 to 100 (green): Good
To provide a good user experience, sites should strive to have a good score (90-100). A "perfect" score of 100 is extremely challenging to achieve and not expected. For example, taking a score from 99 to 100 needs about the same amount of metric improvement that would take a 90 to 94.


For the automated test we will use the PageSpeed Insights API which returns the audit results as a JSON object. To check it, simply: 

curl https://www.googleapis.com/pagespeedonline/v5/runPagespeed?url=${desiredWebsite}

With the general performance score being displayed in the categories section of the returned JSON:
"categories": {
      "performance": {
        "id": "performance",
        "title": "Performance",
        "score": 0.85

The implementation of the automated test can be found here, with:

  • JUnit being used to execute the tests, perform the @before and @after steps and load the websites list as a .csv file;
  • RestAssured performing the GET request and parsing the returned JSON;
  • And the actual test method asserting that the performance score is greater than or equal to 90 being:

@ParameterizedTest
@CsvFileSource(resources = "/pagespeedonline/websites.csv")
void testWebsite(String website) {
    RestAssured.baseURI = "https://www.googleapis.com";
    Double performanceScore;
    Response response = given().log().uri().
    when().get("/pagespeedonline/v5/runPagespeed?url=" + website).
    then().extract().response();
    assertThat(website, response.getStatusCode(), equalTo(200));
    performanceScore = Double.valueOf(response.path("lighthouseResult.categories.performance.score").toString());
    websiteScores.put(website, performanceScore);
    assertThat(performanceScore, greaterThanOrEqualTo(0.9));
}

Being a Java project built with Maven, this test can be easily added in a Jenkins job and executed periodically as a performance healthcheck.

No comments:

Post a Comment