简体   繁体   English

对于node.js,cpu的密集程度太高了(担心阻塞事件循环)

[英]How cpu intensive is too much for node.js (worried about blocking event loop)

I am writing a node app with various communities and within those communities users are able to create and join rooms/lobbies. 我正在与各种社区一起编写节点应用程序,在这些社区中,用户可以创建并加入房间/大厅。 I have written the logic for these lobbies into the node app itself though a collection of lobby objects. 我已经通过游说对象的集合将这些大厅的逻辑写入了节点应用程序本身。

Lobbies require some maintenance once created. 大厅创建后需要进行一些维护。 Users can change various statuses within the lobby and I also have calls using socket.io at regular intervals(about every 2 seconds) for each lobby to keep track of some user input "live". 用户可以在大厅内更改各种状态,我还可以使用socket.io以固定间隔(大约每2秒一次)为每个大厅打电话,以跟踪某些用户输入的“实时”。

None of the tasks are too cpu intensive. 这些任务都不会占用太多CPU。 The biggest potential threat I foresee is a load distributing algorithm but it is not one of the "live calls" and is only activated upon a button press by the lobby creator (it also is never performed on more than 10 things). 我预见到的最大潜在威胁是负载分配算法,但它不是“实时呼叫”之一,仅在游说者创建者按下按钮时才会激活(也绝不会执行超过10件事)。

My concern arises in that, in production, if the server starts to get close too around 100-200 lobbies I could be risking blocking the event loop. 我担心的是,在生产中,如果服务器在100-200个大厅附近开始变得太接近,我可能会冒阻塞事件循环的风险。 Are my concerns reasonable? 我的担心合理吗? Is the potential quantity of these operations, although they are small, large enough to warent offloading this code to a separate executable or getting involved with various franken-thread javascript libs? 这些操作的潜在数量(尽管很小)是否足够大,可以警告将这些代码分流到单独的可执行文件中,或者涉及各种Franken-Thread javascript库?

TL;DR: node app has object with regular small tasks run. TL; DR:节点应用具有运行常规小任务的对象。 Should I worry about event-loop blocking if many of these objects are made. 如果创建了许多这些对象,我是否应该担心事件循环阻塞。

There is no way to know ahead of time whether what you describe will "fill" up the event loop and take all the time one thread has or not. 无法提前知道您所描述的内容是否会“填满”事件循环并占用一个线程所有的时间。 If you want to "know", you will have to build a simulation and measure while using commensurate hardware with what you expect to use in production. 如果您想“知道”,则必须在使用与预期在生产中使用的硬件相当的硬件的同时进行仿真和测量。

With pretty much all performance questions, you have to measure, measure and measure again to really know or understand whether you have a problem and, if so, what is the main source of the problem. 对于几乎所有的性能问题,您都必须重新测量,重新测量,才能真正知道或理解您是否有问题,如果存在问题,那么问题的主要根源是什么。

For non-compute intensive things, your CPU can probably handle a lot of activity. 对于非计算密集型的事情,您的CPU可能可以处理很多活动。 If you get a lot of users all pounding transactions every two seconds though, you could end up with a bottleneck somewhere that causes issues. 但是,如果让很多用户每两秒钟就完成一次事务,那么最终可能会导致出现问题的瓶颈。 200 users with a transaction every 2 seconds means 100 transactions/sec which means if you take more than 10ms of CPU or of any other serialized resource (like perhaps the network card) per transaction, then you may have issues. 每2秒有200个用户进行事务处理,这意味着每秒100个事务处理,这意味着如果每个事务处理占用了10ms以上的CPU或任何其他序列化资源(例如网卡),那么您可能会遇到问题。

As for offloading some work to another process, I wouldn't spend much time worrying about that until you've measured whether you have an issue or not. 至于将一些工作转移到另一个流程,在您确定是否有问题之前,我不会花太多时间担心这一点。 If you do, then it will be very important to understand what the main cause of the issue is. 如果这样做,那么了解问题的主要原因是非常重要的。 It may make more sense to simply cluster your node.js processes to put multiple processes on the same server than it does to break your core logic into multiple processes. 简单地将您的node.js进程集群以将多个进程放在同一服务器上可能比将您的核心逻辑分解为多个进程更有意义。 It will all depend upon what the main cause of your issue is (if you even have an issue). 这完全取决于问题的主要原因(如果您甚至遇到问题)。 Or, you may end up needing multiple network cards. 或者,您可能最终需要多个网卡。 Or something else. 或者是其他东西。 It's far too early to know before measuring and understanding. 在进行测量和理解之前了解还为时过早。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM