WebPagetest API Tutorial with Example

What is WebPagetest?

Webpagetest is one of the most popular and free tools for measuring webpage performance. Webpagetest enables you to run web performance tests on your site from a number of different locations across the world in a number of different browsers.

How to use WebPagetest API

Enter your website and click Start Test. Next, you will see the output window like below

How to use WebPagetest API

From this page, you can find values for following parameters of your webpage

  • Load Time – Time requires to load the page
  • First Byte Time – Time when your request started to execute
  • Page Size – Total Size for your Web page
  • Requests – Number of Requests your Webpage made.

WebPagetest API

Webpagetest has 2 primary API

  1. To Run Tests – http://www.webpagetest.org/runtest.php.
  2. To Check Test Status - http://www.webpagetest.org/testStatus.php
  3. To get Test Results - http://www.webpagetest.org/testStatus.php

To Run Tests:

What is JVM (Java Virtual Machine) with Architecture JAVA Programming Tutorial

Set Parameters

  • URL – URL to be tested
  • runs – Number of test runs
  • fvonly – Set to 1 to skip the Repeat View test
  • f – Response Format. Set to XML to Request an XML response or JSON for JSON encoded response
  • k - API Key (required for public instance)

To Check Test Status

  • Once you submit a test, you will get the following XML response.

How to use WebPagetest API

How to use WebPagetest API

This information gives the test ID, the start time, the number of runs the test requested, etc.

To Check Test Results

Pass the tested to the API - http://www.webpagetest.org/xmlResult/141107_12_BXZ/

You will see the Test results in XML format like below

How to use WebPagetest API

Php Code sample to use Webpagetest API

<?php $url = <a href=http://guru99.com //url to test $api_key = ""; // your api key $webpagetest = "http://www.webpagetest.org/runtest.php?url=$url&runs=1&f=xml&k=$api_key"; $xmlres = simplexml_load_file($webpagetest); $testid = $xmlres->data->testId; echo "Test id : ".$testid." For url : ".$url; ?>
  • Now Make another call after some time to get test results
<?php $test_id = ""; $weburl = "http://www.webpagetest.org/xmlResult/$test_id/"; $xmlres = simplexml_load_file($weburl); if($xmlres){ $loadtime = ($xmlres->data->average->firstView->loadTime)/1000; echo "WebPage loadtime is : ".$loadtime; } ?>