[英]Why is node.js not using all the bandwidth available?
I have this sample code in coffescript for node.js . 我在node.js的 coffescript中有此示例代码。 What I want to do is to parallel download a lot of objects from S3.
我要做的是从S3并行下载很多对象。
I have a list of users that contains a list of friends , and I want to download all these friends as fast as possible. 我有一个包含朋友列表的用户列表,并且我想尽快下载所有这些朋友。 The sample code waits 1 second to simulate how much it spends downloading an user object , and it downloads an object from S3 to simulate the download of a friend object .
示例代码等待1秒钟,以模拟它花了多少钱来下载用户对象 ,然后从S3下载一个对象来模拟朋友对象的下载。
async = require 'async'
aws = require 'aws-sdk'
cluster = require 'cluster'
config = require './config'
USERS = 30
FRIENDS = 300
class TestSpeed
constructor: () ->
@s3 = new aws.S3 accessKeyId:config.S3_KEY, secretAccessKey:config.S3_SECRET
start: (next) =>
async.map([0...USERS], @downloadUser, next)
downloadUser: (x, next) =>
console.log "Starting to download user #{x}"
setTimeout(
=>
console.log "User downloaded"
@downloadFriends(next)
, 1000
)
downloadFriends: (next) =>
console.log "Starting to download friends"
async.map([0...FRIENDS], @downloadFriend, next)
downloadFriend: (x, next) =>
console.log "Starting to download friend #{x}"
@s3.getObject Bucket:config.BUCKET, Key:config.UID, (err, data) ->
return console.log err if err?
console.log "Friend downloaded"
next()
if cluster.isMaster
console.log("starting at master process...")
cluster.fork() for [0...4]
console.log "init"
new TestSpeed().start (err, result) =>
return console.trace err if err?
console.log "OK"
What I expected to happen is node to use all the bandwidth available as I'm creating 4 processes in a 4 core machine. 我预期会发生的事情是,节点将在4台核心计算机上创建4个进程时使用所有可用带宽。 But what I get is a download of around 50Mbps, that doubles to 100Mbps with 8 processes (more than the cores available!).
但是我得到的下载速度约为50Mbps,通过8个进程(超过可用的内核!)翻了一番,达到100Mbps。
I thought what node.js uses all the resources available, but not the CPU nor the network are working at 100%. 我以为node.js使用的是所有可用资源,但CPU和网络都没有100%正常工作。 What am I missing?
我想念什么?
I guess you're limited by maxSockets. 我想你受到maxSockets的限制。
Try require('http').globalAgent.maxSockets = 1000
at the beginning of your file. 在文件开头尝试
require('http').globalAgent.maxSockets = 1000
。
Look at this question Optimizing Node.js for a large number of outbound HTTP requests? 看看这个问题, 针对大量出站HTTP请求优化Node.js吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.