From 65a2e805968d3b0aac0340863c2bf1f1575167a6 Mon Sep 17 00:00:00 2001 From: Benedikt Meurer Date: Mon, 3 Jul 2017 11:52:37 +0200 Subject: [PATCH] benchmark: Improve event performance tests. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Currently most of the event tests only test a single event type, which might let those benchmark take fast-paths (in V8) that aren't taken in realistic use cases, and thus the benchmarks are not good proxies of real world uses. PR-URL: https://github.com/nodejs/node/pull/14052 Reviewed-By: Matteo Collina Reviewed-By: Timothy Gu Reviewed-By: Michaƫl Zasso --- benchmark/events/ee-add-remove.js | 11 +++++++---- benchmark/events/ee-listener-count-on-prototype.js | 9 ++++++--- benchmark/events/ee-listeners-many.js | 9 ++++++--- benchmark/events/ee-listeners.js | 9 ++++++--- benchmark/events/ee-once.js | 5 +++-- 5 files changed, 28 insertions(+), 15 deletions(-) diff --git a/benchmark/events/ee-add-remove.js b/benchmark/events/ee-add-remove.js index 99d85367cb8d6f..1140a81649f9a6 100644 --- a/benchmark/events/ee-add-remove.js +++ b/benchmark/events/ee-add-remove.js @@ -16,10 +16,13 @@ function main(conf) { bench.start(); for (var i = 0; i < n; i += 1) { - for (k = listeners.length; --k >= 0; /* empty */) - ee.on('dummy', listeners[k]); - for (k = listeners.length; --k >= 0; /* empty */) - ee.removeListener('dummy', listeners[k]); + var dummy = (i % 2 === 0) ? 'dummy0' : 'dummy1'; + for (k = listeners.length; --k >= 0; /* empty */) { + ee.on(dummy, listeners[k]); + } + for (k = listeners.length; --k >= 0; /* empty */) { + ee.removeListener(dummy, listeners[k]); + } } bench.end(n); } diff --git a/benchmark/events/ee-listener-count-on-prototype.js b/benchmark/events/ee-listener-count-on-prototype.js index dfe7e27cd09144..269fd5a493ee34 100644 --- a/benchmark/events/ee-listener-count-on-prototype.js +++ b/benchmark/events/ee-listener-count-on-prototype.js @@ -9,12 +9,15 @@ function main(conf) { var ee = new EventEmitter(); - for (var k = 0; k < 10; k += 1) - ee.on('dummy', function() {}); + for (var k = 0; k < 5; k += 1) { + ee.on('dummy0', function() {}); + ee.on('dummy1', function() {}); + } bench.start(); for (var i = 0; i < n; i += 1) { - ee.listenerCount('dummy'); + var dummy = (i % 2 === 0) ? 'dummy0' : 'dummy1'; + ee.listenerCount(dummy); } bench.end(n); } diff --git a/benchmark/events/ee-listeners-many.js b/benchmark/events/ee-listeners-many.js index 063732e1facb4b..09e533de32bf9a 100644 --- a/benchmark/events/ee-listeners-many.js +++ b/benchmark/events/ee-listeners-many.js @@ -10,12 +10,15 @@ function main(conf) { var ee = new EventEmitter(); ee.setMaxListeners(101); - for (var k = 0; k < 100; k += 1) - ee.on('dummy', function() {}); + for (var k = 0; k < 50; k += 1) { + ee.on('dummy0', function() {}); + ee.on('dummy1', function() {}); + } bench.start(); for (var i = 0; i < n; i += 1) { - ee.listeners('dummy'); + var dummy = (i % 2 === 0) ? 'dummy0' : 'dummy1'; + ee.listeners(dummy); } bench.end(n); } diff --git a/benchmark/events/ee-listeners.js b/benchmark/events/ee-listeners.js index e91ca5078f5f98..56c0e85dad41a4 100644 --- a/benchmark/events/ee-listeners.js +++ b/benchmark/events/ee-listeners.js @@ -9,12 +9,15 @@ function main(conf) { var ee = new EventEmitter(); - for (var k = 0; k < 10; k += 1) - ee.on('dummy', function() {}); + for (var k = 0; k < 5; k += 1) { + ee.on('dummy0', function() {}); + ee.on('dummy1', function() {}); + } bench.start(); for (var i = 0; i < n; i += 1) { - ee.listeners('dummy'); + var dummy = (i % 2 === 0) ? 'dummy0' : 'dummy1'; + ee.listeners(dummy); } bench.end(n); } diff --git a/benchmark/events/ee-once.js b/benchmark/events/ee-once.js index 29ea562c406549..56f7fb9e7c3a9e 100644 --- a/benchmark/events/ee-once.js +++ b/benchmark/events/ee-once.js @@ -13,8 +13,9 @@ function main(conf) { bench.start(); for (var i = 0; i < n; i += 1) { - ee.once('dummy', listener); - ee.emit('dummy'); + var dummy = (i % 2 === 0) ? 'dummy0' : 'dummy1'; + ee.once(dummy, listener); + ee.emit(dummy); } bench.end(n); }