How To Speed Up Testing With Localized Servers

by Stephen Smith on

You know what ticks me off? When my time gets wasted and I didn't agree to it.

Don't get me wrong, I love wasting time, it's one of my favourite things to do. But sometimes I'm trying to test that all my sites are up, fast, and localized to their country, my tests are running slow, and that's frustrating.

You watch things crawl along, and you wonder "How can I speed up my tests?"

There's a way. It involves a little bit of nerding out, but stick with me here.

When was the last time you sent a letter? I know we're all fans of email (sort of), but occasionally you can't get around putting a stamp on a piece of paper and dropping it off at your nearest post office.

When you sent that letter, did you expect it to arrive immediately? Of course not, there are tons of steps it has to go through. It has to be:

  1. checked in
  2. put on a truck and driven to the nearest depot
  3. processed so they know where it's going
  4. sorted into the right bin that's going there
  5. put on another truck and driven to a central depot
  6. transferred from that central depot to the central depot nearest its destination
  7. processed again so they know what depot in the destination area to send it to
  8. driven to that depot
  9. put on a delivery vehicle and delivered

And that's the simple version. Imagine if you were sending a letter to another country, or halfway around the world. It would take way longer, right?

That's why it takes so long to test your worldwide sites from one location: The same thing happens to every request you make.

Your request starts at your computer or server, and travels:

  1. to your internet provider's local hub
  2. to a larger hub/IXP where it can access a backbone connection
  3. along the backbone to the nearest IXP to its destination
  4. to the local hub closest to the server it wants to contact
  5. to the destination server

So when it's put this way, the solution to how to speed up your tests becomes obvious: Make your requests from as close as possible to the destination server. Sending a request from Boston to Pittsburgh is quicker than Boston to Los Angeles, and way quicker than Boston to Beijing.

This is where Where’s It Up comes in. Where’s It Up has a network of 250+ locations around the world you can send your requests from, so no matter where your servers are located, you can speed up your tests by using the service.

Don't take my word for it, I can prove it with code. This script times how long it takes for an average web request to go from 250+ different locations around the world to America's east coast and back:

<?php

// Get all servers.
$curl = curl_init('https://api.wheresitup.com/v4/sources');

// Ensure the payload is returned as a string.
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

// Obviously, fill in your details where indicated before running this.
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
  'Auth: Bearer YOUR_CLIENT_ID YOUR_TOKEN',
));

// Execute the request, decode the json-formatted response, store.
$result = json_decode(curl_exec($curl));
$result = $result->sources;

// Assemble a list of hostnames.
$hostnames = array();

foreach($result as $server)
{
  // Convert server names into raw hostnames.
  $hostnames[] = $server->name . ".wonderproxy.com";
}

$response_times = array();

// Query each server and record the execution time.
foreach($hostnames as $hostname)
{
  // Start the clock.
  // https://secure.php.net/manual/en/function.microtime.php
  $time_start = microtime(true);

  // Make the request.
  // https://secure.php.net/manual/en/function.gethostbyname.php
  gethostbyname($hostname);

  $time_end = microtime(true);

  $time_elapsed = $time_end - $time_start;

  // Add the response time to the list.
  $response_times[$hostname] = $time_elapsed;

}

// Sort the response times in descending order.
// https://secure.php.net/manual/en/function.arsort.php
arsort($response_times);

// Output the results.
var_dump($response_times);

After I let that run for 10-15 seconds, it responded with:

array(254) {
  ["kiev.wonderproxy.com"]=>
  float(0.46902704238892)
  ["desmoines.wonderproxy.com"]=>
  float(0.24101114273071)
  ["brunswick.wonderproxy.com"]=>
  float(0.22357606887817)
  ["missoula.wonderproxy.com"]=>
  float(0.2017650604248)
  ["laceiba.wonderproxy.com"]=>
  float(0.19510197639465)

...a few hundred lines later...

  ["saopaulo.wonderproxy.com"]=>
  float(0.013137817382812)
  ["tampa.wonderproxy.com"]=>
  float(0.013134956359863)
  ["bratislava.wonderproxy.com"]=>
  float(0.013114929199219)
  ["vilnius.wonderproxy.com"]=>
  float(0.013105869293213)
  ["moscow.wonderproxy.com"]=>
  float(0.013011932373047)
}

As you can see, there are some clear differences between locations. Kiev is probably just having a rough day, and it should be noted that every time I ran this script the precise rankings changed due to different server loads along the way.

But the general theme remains constant: For the exact same request, location affects response time.

Consider a testing scenario where you are making 25,000 requests. Adding 0.1 seconds to each one of them adds 40+ minutes to your test time. Do you feel like waiting 40+ minutes if you don't have to? I don't.

I encourage you to run your own speed comparisons. Where’s It Up makes it easy, by offering a free trial and easy API access. You have nothing to lose, and hours of time to gain.