NodeJS 网络传输随着长度线性增加
NodeJS network transmission gets linear higher with length
目标是使用 nodejs http 服务器传送文件。
问题是服务器响应浏览器的传输。
我把一个字符串作为程序代码中显示的内容传送。
VPS:debian 7 / SSD / 2 核 / ~30ms ping
相关:
node.js response.write(data) taking long time when size of data is small
Some string size benchmarking (content-download time)
- 1 kb: 0,18 ms
- 10 kb: 7,00 ms
- 20 kb: 47,50 ms
- 30 kb: 55,00 ms
- 40 kb: 58,10 ms
- 50 kb: 86,20 ms
- 60 kb: 93,10 ms
- 80 kb: 107,10 ms
- 100 kb: 120,00 ms (nginx needs 0.31ms)
程序代码:
// var request => the http incoming request
request.socket.setNoDelay();
request.connection.setNoDelay();
// var response => the response from the http incoming request
response.on('close', function () {
console.log("[ABORTED][%s] %s", request.connection.remoteAddress, request.url);
});
response.on('finish', function () {
console.log("[SUCCESS][%s] %s", request.connection.remoteAddress, request.url);
});
// get the content of the file (takes ~0ms)
var fileContent = fileSystem.readFileSync(filePath, 'utf8');
// Configure response header
response.writeHead(200, {
'content-length': ''+fileContent.length,
'content-type': 'text/plain',
});
// Send content
response.end(fileContent, 'utf8', function(){
console.log("Ending response took %sms", Date.now()-timeB2);
});
长话短说:
改变
response.writeHead(200, {
'Content-Type': 'text',
'Content-Length': stat.size
});
到
response.writeHead(200, {
'Content-Type': 'application/arraybuffer',
'Content-Length': stat.size
});
长版:
问题是 content-length 是错误的。事实上,header 的信息是错误的,因为 createReadStream returns 是一个缓冲区(数字数组),而不是文本。
如果您想以文本形式发送,只需使用
response.end(readFileSync (filePath, 'utf8'));
它会自动构造正确的header。否则,您将需要找到 'the number of characters' 作为 content-length 并将编码传递给 createReadStream,即:
var readStream = fileSystem.createReadStream(filePath, 'utf8');
我不知道在文本文档中获取 'the number of characters' 的简单方法。
另一种方法是将其作为 application/arraybuffer 发送,并将 content-length 作为缓冲区的 byteLength。当你使用stat.size时,它实际上是文件的字节大小,如果你要将它作为Buffer流读取,它相当于byteLength。
所以,没有问题。
内容下载时间是根据我的网速计算的。
不同之处在于压缩和缓存。 Nginx ist 更快,因为它使用某种类型的压缩和缓存。
为了获得更好的结果,我在我的 nodejs 应用程序中添加了 SPDY 和 GZIP,结果几乎与 nginx 相同。
目标是使用 nodejs http 服务器传送文件。
问题是服务器响应浏览器的传输。
我把一个字符串作为程序代码中显示的内容传送。
VPS:debian 7 / SSD / 2 核 / ~30ms ping
相关: node.js response.write(data) taking long time when size of data is small
Some string size benchmarking (content-download time)
- 1 kb: 0,18 ms - 10 kb: 7,00 ms - 20 kb: 47,50 ms - 30 kb: 55,00 ms - 40 kb: 58,10 ms - 50 kb: 86,20 ms - 60 kb: 93,10 ms - 80 kb: 107,10 ms - 100 kb: 120,00 ms (nginx needs 0.31ms)
程序代码:
// var request => the http incoming request
request.socket.setNoDelay();
request.connection.setNoDelay();
// var response => the response from the http incoming request
response.on('close', function () {
console.log("[ABORTED][%s] %s", request.connection.remoteAddress, request.url);
});
response.on('finish', function () {
console.log("[SUCCESS][%s] %s", request.connection.remoteAddress, request.url);
});
// get the content of the file (takes ~0ms)
var fileContent = fileSystem.readFileSync(filePath, 'utf8');
// Configure response header
response.writeHead(200, {
'content-length': ''+fileContent.length,
'content-type': 'text/plain',
});
// Send content
response.end(fileContent, 'utf8', function(){
console.log("Ending response took %sms", Date.now()-timeB2);
});
长话短说:
改变
response.writeHead(200, {
'Content-Type': 'text',
'Content-Length': stat.size
});
到
response.writeHead(200, {
'Content-Type': 'application/arraybuffer',
'Content-Length': stat.size
});
长版: 问题是 content-length 是错误的。事实上,header 的信息是错误的,因为 createReadStream returns 是一个缓冲区(数字数组),而不是文本。
如果您想以文本形式发送,只需使用
response.end(readFileSync (filePath, 'utf8'));
它会自动构造正确的header。否则,您将需要找到 'the number of characters' 作为 content-length 并将编码传递给 createReadStream,即:
var readStream = fileSystem.createReadStream(filePath, 'utf8');
我不知道在文本文档中获取 'the number of characters' 的简单方法。
另一种方法是将其作为 application/arraybuffer 发送,并将 content-length 作为缓冲区的 byteLength。当你使用stat.size时,它实际上是文件的字节大小,如果你要将它作为Buffer流读取,它相当于byteLength。
所以,没有问题。
内容下载时间是根据我的网速计算的。
不同之处在于压缩和缓存。 Nginx ist 更快,因为它使用某种类型的压缩和缓存。
为了获得更好的结果,我在我的 nodejs 应用程序中添加了 SPDY 和 GZIP,结果几乎与 nginx 相同。