Before we learn more about webpagetest API, let's understand-

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:

  • To run a test you can make a GET or a POST to http://www.webpagetest.org/runtest.php.
  • In response, you will either get re-directed to the results page or you will receive an XML response (depending on if XML was requested).
  • You need API KEY to use Webpagetest API, and you have to contact wepagetest.org to get your API KEY. With this API key, you can submit max 100 tests per day for more you need a private instance. For Private instance - set up a test environment on your own server.
  • Sample - http://www.webpagetest.org/runtest.php?url=guru99.com&runs=1&f=xml&k=<your-api-key>

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

  • Check testId field in response and note it
  • Webpagetest is a free and popular tool so to ensure fair usage, your Request has to stand in the queue and wait for your turn.
  • You can check test status by doing a GET to http://www.webpagetest.org/testStatus.php with your test id.
  • Example - http://www.webpagetest.org/testStatus.php?f=xml&test=141107_12_BXZ

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-copy-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 = "<test-id-you-got-from-request-call>";
	$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;
	}
	?>

 

YOU MIGHT LIKE: