PHP: Calculate Server Download Speed


#1

Hello All.

Im trying to code a small app that will tell me the server download speed. (not upload speed).

I figured useing curl whould be best to download file.
Then use microtime before and after the curl. To find the time it took to download.
Divide filesize by time it took.

Here Is My Attempt:

$time_start = microtime(true);

    // initialize curl with given url
    $ch = curl_init("http://download.bethere.co.uk/images/61859740_3c0c5dbc30_o.jpg");
    // make sure we get the header
    curl_setopt($ch, CURLOPT_HEADER, 1);
    // make it a http HEAD request
    curl_setopt($ch, CURLOPT_NOBODY, 1);
    // add useragent
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)");
    //Tell curl to write the response to a variable
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    // The maximum number of seconds to allow cURL functions to execute.
    curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,60);
    // Tell curl to stop when it encounters an error
    curl_setopt($ch, CURLOPT_FAILONERROR, 1);

    $execute = curl_exec($ch);

    // Check if any error occured
    if(!curl_errno($ch)){

$bytes = 6576848 / 1048576; // 6576848=filesize || 1048576=how many bytes in a mb

$time_end = microtime(true);

$time = $time_end - $time_start; //Time it took

$mbps = $time / $bytes;
echo 'MBps: '.$mbps;
        clearstatcache();
}
curl_close($ch);

It says my speed is MBps: 75.556655877259 :goof: ... Thats wrong by the way..

Any idea where I am going wrong? ... Or know a better method to calculate MBps server download speed?

Much Thanks
Very Appretiated!


#2

cURL provides some handy metrics about a request, available using the curl_getinfo() function after using curl_exec().

I whipped up an example (warning: it might not be perfect, or work for you at all!)
http://pastie.org/private/eljzecktih05vh57pgo8q


#3

Hi,

I've never used cURL before so there is probably better ways of doing it than my code below. Try this (untested, might have syntax errors or whatever):

<?php

function measure_speed($url)
{
  $ch = curl_init($url);

  // Set options:
    curl_setopt($ch, CURLOPT_HEADER, 1)
    // HTTP HEAD request...
    curl_setopt($ch, CURLOPT_NOBODY, 1);
    // Useragent...
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)");
    // Write the response to a variable...
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    // Max execution time...
    curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,60);
    // Stop on error
    curl_setopt($ch, CURLOPT_FAILONERROR, 1);

    return curl_exec($ch);
}

function microtime_diff ($start, $end = time())
{
  list($start_usec, $start_sec) = explode(" ", $start);
  list($end_usec, $end_sec) = explode(" ", $end);

  $diff_sec= intval($end_sec) - intval($start_sec);
  $diff_usec= floatval($end_usec) - floatval($start_usec);

  return floatval($diff_sec) + $diff_usec;
}

$time_start = microtime(true);
$execute = measure_speed("http://download.bethere.co.uk/images/61859740_3c0c5dbc30_o.jpg");
$time_end = microtime(true);

// Calculate time taken to receive file
$time_taken = microtime_diff ($time_start, $time_end);

// File size in bytes
$filesize = 6576848;

// If no error:
if (!curl_errno ($ch)) {
  // Calculate speed
  $bytes_per_sec = $filesize / $time_taken;
  $KB_per_sec = $bytes_per_sec / 1024;
  $MB_per_sec = $KB_per_sec / 1024;

  // Print result
  echo 'MB/s: ', $MB_per_sec;

  clearstatcache();
} else {
  echo 'We had a CURL error!';
}

curl_close($ch);


#4

AlienDev, Your code has a few systax errors but when I fixed the little mistakes it says my server speed is MB/s: 86.992467180318 ... I Wish it was! lol

Salathe, YOU HAVE IT! .. I can not thank you enough for this, and the script working great speedtest is correct and the code and output is exsactly what i needed.


#5