我有一个NodeJS服务器(Express),我使用nodeJs站点上的集群模块示例将请求扩展到多个处理器。
if (cluster.isMaster) {
for (var i = 0; i < numCPUs; i++) {
cluster.fork();
};
cluster.on('exit', function(worker, code, signal) {
console.log('worker ' + worker.process.pid + ' died');
cluster.fork();
});
} else {
server.listen(app.get('port'), function(){
console.log('HTTP server on port ' + app.get('port') + ' - running as ' + app.settings.env);
});
// setup socket.io communication
io.sockets.on('connection', require('./app/sockets'));
io.sockets.on('connection', require('./app/downloadSockets'));
}问题是,围城的基准告诉我,点击次数没有增加。这是围困的产物:
$ siege -c100 192.168.111.1:42424 -t10S
** SIEGE 3.0.5
** Preparing 100 concurrent users for battle.
The server is now under siege...
Lifting the server siege... done.
Transactions: 1892 hits
Availability: 100.00 %
Elapsed time: 10.01 secs
Data transferred: 9.36 MB
Response time: 0.01 secs
Transaction rate: 189.01 trans/sec
Throughput: 0.93 MB/sec
Concurrency: 1.58
Successful transactions: 1892
Failed transactions: 0
Longest transaction: 0.05
Shortest transaction: 0.00聚类后:
$ siege -c100 192.168.111.1:42424 -t10S
** SIEGE 3.0.5
** Preparing 100 concurrent users for battle.
The server is now under siege...
Lifting the server siege... done.
Transactions: 1884 hits
Availability: 100.00 %
Elapsed time: 9.52 secs
Data transferred: 9.32 MB
Response time: 0.01 secs
Transaction rate: 197.90 trans/sec
Throughput: 0.98 MB/sec
Concurrency: 1.72
Successful transactions: 1884
Failed transactions: 0
Longest transaction: 0.07
Shortest transaction: 0.00这是否意味着我的服务器已经获得了最大的单服务器吞吐量,可能是因为它是一台本地机器,或者它无法获得4个处理器,因为运行的进程太多,我不确定。
如何使用集群模块来增加节流,以及为什么我的当前代码不成功?此外,我还检查了它是否确实创建了服务器的4个实例,即cluster.fork工作。任何建议都是非常有用的。
发布于 2015-07-04 13:20:40
这种效果是通过集群或增长并发查询来实现的(尝试将并发用户的数量增加到300-400)。或者是给你带来沉重负担的任务。让我们花费更多有趣的测试:将下载大约1MB的文件大小,另外,为了模拟复杂的操作,我们会延迟5毫秒和50毫秒。对于本地测试中的四个核心处理器,如下所示(分别用于正常和集群):
$ siege -c100 http://localhost/images/image.jpg -t10S正常模式(5毫秒延迟):
Lifting the server siege... done.
Transactions: 1170 hits
Availability: 100.00 %
Elapsed time: 9.10 secs
Data transferred: 800.79 MB
Response time: 0.27 secs
Transaction rate: 128.57 trans/sec
Throughput: 88.00 MB/sec
Concurrency: 34.84
Successful transactions: 1170
Failed transactions: 0
Longest transaction: 0.95
Shortest transaction: 0.01集群模式(延迟5毫秒):
Lifting the server siege... done.
Transactions: 1596 hits
Availability: 100.00 %
Elapsed time: 9.04 secs
Data transferred: 1092.36 MB
Response time: 0.06 secs
Transaction rate: 176.55 trans/sec
Throughput: 120.84 MB/sec
Concurrency: 9.81
Successful transactions: 1596
Failed transactions: 0
Longest transaction: 0.33
Shortest transaction: 0.00正常模式(50毫秒延迟):
Lifting the server siege... done.
Transactions: 100 hits
Availability: 100.00 %
Elapsed time: 9.63 secs
Data transferred: 68.44 MB
Response time: 5.51 secs
Transaction rate: 10.38 trans/sec
Throughput: 7.11 MB/sec
Concurrency: 57.18
Successful transactions: 100
Failed transactions: 0
Longest transaction: 7.77
Shortest transaction: 5.14集群模式(延迟50毫秒):
Lifting the server siege... done.
Transactions: 614 hits
Availability: 100.00 %
Elapsed time: 9.24 secs
Data transferred: 420.25 MB
Response time: 0.90 secs
Transaction rate: 66.45 trans/sec
Throughput: 45.48 MB/sec
Concurrency: 59.59
Successful transactions: 614
Failed transactions: 0
Longest transaction: 1.50
Shortest transaction: 0.50发布于 2015-07-08 17:48:17
在你的例子中,你并没有做任何事情。连接到mysql并运行一个繁重的查询,或者发出一个只需几秒钟的http请求。您注意到,最终,您将编写一些代码(或使用第三方库)来阻止事件循环。这是集群非常重要的时候,因为实际上每个处理器都有一个事件循环。如果一个查询很慢,而事件循环需要等待,它不会停止访问API/应用程序的新请求。
此外,如果计划使用或连接数据库或获取外部资源,则可能需要阅读连接池,特别是npm上的泛型池。
https://stackoverflow.com/questions/31003016
复制相似问题