I am going to implement the speed of Internet connection in our web application. There is no requirement that it be a very advanced feature, so I started to make everything as simple as possible.
The idea is to extract some data from the HttpHandler and see how long it takes, and then calculate the speed from the amount of data and time. I thought it would be better to send, say, maybe ten data packets and throw out the highest and lowest time, and then calculate the 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 a button and displays a dialog in which I will show 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();
});
});
});
});
The 1mb.txt file was created using the command line in the windows:
Fsutil file c: \ temp \ 1mbfile.txt 1048576
1 . . , , firebug, 8.8 kb. ? GZIP? 1 8,8 ? , fsutil, ?
, , - ( ), ? , .
, , , ?
. , speedtest , .