Brian
Brian

Reputation: 7145

Compare latency between two GET requests

I'm working on a website where requests pages of the site need to go through a reverse proxy first, because some paths on the domain should be routed to other web hosts.

I want to make the static assets (CSS, JS, images) load really fast, and my thought is that the additional "step" of having these requests go to the proxy is adding latency. There is no reason for these static assets to get routed thru the proxy.

I can achieve this setup by having my site serve the static assets through a separate domain, which points directly to the web host:

www.sample.com --> reverse proxy --> web host assets.sample.com --> web host

Before I go through with this setup, which adds a bit of complexity (I have to modify the site so it knows to output paths to the assets with a different domain), I want to make sure it's even worth doing.

How can I accurately measure the impact the reverse proxy has on response time of the requests?

I created this simple PHP script, but I wonder if it's a good way or not:

$with_proxy = get_average_response_time('https://www.example.com/path/to/asset.jpg');
$without_proxy = get_average_response_time('https://assets.example.com/path/to/asset.jpg');

echo "difference: " . $with_proxy - $without_proxy;

function get_average_response_time($url) {
    $total = 0;
    $amount = 50;
    for ($i = 0; $i < $amount; $i++) {
        $start = milliseconds();
        file_get_contents($url);
        $total += milliseconds() - $start;
    }

    return $total / $amount;
}

function milliseconds() {
    $mt = explode(' ', microtime());
    return ((int)$mt[1]) * 1000 + ((int)round($mt[0] * 1000));
}

Upvotes: 0

Views: 57

Answers (1)

yelsayed
yelsayed

Reputation: 5532

Depends what you have in file_get_contents. You would want to run this script from a separate host, other than the one serving your files. Also you want to make sure this function doesn't use caching.

As a general rule of thumb though, you should be serving your static resources from a CDN service, which makes sure the latency is relatively low for clients all over the web. Setting that up shouldn't be a lot of work.

Upvotes: 1

Related Questions