网站速度测试。 gzip 有问题吗?
我即将在我们的网络应用程序中实现互联网连接速度测试。没有要求它必须是一个非常高级的功能,所以我开始让它尽可能简单。
这个想法是从 HttpHandler 获取一些数据并查看需要多长时间,然后根据数据量和时间计算速度。我认为最好发送十个数据包并丢弃最高和最低时间,然后计算平均时间。
我的 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;
}
}
}
这是我的 jquery 代码,它添加按钮并显示一个对话框,我将在其中显示结果等。
$(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
});
});
});
});
文件 1mb.txt 是通过 Windows 中的命令提示符创建的:
fsutil 文件创建新 c:\temp\1mbfile.txt 1048576
该文件正好是 1mb。现在来说说问题。当我检查例如 firebug 中的响应时,它的大小只有 8.8 kb。这怎么可能?是GZIP压缩吗?如何将 1mb 文件压缩到 8.8kb?是因为当您使用 fsutil 创建文件时数据重复吗?
我寻找的解决方案是一种强制其不压缩的方法(如果这是问题所在),这可能吗?我找不到一种方法来设置正确的标头以不压缩。
如果这是一个压缩问题并且对此无能为力,我可以检查响应大小并始终指望它在所有环境中都完全符合该大小吗?
欢迎任何其他想法。如果有类似的想法,还有关于如何实现一般速度测试的想法。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,这可能是 gzip 压缩问题。
fsutil file createnew
创建一个由零组成的文件,因此它当然会非常有效地压缩。可以使用 Fiddler Web 调试代理来验证您的 Web 服务器是否正在使用压缩,如此处所述文章:
IIS 6 和 IIS 7 中的 HTTP 压缩
Yes, it could be a gzip compression issue.
fsutil file createnew
creates a file that consists of zeroes, so it would of course compress very efficiently.It's possible to use the Fiddler web debugging proxy to verify whether your web server is using compression, described in this article:
HTTP Compression in IIS 6 and IIS 7
肯定是压缩问题。作为一种解决方法,我没有尝试过,但我认为它应该有效,将生成的文件的扩展名更改为 Gzip 将避免的扩展名(例如:mp3),然后再次尝试测试,看看它是否再次被压缩。
让我知道发生了什么事,谢谢。
Definitely it's a compression issue. as a workaround which I didn't try but I think it should work, change the extension of your generated file to something that Gzip will avoid (ex: mp3) and try your test again and see if it get compressed again or not.
let me know what happened, thanks.