提高网络速度并连接到node.js服务器时出现意外的行为 [英] Unexpected behavior when increasing network speed and connecting to a node.js server

查看:135
本文介绍了提高网络速度并连接到node.js服务器时出现意外的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  var app = require('表现')(); 
var compression = require('compression');
app.use(compression());

app.get('/',function(request,response){
response.send('<!DOCTYPE html> .......');
}
app.listen(2345);

我发送的html是2.4kB (压缩时为1.2kB)。

当使用不同的网络速度(使用开发工具)进行测试时,我得到这个意想不到的行为:

 code> 50kbps:延迟600ms,下载1ms 
250kbps:延迟300ms,下载0.6ms
750kbps:延迟100ms,下载100ms
2Mbps:延迟10ms,下载200ms
32Mbps:延迟5ms,下载210ms

我不认为下载时间应该会增加网络速度在250kbps之后增加?

再次看看如果我删除压缩会发生什么:

  var app = require('express')(); 

app.get('/',function(request,response){
response。 send('<!DOCTYPE html> ....... ');
}
app.listen(2345);

现在文件只有2.4kB,看看延迟/下载次数:

  50kbps:延迟550ms,下载230ms 
250kbps:延迟350ms,下载50ms
750kbps:延迟120ms,下载15ms
2Mbps:延迟35ms,下载6ms
32Mbps:延迟4ms,下载0.5ms

使用非gzip压缩内容(和contet-length标头)的响应似乎是可以的,但是使用gzip压缩的内容(使用transfer-encoding chunked标题)的响应似乎不能正常。
这是什么关于?
我强烈建议您用自己喜欢的任何工具自己模拟一个类似的测试,然后再说出我的基准是错误的,这是不可能的。如果你得到不同的结果,请分享他们。

解决方案

Express.js压缩选项



我也会毫不犹豫地更改不同的压缩质量设置,策略,特别是快速压缩模块的阈值设置,如下所述: https://github.com/expressjs/compression ,特别是:



压缩阈值级别< h3>

由于您只发送几个字节的文本数据作为正文,请尝试将阈值设置为低于默认值 1Kb


对于响应,考虑压缩前的响应体大小的字节阈值,默认为1kb。这是一些字节,字节模块接受的任何字符串,或者是false。


模块github页面)



HTTP压缩并不总是更快



确保玩其他HTTP功能,如HTTP流水线或接受的编码(也在客户端)打开或关闭切换可能会大大改变下载时间的结果。



IBM进行了一系列优秀的HTTP测试,我建议您在这里阅读: http ://www.ibm.com/developerworks/library/wa-httpcomp/


I have a simple node.js server like:

var app = require('express')();
var compression = require('compression');
app.use(compression());

app.get('/', function(request, response) {
  response.send('<!DOCTYPE html>.......');
}
app.listen(2345);

The html I send is 2.4kB (1.2kB when compressed).
When testing on different network speed (using dev tools) I get this unexpected behavior:

50kbps:  Latency 600ms, download   1ms
250kbps: Latency 300ms, download 0.6ms
750kbps: Latency 100ms, download 100ms
2Mbps:   Latency  10ms, download 200ms
32Mbps:  Latency   5ms, download 210ms

I don't think that the download time is supposed to increase when network speed increases after 250kbps. What is going on?
Again look at what happens if I remove compression:

var app = require('express')();

app.get('/', function(request, response) {
  response.send('<!DOCTYPE html>.......');
}
app.listen(2345);

Now the file is just 2.4kB and look at the latency/download times:

50kbps:  Latency 550ms, download 230ms
250kbps: Latency 350ms, download  50ms
750kbps: Latency 120ms, download  15ms
2Mbps:   Latency  35ms, download   6ms
32Mbps:  Latency   4ms, download 0.5ms

The response with the non-gzipped content (and contet-length header) seems to be ok, but the response with the gzipped content (with transfer-encoding chunked header) doesn't seem to be ok.
What is this all about?
I strongly encourage you to simulate a similar test yourself with whatever tools you like and see the results yourself before saying that my benchmark is wrong and that this cannot be possible. And if you get different results please share them.

解决方案

Express.js compression options

I would also not hesitate to change the different compression quality settings, strategies and especially the treshold setting of the express compression module, as described here: https://github.com/expressjs/compression, especiall the:

Compression Treshold Level

Since you are sending only a few bytes of textual data as body, try to set the treshold lower then the default of 1Kb.

The byte threshold for the response body size before compression is considered for the response, defaults to 1kb. This is a number of bytes, any string accepted by the bytes module, or false.

(cited from the express compression module github page)

HTTP Compression is not always faster

Make sure to play around with other HTTP features like HTTP Pipelining or accepted encodings (also on the client side) as switching those on or off may vastly alter the outcome of download time.

IBM conducted a series of excellent HTTP tests which I recommend you read about here: http://www.ibm.com/developerworks/library/wa-httpcomp/

这篇关于提高网络速度并连接到node.js服务器时出现意外的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆