From 869460aa5826e4d61f5e35879815efc7e9f3848b Mon Sep 17 00:00:00 2001 From: SindreXie Date: Wed, 7 Sep 2022 11:11:29 +0800 Subject: [PATCH] benchmark: add stream destroy benchmark PR-URL: https://github.com/nodejs/node/pull/44533 Reviewed-By: Robert Nagy Reviewed-By: Matteo Collina Reviewed-By: Erick Wendel --- benchmark/streams/destroy.js | 56 ++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 benchmark/streams/destroy.js diff --git a/benchmark/streams/destroy.js b/benchmark/streams/destroy.js new file mode 100644 index 00000000000..c6811139d8c --- /dev/null +++ b/benchmark/streams/destroy.js @@ -0,0 +1,56 @@ +'use strict'; +const common = require('../common.js'); +const { + Duplex, + Readable, + Transform, + Writable, +} = require('stream'); + +const bench = common.createBenchmark(main, { + n: [1e6], + kind: ['duplex', 'readable', 'transform', 'writable'] +}); + +function main({ n, kind }) { + switch (kind) { + case 'duplex': + new Duplex({}); + new Duplex(); + + bench.start(); + for (let i = 0; i < n; ++i) + new Duplex().destroy(); + bench.end(n); + break; + case 'readable': + new Readable({}); + new Readable(); + + bench.start(); + for (let i = 0; i < n; ++i) + new Readable().destroy(); + bench.end(n); + break; + case 'writable': + new Writable({}); + new Writable(); + + bench.start(); + for (let i = 0; i < n; ++i) + new Writable().destroy(); + bench.end(n); + break; + case 'transform': + new Transform({}); + new Transform(); + + bench.start(); + for (let i = 0; i < n; ++i) + new Transform().destroy(); + bench.end(n); + break; + default: + throw new Error('Invalid kind'); + } +}