未加星标

Node.JS Response Time

字体大小 | |
[前端(javascript) 所属分类 前端(javascript) | 发布者 店小二03 | 时间 2019 | 作者 红领巾 ] 0人收藏点击收藏

Threw Node.JS on an AWS instance and was testing the request times, got some interesting results.

I used the following for the server:

var http = require('http'); http.createServer(function(req, res) { res.writeHead(200, {'Content-Type': 'text/html'}); res.write('Hello World'); res.end(); }).listen(8080);

I have an average 90ms delay to this server, but the total request takes ~350+ms. Obviously a lot of time is wasted on the box. I made sure the DNS was cached prior to the test.

I did an Apache bench on the server with a cocurrency of 1000 - it finished 10,000 requests in 4.3 seconds... which means an average of 4.3 milliseconds.

UPDATE: Just for grins, I installed Apache +php on the same machine and did a simple "Hello World" echo and got a 92ms response time on average (two over ping).

Is there a setting somewhere that I am missing?

Problem courtesy of: Jonathan

Solution

I absolutely hate answering my own questions, but I want to pass along what I have discovered with future readers.

tl;dr: There is something wrong with res.write(). Use express.js or res.end()

I just got through conducting a bunch of tests. I setup multiple types of Node server and mixed in things like PHP and Nginx. Here are my findings.

As stated previously, with the snippet I included above, I was loosing around 250ms/request, but the Apache benchmarks did not replicate that issues. I then proceeded to do a PHP test and got results ranging from 2ms - 20ms over ping... a big difference.

This prompted some more research, I started a Nginx server and proxied the node through it, and somehow, that magically changed the response from 250ms to 15ms over ping. I was on par with that PHP script, but that is a really confusing result. Usually additional hops would slow things down.

Intrigued, I made an express.js server as well - and something even more interesting happened, the ping was 2ms over on its own. I dug around in the source for quite a while and noticed that it lacked a res.write() command, rather, it went straight to the res.end() . I started another server removing the "Hello World" from the res.write and added it to the res.end and amazingly, the ping was 0ms over ping.

I did some searching on this, wanted to see if it was a well-known issue and came across this SO question, who had the exact same problem. nodejs response speed and nginx

Overall, intresting stuff. Make sure you optimize your responses and send it all at once.

Best of luck to everyone!

Solution courtesy of: Jonathan

Discussion

While Chrome Developer Tools is a good way to investigate front end performance, it gives you very rough estimate of actual server timings / cpu load. If you have ~350 ms total request time in dev tools, subtract from this number DNS lookup + Connecting + Sending + Receiving, then subtract roundtrip time (90 ms?) and after that you have first estimate. In your case I expect actual request time to be sub-millisecond. Try to run this code on server:

var http = require('http'); function hrdiff(t1, t2) { var s = t2[0] - t1[0]; var mms = t2[1] - t1[1]; return s*1e9 + mms; } http.createServer(function(req, res) { var t1 = process.hrtime(); res.writeHead(200, {'Content-Type': 'text/html'}); res.write('Hello World'); res.end(); var t2 = process.hrtime(); console.log(hrdiff(t1, t2)); }).listen(8080);

Based on ab result you should estimate average send+request+receive time to be at most 4.2 ms ( 4200 ms / 10000 req) (did you run it on server? what concurrency?)

Discussion courtesy of: Andrey Sidorov

This recipe can be found in it's original form on Stack Over Flow .

本文前端(javascript)相关术语:javascript是什么意思 javascript下载 javascript权威指南 javascript基础教程 javascript 正则表达式 javascript设计模式 javascript高级程序设计 精通javascript javascript教程

代码区博客精选文章
分页:12
转载请注明
本文标题:Node.JS Response Time
本站链接:https://www.codesec.net/view/628198.html


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 前端(javascript) | 评论(0) | 阅读(39)