【问题标题】:How does a NodeJS app with multiple timers scale?具有多个计时器的 NodeJS 应用程序如何扩展?
【发布时间】:2016-07-31 06:50:49
【问题描述】:

我的应用在任何给定时刻最多需要 1000 个计时器。计时器会消耗大量资源吗?部署多个计时器是一种公认​​的做法吗?还是应该避免?

【问题讨论】:

  • 大约需要 10 行代码来为 10,000 个计时器编写一个快速测试,每个计时器设置的时间略有不同,并查看它的工作情况。

标签: node.js performance timer scaling


【解决方案1】:

根据 @jfriend00 的建议,我在下面做了一个示例检查,这可能不准确(由于 dom 操作),但希望它能给您提供概念

// let's use 2 measurings, window.performance.now and console.time

// start console.time, usually it gives same result as window.perf.now
// but window.perf.now uses 10 decimal points
console.time('timer_perf_test');
var now1 = window.performance.now();

// our count, this test really lags when 100,000
// CHANGE THIS 
var count = 10000;

// some dom elements for text
var elem = document.querySelector('img');
var counter = document.querySelector('#counter');
var perf = document.querySelector('#perf');

// how smooth our image gonna rotate?
var rotate = function(degree){
    elem.style.transform = 'rotate(' + degree +'deg)';
    counter.textContent = 'timers executed: ' + degree;
}

// create bunch of timers with different timeout
var timer = function(added_time){
    setTimeout(function(){
    	rotate(added_time);
    }, 1000 + (added_time * 10));
}
// test begins
for (var i = 0; i < count; i++) {
    timer(i)
}

// check results
var now2 = window.performance.now();
perf.textContent = now2 - now1 + ' MS required to create ' + count + ' timers';
console.timeEnd('timer_perf_test');
<img src="https://km.support.apple.com/library/APPLE/APPLECARE_ALLGEOS/HT4211/HT4211-ios7-activity_icon-001-en.png" width="100" height="100">
<p id="counter"></p>
<p id="perf"></p>

【讨论】:

    猜你喜欢
    • 2015-05-23
    • 1970-01-01
    • 1970-01-01
    • 2018-06-07
    • 1970-01-01
    • 2018-08-16
    • 2023-04-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多