Socket.IO服务器性能和带宽使用情况 [英] Socket.IO server performance and bandwidth usage

查看:357
本文介绍了Socket.IO服务器性能和带宽使用情况的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

服务器是否会导致性能显着下降它托管的电脑或者我的互联网速度慢下来?

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:

Server.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:

Client.js:

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


推荐答案

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

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.

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

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.

我已经使用了一些测量代理人。以下是结果:

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

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


  • 如果事件 args 未提供,12个字节被发送到服务器。

  • 如果 args 被省略但是<$ c $提供了c> event ,总大小为22个字节,长度为 event

  • 如果提供 args 事件,遵循相同的规则,但结果可能会因<的数据类型而异code> args 。

  • If event and args are not supplied, 12 bytes are sent to the server.
  • If args is omitted but event is supplied, the total size is 22 bytes and the length of event.
  • If args and event are supplied, the same rules are followed, but the results may vary depending on the data type of args.

从服务器发出:相同格式来自客户


  • 如果事件 args 未提供,8个字节被发送到客户端。

  • 如果 args 被省略但是<$ c提供$ c> event ,总大小为17个字节,长度为 event

  • 如果 args 事件是sup plied,遵循相同的规则,但结果可能会有所不同,具体取决于 args 的数据类型。

  • If event and args are not supplied, 8 bytes are sent to the client.
  • If args is omitted but event is supplied, the total size is 17 bytes and the length of event.
  • If args and event are supplied, the same rules are followed, but the results may vary depending on the data type of args.

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

Server to client heartbeat: every 25 seconds per client


  • 服务器5个字节

  • 9字节客户响应

握手:每个客户一次


  • 来自服务器的216字节

  • 来自客户的431字节响应

  • 从服务器跟进129字节

因此,如果负载超过5000个客户端,预计握手至少需要3.7MB,3KB / s表示心跳,至少107KB带宽表示 socket.emit()。这些不是确切的数字,因为客户可能会丢失数据,断开连接,需要重新连接等等。

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.

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

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).

这篇关于Socket.IO服务器性能和带宽使用情况的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆