简体   繁体   English

调用node.js中可写流的方法write()的速度有多快?

[英]How fast can the method write() of writable stream in node.js be called?

On my server side I use writable stream to record the changing data of all the clients that are connected to the server. 在我的服务器端,我使用可写流来记录连接到服务器的所有客户端的更改数据。 Here is what I wrote: 这是我写的:

function updateLoop () {
    var data = {'timeStep': timeStep, 'playerInfo': playerInfo};
    var text = JSON.stringify(data);
    writeStram.write(text + '\n');
    timeStep += 1;
}
...
updateTimer = setInterval(updateLoop, 50);

So the function updateLoop() is called every 50ms. 因此每50ms调用一次函数updateLoop()。

It works when 'playerInfo' is small (less than 1KB), for example, after the server side running for 20s, there are 20*1000/50 = 400 lines of data in the output file 当'playerInfo'很小(小于1KB)时它可以工作,例如,在服务器端运行20s后,输出文件中有20 * 1000/50 = 400行数据

But when 'playerInfo' became larger, like 6KB or more, after the server side running for 20s, there only 220 lines of data in the output file. 但是当'playerInfo'变大时,如6KB或更多,在服务器端运行20s后,输出文件中只有220行数据。 The larger playerInfo is, the lesser lines of successfully recorded data in the output file there is. 较大的playerInfo是输出文件中成功记录数据的较小行。

I am wondering if there is some limit to the calling rate of the method write() 我想知道方法write()的调用率是否有一些限制

Thanks a lot for your guys' help! 非常感谢你们的帮助!

The issue likely isn't specifically with write , but with setInterval and how timers work in JavaScript. 这个问题可能不是专门针对write ,而是使用setInterval以及定时器如何在JavaScript中工作

With the larger data, updateLoop is probably taking longer to finish executing than the set delay of 50 ms. 对于较大的数据, updateLoop可能需要更长的时间才能完成执行,而不是50 ms的设置延迟。 If it take long enough that the intervals overlap each other, where one would be added to the event queue when another is already waiting, the newer interval is skipped (or "dropped"). 如果间隔足够长,使得间隔彼此重叠,当另一个间隔已经等待时,将在事件队列中添加一个间隔,则跳过(或“丢弃”)更新的间隔。

How fast can the method write() of writable stream in node.js be called? 调用node.js中可写流的方法write()的速度有多快?

write shouldn't explicitly throttle performance, but it can be limited by the overall performance Node can achieve on your computer (affected by CPU, RAM, etc.). write不应该明确地限制性能,但它可以受到Node在计算机上可以实现的整体性能的限制(受CPU,RAM等影响)。

You'll have to benchmark and compare the performance of updateLoop with varying amounts of playerInfo . 您必须对updateLoop的性能进行基准测试并将其与不同数量的playerInfo There are a few packages you can use for this, including ben : 你可以使用一些软件包 ,包括ben

ben(1000, updateLoop, function (ms) {
    console.log(ms, 'milliseconds per iteration');
});

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

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