nodejs官方cluster测试(2)
发布于 2年前 作者 zaobao 1503 次浏览

上回测试失败(nodejs cluster测试 )的原因总算找到了,不是nodejs的原因,nodejs官方cluster工作良好,问题在于测试方式上,在浏览器中同时开两个相同标签,浏览器只会先发送一个请求,等该请求返回后,在发送第二个,也就是说请求在浏览器端就已经被阻塞了。

所以《Node入门》一文中测试阻塞的方式不完全正确,文中描述的阻塞可能在浏览器端就被阻塞了。

正确的方法是在一个请求返回后不断刷新,或是开不同的浏览器进程。这是开十个woker的输出,可以看到十个woker完美并行执行。

A worker with #2 is now connected to 0.0.0.0:80
A worker with #4 is now connected to 0.0.0.0:80
A worker with #5 is now connected to 0.0.0.0:80
A worker with #8 is now connected to 0.0.0.0:80
A worker with #1 is now connected to 0.0.0.0:80
A worker with #3 is now connected to 0.0.0.0:80
A worker with #6 is now connected to 0.0.0.0:80
A worker with #7 is now connected to 0.0.0.0:80
A worker with #9 is now connected to 0.0.0.0:80
A worker with #10 is now connected to 0.0.0.0:80
Worker #1 has a request
Worker #7 has a request
Worker #6 has a request
Worker #3 has a request
Worker #8 has a request
Worker #5 has a request
Worker #4 has a request
Worker #2 has a request
Worker #10 has a request
Worker #9 has a request
Worker #1 makes a response
Time: 5003ms
Worker #1 has a request
Worker #7 makes a response
Time: 5005ms
Worker #6 makes a response
Time: 5008ms
Worker #3 makes a response
Time: 5004ms
Worker #8 makes a response
Time: 5008ms
Worker #5 makes a response
Time: 5008ms
Worker #4 makes a response
Time: 5004ms
Worker #2 makes a response
Time: 5001ms
Worker #10 makes a response
Time: 5000ms
Worker #9 makes a response
Time: 5000ms

另外,火狐浏览器在一个请求超过一秒并且失败时,会再次发送该请求,但不会发送第三次,所以在模拟宕机的测试中,开一个Firefox的标签,你会发现实际宕掉两个worker。

5 回复

多线程/多进程/单线程事件驱动 任何1种都是高性能的

NODE属于第三种, 弄CLUSTER反而享受不了JS的原生特性,徒添消耗

node在什么情况下才会触发切换进程?

@peiweippww 这个工作是操作系统自己负载均衡的,控制不了

CLUSTER跟原生特性没有冲突,可以更充分运用多核硬件。

@dgq420377903 比如聊天应用,张三向李四发送消息,是进程A处理的;但李四被系统回复说没有他的信息,因为这是是进程B处理的李四请求

回到顶部