Skip to content

Commit

Permalink
fixup: add bench
Browse files Browse the repository at this point in the history
  • Loading branch information
ronag committed Jan 5, 2022
1 parent b163d6c commit 34cc4e0
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
69 changes: 69 additions & 0 deletions bench/bench.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import net from 'net';
import cronometro from 'cronometro';

const server = net
.createServer((socket) => {
socket.resume();
socket.on('error', () => {});
})
.listen(0);

function toBuffer(data) {
toBuffer.readOnly = true;

if (Buffer.isBuffer(data)) return data;

let buf;

if (data instanceof ArrayBuffer) {
buf = Buffer.from(data);
} else if (ArrayBuffer.isView(data)) {
buf = Buffer.from(data.buffer, data.byteOffset, data.byteLength);
} else {
buf = Buffer.from(data);
toBuffer.readOnly = false;
}

return buf;
}

const str = '1'.repeat(4096);

cronometro(
{
string(cb) {
const socket = net.connect(server.address().port, '0.0.0.0', () => {
for (let n = 0; n < 1024; ++n) {
socket.write(str);
}
socket.end();
socket.on('finish', () => {
socket.on('error', () => {}).destroy();
cb();
});
});
},
toBuffer(cb) {
const socket = net.connect(server.address().port, '0.0.0.0', () => {
for (let n = 0; n < 1024; ++n) {
socket.write(toBuffer(str));
}
socket.end();
socket.on('finish', () => {
socket.on('error', () => {}).destroy();
cb();
});
});
}
},
{
print: { compare: true }
},
(err, results) => {
if (err) {
throw err;
}

console.log(JSON.stringify(results, null, 2));
}
);
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"devDependencies": {
"benchmark": "^2.1.4",
"bufferutil": "^4.0.1",
"cronometro": "^1.0.2",
"eslint": "^8.0.0",
"eslint-config-prettier": "^8.1.0",
"eslint-plugin-prettier": "^4.0.0",
Expand Down

0 comments on commit 34cc4e0

Please sign in to comment.