node.js Socket.IO 服务器性能和带宽使用情况

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18907591/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 15:41:55  来源:igfitidea点击:

Socket.IO server performance and bandwidth usage

javascriptnode.jsperformancesocket.iobandwidth

提问by Joe Boris

I'm about to host a small socket server on a local computer and I'd like to know what kind of bandwidth it's going to use. On most days it will have no more than 50 clients connected at once, but once or twice a week it could have as many as 5,000+ clients at once. However, the only messages sent will be an occasional single message to all connected clients at once with no extra data or anything.

我即将在本地计算机上托管一个小型套接字服务器,我想知道它将使用什么样的带宽。在大多数日子里,它一次连接的客户端不会超过 50 个,但每周一次或两次它可以同时连接多达 5,000 多个客户端。但是,发送的唯一消息将是偶尔向所有连接的客户端发送一条消息,没有额外的数据或任何东西。

Will the server cause a significant drop in performance on the computer it's hosted on or slow down my internet speeds at all?

服务器是否会导致托管它的计算机的性能显着下降或完全降低我的互联网速度?

Server.js:

服务器.js:

var app = require('http').createServer(handler)
   , io = require('socket.io').listen(app)
   , fs = require('fs')

 app.listen(8001);

function handler (req, res) {
fs.readFile(__dirname + '/index.html',
  function (err, data) {
    if (err) {
      res.writeHead(500);
      return res.end('Error loading index.html');
    }

    res.writeHead(200);
    res.end(data);
  });
}

io.sockets.on('connection', function (socket) {
  socket.on('SendDefault', function(data) {
    socket.broadcast.emit('GetDefault');
  });
});

Client.js:

客户端.js:

setTimeout( function( ){ 
  socket = io.connect('[IP Address]:8001');
  socket.on('GetDefault', function(data) {
    DoStuff( );
  );
} ); }, 10000 );

回答by hexacyanide

The amount of bandwidth will depend heavily on the amount of data you're going to send from the server, and how much data the client will send. The bandwidth usage will also depend on which Socket.IO transport you're using, and the heartbeat interval of your application.

带宽量在很大程度上取决于您将从服务器发送的数据量以及客户端将发送的数据量。带宽使用情况还取决于您使用的 Socket.IO 传输以及应用程序的心跳间隔。

The performance impact of the application also varies on the type of application you're running and the performance capability of your machine and/or network. However, 5000+ clients will have a considerable impact on performance, regardless of your computer's capabilities unless you are scaling the application across multiple cores.

应用程序的性能影响还因您正在运行的应用程序类型以及您的机器和/或网络的性能能力而异。但是,5000 多个客户端将对性能产生相当大的影响,无论您的计算机功能如何,除非您跨多个内核扩展应用程序。

I've taken some measurements using a proxy. Here are the results:

我使用代理进行了一些测量。结果如下:

Emitting from a client: socket.emit(event, args)

从客户端发出socket.emit(event, args)

  • If eventand argsare not supplied, 12 bytes are sent to the server.
  • If argsis omitted but eventis supplied, the total size is 22 bytes and the length of event.
  • If argsand eventare supplied, the same rules are followed, but the results may vary depending on the data type of args.
  • 如果eventargs未提供,12个字节被发送到服务器。
  • 如果args省略但event提供了,则总大小为 22 字节,长度为event.
  • 如果提供argsevent,则遵循相同的规则,但结果可能因 的数据类型而异args

Emitting from the server: same format as from client

从服务器发出:与从客户端相同的格式

  • If eventand argsare not supplied, 8 bytes are sent to the client.
  • If argsis omitted but eventis supplied, the total size is 17 bytes and the length of event.
  • If argsand eventare supplied, the same rules are followed, but the results may vary depending on the data type of args.
  • 如果eventargs未提供,则向客户端发送 8 个字节。
  • 如果args省略但event提供,则总大小为 17 个字节,长度为event.
  • 如果提供argsevent,则遵循相同的规则,但结果可能因 的数据类型而异args

Server to client heartbeat: every 25 seconds per client

服务器到客户端的心跳:每个客户端每 25 秒一次

  • 5 bytes from server
  • 9 bytes client response
  • 来自服务器的 5 个字节
  • 9 字节客户端响应

Handshaking: once per client

握手:每个客户一次

  • 216 bytes from server
  • 431 bytes response from client
  • 129 bytes follow up from server
  • 来自服务器的 216 字节
  • 来自客户端的 431 字节响应
  • 从服务器跟进 129 字节

Therefore with a load of 5000+ clients, expect at least 3.7MB for handshaking, 3KB/s for heartbeats, and at least 107KB bandwidth for a socket.emit(). These are not exact figures, as clients can lose data, drop connections, need to reconnect, etc.

因此,对于 5000 多个客户端的负载,预计至少有 3.7MB 的握手、3KB/s 的心跳以及至少 107KB 的socket.emit(). 这些不是准确的数字,因为客户端可能会丢失数据、断开连接、需要重新连接等。

Conclusively, your network will probably hold up, but the main concern should be the amount of concurrent connections your network will have to handle. Many concurrent connections can also be CPU intensive, so you should think about clustering across cores. Also keep in mind the amount of heartbeats the Socket.IO server will have to handle. With 50 concurrent users, that's an average of 2 heartbeats per second. At 5000+ concurrent users, that's 200+ heartbeats per second, which I'd imagine is more CPU intensive than network intensive (2.8KB/s).

最后,您的网络可能会支持,但主要的问题应该是您的网络必须处理的并发连接数量。许多并发连接也可能是 CPU 密集型的,因此您应该考虑跨内核集群。还要记住 Socket.IO 服务器必须处理的心跳数量。有 50 个并发用户时,平均每秒 2 次心跳。在 5000 多个并发用户时,即每秒 200 多个心跳,我认为 CPU 密集型比网络密集型(2.8KB/s)更多。

回答by Jesse Fulton

WebSockets can stay open for a very long time, so handling a large number of concurrent connections usually means you'll need to scale out that service to accommodate for the increased load. This is the same for almost any technology, but there's usually a limit to the max number of open connections a server can handle before things go downhill quickly. If you're likely to have such peaks in traffic, I'd consider looking into a 3rd party service like pusheror kaazing(disclaimer: I haven't tried either yet.)

WebSockets 可以保持打开很长时间,因此处理大量并发连接通常意味着您需要扩展该服务以适应增加的负载。这对于几乎任何技术都是一样的,但是在事情迅速走下坡路之前,服务器可以处理的最大打开连接数通常是有限的。如果您可能会遇到这样的流量高峰,我会考虑研究像pusherkaazing这样的 3rd 方服务(免责声明:我还没有尝试过。)

You've posted a pretty vague question (we know nothing about your application, architecture, etc. - just expected traffic), but hopefully that helps point you in the right direction. That being said... based upon your use case (broadcasting one or two small messages, occasionally), my gut is telling me that WebSockets are not the right technology for you.

您发布了一个非常模糊的问题(我们对您的应用程序、架构等一无所知 - 只是预期的流量),但希望这有助于您指明正确的方向。话虽如此……根据您的用例(偶尔广播一两条小消息),我的直觉告诉我 WebSockets 不是适合您的技术。

(Note that bandwidth probably shouldn't be a concern - generally speaking if you were to send many messages over WebSockets vs. REST you'll be sending less data due to headers, cookies, etc.)

(请注意,带宽可能不应该是一个问题——一般来说,如果您要通过 WebSockets 与 REST 发送许多消息,由于标头、cookie 等,您将发送更少的数据。)