上回测试失败(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 回复