Jump to content

PHP Upload Timer / Bandwidth Test


Roq

Recommended Posts

I've been tasked to build a bandwidth tester for my company since we have many WAN sites.  To start off the script will only run in one location, in our main datacenter.  The basic design is a webpage running on a server in our main datacenter, that basically just has a button to test network speed from their  current location.  Since I have a lot of other PHP / Perl / MySQL code on this box I was thinking about doing this with these languages, but I fear that I'm going to need some sort of client language like Java to really complete this task.  Ideally, after the test is performed I will record the result per /24 subnet, so if the request comes from, say, 172.22.28.35,  an entry would be put in the database for 172.22.28.0 with the upload and download speeds.  Then, when someone does the test I'll be able to display the 'average' speed for the subnet location, and the test results, for the end user to determine if there are network speed issues or if things are normal.

 

Now that said, if you think I should use a different language then by all means, please give me suggestions.  But since I'm currently attempting this in PHP I've built a simple uploader using the code below:

 

<html>
<?PHP
function gettime ()
{
   $mtime = microtime(); 
   $mtime = explode(" ",$mtime); 
   $mtime = $mtime[1] + $mtime[0]; 
   return $mtime;
}
$maxfilesize = "100000000"; #bytes
?>


<form enctype="multipart/form-data" action="<?PHP $_SERVER['PHP_SELF'] ?>" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="<?PHP $maxfilesize ?>" />
Choose a file to upload: <input name="uploadedfile" type="file" /><br />
<input name="form_uploadfile" id="form_uploadfile" type="submit" value="Upload File" />
</form>


<?PHP

echo "Max Filesize: ".substr("$maxfilesize", 0, -6)."mb";
$filesize = $_FILES['uploadedfile']['size'];
if ($filesize > $maxfilesize) {echo "<font color=red>Error: $filesize > $maxfilesize.";}
if(isset($_POST['form_uploadfile'])){

echo "<br>isset passed<br>";
echo "Filename: ".$_FILES['uploadedfile']['name']."<br>";
echo "Filesize: $filesize bytes<br>";
$target_path = $_SERVER["DOCUMENT_ROOT"] . "/uploads/";
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']); 
echo "Current Targetpath: $target_path<br>";
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
	echo "<font color=green>The file ".  basename( $_FILES['uploadedfile']['name']). 
	" has been uploaded.</font>";
} else {
	echo "<font color=red>There was an error uploading the file, please try again!</font>";
}
}

$endtime = gettime();
$totaltime = ($endtime - $starttime); 
echo "<br><br>PHP Execute Time: $totaltime seconds";
?>

 

Alright, so most of this was pieced together by other tutorials and whatnot, but the point is that this works and I have successfully uploaded up to 77 meg files and everything is peachy (I changed max post data, execute time, max upload size, etc to support up to 100m uploads since this is private right now).

 

The problem with this code is I want to measure the time it takes to upload the file, and the timer works, but a small file thats like 20kb will say it took .0005s and a 50 meg file takes maybe .7s, even though it obviously takes much longer to complete the upload.

 

I *THINK* the problem is that the upload is actually happening when the user hits the submit button and is attempting to SEND the POST data, and that I'm really only calculating the time it takes to move the file and run the rest of the PHP code, does this sound right?  Does anyone have any ideas for ways around this, or a different approach I could take?

 

Thanks,

-roq

 

Link to comment
Share on other sites

Use JavaScript to send the timestamp of the client's machine with the initial upload.

 

As soon as the upload is finished, user JavaScript on the client's machine to send the timestamp again.

 

That will give you the time required to upload the file plus a little extra for the basic communication with the server.

Link to comment
Share on other sites

The most I've used javascript for is a confirm delete, could you maybe show me an example of how I could do this?  My research online for 'passing javascript variables to php' always seems to be people passing variables in the URL and using $_GET, which isn't exactly desired behavior for me, and I have no idea how to measure the end time of the file transfer.  I would  guess the start time would be in the form with something like this?

 

<input name="form_uploadfile" id="form_uploadfile" type="submit" value="Upload File" onClick="return sendTime()"/>

 

Any other details would be great, thanks.

 

Link to comment
Share on other sites

I'd do this with AJAX.

 

1) User clicks button

2) You post an "init" event to timer.php using AJAX.  timer.php records the timestamp in $_SESSION['init_tm'].

3) You then create a string in JavaScript 1024 or 2048 characters in length.  Post this to timer.php as an "upload" event.  timer.php records the timestamp in $_SESSION['end_tm']

4) Make one more AJAX request to timer.php as a "calculate" event.  timer.php subtracts the time between 'end_tm' and 'init_tm' that were stored in $_SESSION.  That's the duration of the request.  Divide the size of the string sent by the duration and that's your MB / s upload rate.  Display to the user.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.