From 1a6df1139a1cd927f63f923399c3fa3ce53db448 Mon Sep 17 00:00:00 2001 From: Thiago Santos Date: Thu, 25 Jun 2020 10:20:40 -0300 Subject: [PATCH 1/4] proposing Queue use instead of array Signed-off-by: Thiago Santos --- lib/sender.js | 3 ++- package.json | 3 ++- test/test.queue-performance.js | 33 +++++++++++++++++++++++++++++++++ test/test.sender.js | 6 +++--- 4 files changed, 40 insertions(+), 5 deletions(-) create mode 100644 test/test.queue-performance.js diff --git a/lib/sender.js b/lib/sender.js index 1668cb1..83b53b3 100644 --- a/lib/sender.js +++ b/lib/sender.js @@ -8,6 +8,7 @@ const tls = require('tls'); const zlib = require('zlib'); const FluentLoggerError = require('./logger-error'); const EventTime = require('./event-time'); +const Queue = require('tiny-queue'); const codec = msgpack.createCodec(); codec.addExtPacker(0x00, EventTime, EventTime.pack); @@ -35,7 +36,7 @@ class FluentSender { this._timeResolution = options.milliseconds ? 1 : 1000; this._socket = null; if (this._eventMode === 'Message') { - this._sendQueue = []; // queue for items waiting for being sent. + this._sendQueue = new Queue(); // queue for items waiting for being sent. this._flushInterval = 0; this._messageQueueSizeLimit = options.messageQueueSizeLimit || 0; } else { diff --git a/package.json b/package.json index f9c13ec..3cdb1f2 100644 --- a/package.json +++ b/package.json @@ -31,7 +31,8 @@ "node": ">=6" }, "dependencies": { - "msgpack-lite": "*" + "msgpack-lite": "*", + "tiny-queue": "^0.2.1" }, "devDependencies": { "async": "*", diff --git a/test/test.queue-performance.js b/test/test.queue-performance.js new file mode 100644 index 0000000..ae9e93b --- /dev/null +++ b/test/test.queue-performance.js @@ -0,0 +1,33 @@ +/* eslint-disable no-console */ +'use strict'; +/* globals describe, it */ +/* eslint node/no-unpublished-require: ["error", {"allowModules": ["chai"]}] */ +const expect = require('chai').expect; +const Queue = require('tiny-queue'); + +describe('queue performance', () => { + it('should be more than 100 times faster than array for large lengths', () => { + const array = []; + const queue = new Queue(); + for (let i = 1; i < 100000; i++) { + array.push(i); + queue.push(i); + } + + + const startQueue = process.hrtime(); + while (queue.length > 0) { + queue.shift(); + } + const totalQueue = process.hrtime(startQueue); + const startArray = process.hrtime(); + while (array.length > 0) { + array.shift(); + } + const totalArray = process.hrtime(startArray); + + console.log(`Array time: ${totalArray[0] + totalArray[1] / 1e9}`); + console.log(`Queue time: ${totalQueue[0] + totalQueue[1] / 1e9}`); + expect(totalArray[0] + totalArray[1] / 1e9 > (totalQueue[0] + totalQueue[1] / 1e9) * 100).to.be.true; + }); +}) \ No newline at end of file diff --git a/test/test.sender.js b/test/test.sender.js index 3d7bc45..f6056af 100644 --- a/test/test.sender.js +++ b/test/test.sender.js @@ -960,9 +960,9 @@ let doTest = (tls) => { s.emit('message3', {}); s.emit('message4', {}); expect(s._sendQueue.length).to.be.equal(3); - expect(s._sendQueue[0].tag).to.be.equal('debug.message2'); - expect(s._sendQueue[1].tag).to.be.equal('debug.message3'); - expect(s._sendQueue[2].tag).to.be.equal('debug.message4'); + expect(s._sendQueue.shift().tag).to.be.equal('debug.message2'); + expect(s._sendQueue.shift().tag).to.be.equal('debug.message3'); + expect(s._sendQueue.shift().tag).to.be.equal('debug.message4'); done(); }); }); From d47e97871c43d1caba3d6a4e47bcdad96b6a90bc Mon Sep 17 00:00:00 2001 From: Thiago Santos Date: Thu, 25 Jun 2020 10:27:32 -0300 Subject: [PATCH 2/4] adding test case for irrelevant size Signed-off-by: Thiago Santos --- test/test.queue-performance.js | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/test/test.queue-performance.js b/test/test.queue-performance.js index ae9e93b..b229415 100644 --- a/test/test.queue-performance.js +++ b/test/test.queue-performance.js @@ -30,4 +30,29 @@ describe('queue performance', () => { console.log(`Queue time: ${totalQueue[0] + totalQueue[1] / 1e9}`); expect(totalArray[0] + totalArray[1] / 1e9 > (totalQueue[0] + totalQueue[1] / 1e9) * 100).to.be.true; }); + + it('the time difference between array.shift and queue.shift should be irrelevant for small lengths', () => { + const array = []; + const queue = new Queue(); + for (let i = 1; i < 10000; i++) { + array.push(i); + queue.push(i); + } + + + const startQueue = process.hrtime(); + while (queue.length > 0) { + queue.shift(); + } + const totalQueue = process.hrtime(startQueue); + const startArray = process.hrtime(); + while (array.length > 0) { + array.shift(); + } + const totalArray = process.hrtime(startArray); + + console.log(`Array time: ${totalArray[0] + totalArray[1] / 1e9}`); + console.log(`Queue time: ${totalQueue[0] + totalQueue[1] / 1e9}`); + expect(totalArray[0] + totalArray[1] / 1e9 - (totalQueue[0] + totalQueue[1] / 1e9) < 0.001).to.be.true; + }); }) \ No newline at end of file From e83d73a0cbfcbba41452cd0f7ba9c5a05a74a9e0 Mon Sep 17 00:00:00 2001 From: Thiago Santos Date: Thu, 25 Jun 2020 10:41:06 -0300 Subject: [PATCH 3/4] fixing semicolon Signed-off-by: Thiago Santos --- test/test.queue-performance.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test.queue-performance.js b/test/test.queue-performance.js index b229415..347d520 100644 --- a/test/test.queue-performance.js +++ b/test/test.queue-performance.js @@ -55,4 +55,4 @@ describe('queue performance', () => { console.log(`Queue time: ${totalQueue[0] + totalQueue[1] / 1e9}`); expect(totalArray[0] + totalArray[1] / 1e9 - (totalQueue[0] + totalQueue[1] / 1e9) < 0.001).to.be.true; }); -}) \ No newline at end of file +}); \ No newline at end of file From ad3890cbabc17455bdac0c742310603fc15de854 Mon Sep 17 00:00:00 2001 From: Thiago Santos Date: Thu, 25 Jun 2020 11:02:22 -0300 Subject: [PATCH 4/4] fixing mocha version as the last version no longer supports node 6 Signed-off-by: Thiago Santos --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 3cdb1f2..799d5f2 100644 --- a/package.json +++ b/package.json @@ -39,7 +39,7 @@ "chai": "*", "eslint": "^5.1.0", "eslint-plugin-node": "*", - "mocha": "*", + "mocha": "^6.0.0", "selfsigned": "*", "winston": "*" },