I am about to implement a internet connection speedtest in our web application. There is no requirement that it has to be a very advanced feature so I started to make it as simple as I could.
The idea is to fetch some data from a HttpHandler and see how long time it takes and then calculate the speed from the amount of data and the time. I thought it is better to send lets say maybe ten packages of data and throw away highest and lowest time and then calculate average time.
My HttpHandler:
public class SpeedTest : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
var Response = context.Response;
FileInfo file = new FileInfo(@"C:\dev\Project\****\trunk\Application\1mb.txt");
if (file.Exists)
{
Response.Clear();
Response.AddHeader("Content-Length", file.Length.ToString());
Response.WriteFile(file.FullName);
Response.End();
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
Here is my jquery code that adds button and displays a dialog where I will present the results etc.
$(document).ready(function () {
$('<a href="#">Speedtest</a>').prependTo('#HeaderInfoBarRight').button({
text: false, icons: { primary: "ui-icon-clock" }
})
.click(function () {
var dialog = $('<div></div>').dialog({
autoOpen: false
, title: 'Speedtest'
, modal: true
, width: 'auto'
, resizable: false
, height: 'auto'
, minHeight: 50
, close: function () {
$(this).dialog('destroy').remove();
}
});
var html = '<div>To start the test click below.</div>' +
'<a href="#">Start test</a>';
dialog.html(html).dialog('open');
dialog.find('a').button({ text: true })
.click(function () {
var startTest = new Date();
$.post('SpeedTest.ashx', {}, function (result) {
var endTest = new Date();
//Calculate time
});
});
});
});
The file 1mb.txt was created through command prompt in windows as:
fsutil file createnew c:\temp\1mbfile.txt 1048576
The file is exactly 1mb. Now to the problem. When I inspect the response in for example firebug it is only 8.8 kb in size. How is this possible? Is it GZIP compression? How can it compress a 1mb file to 8.8 kb? Is it because when you create a file with fsutil the data is to repetitive?
A solution I search for is a way to force it to not compress (if this is the problem), is that possible? I can't find a way to set correct headers to not compress.
If it is a compression problem and there is nothing to do about it, can I check the response size and always count on it to be exactly that size in all environments?
Any other ideas are welcome. Also ideas over how to implement the speedtest in general if there are ideas like that.