Skip to content

Commit 4f03851

Browse files
Add MessageIO.readMessage and MessageIO.writeMessage.
1 parent 2ecdc11 commit 4f03851

File tree

6 files changed

+747
-3
lines changed

6 files changed

+747
-3
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
const { createBenchmark } = require('../common');
2+
const { Readable } = require('stream');
3+
4+
const Debug = require('tedious/lib/debug');
5+
const IncomingMessageStream = require('tedious/lib/incoming-message-stream');
6+
const { Packet } = require('tedious/lib/packet');
7+
8+
const bench = createBenchmark(main, {
9+
n: [100, 1000, 10000, 100000]
10+
});
11+
12+
function main({ n }) {
13+
const debug = new Debug();
14+
15+
const stream = Readable.from((async function*() {
16+
for (let i = 0; i < n; i++) {
17+
const packet = new Packet(2);
18+
packet.last(true);
19+
packet.addData(Buffer.from([1, 2, 3, 4, 5, 6, 7, 8, 9]));
20+
21+
yield packet.buffer;
22+
}
23+
})());
24+
25+
const incoming = new IncomingMessageStream(debug);
26+
stream.pipe(incoming);
27+
28+
bench.start();
29+
console.profile('incoming-message-stream');
30+
31+
(async function() {
32+
let total = 0;
33+
34+
for await (m of incoming) {
35+
for await (const buf of m) {
36+
total += buf.length;
37+
}
38+
}
39+
40+
console.profileEnd('incoming-message-stream');
41+
bench.end(n);
42+
})();
43+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
const { createBenchmark } = require('../common');
2+
const { Duplex } = require('stream');
3+
4+
const Debug = require('../../lib/debug');
5+
const OutgoingMessageStream = require('../../lib/outgoing-message-stream');
6+
const Message = require('../../lib/message');
7+
8+
const bench = createBenchmark(main, {
9+
n: [100, 1000, 10000, 100000]
10+
});
11+
12+
function main({ n }) {
13+
const debug = new Debug();
14+
15+
const stream = new Duplex({
16+
read() {},
17+
write(chunk, encoding, callback) {
18+
// Just consume the data
19+
callback();
20+
}
21+
});
22+
23+
const payload = [
24+
Buffer.alloc(1024),
25+
Buffer.alloc(1024),
26+
Buffer.alloc(1024),
27+
Buffer.alloc(256),
28+
Buffer.alloc(256),
29+
Buffer.alloc(256),
30+
Buffer.alloc(256),
31+
];
32+
33+
const out = new OutgoingMessageStream(debug, {
34+
packetSize: 8 + 1024
35+
});
36+
out.pipe(stream);
37+
38+
bench.start();
39+
console.profile('write-message');
40+
41+
function writeNextMessage(i) {
42+
if (i == n) {
43+
out.end();
44+
out.once('finish', () => {
45+
console.profileEnd('write-message');
46+
bench.end(n);
47+
});
48+
return;
49+
}
50+
51+
const m = new Message({ type: 2, resetConnection: false });
52+
out.write(m);
53+
54+
for (const buf of payload) {
55+
m.write(buf);
56+
}
57+
58+
m.end();
59+
60+
if (out.needsDrain) {
61+
out.once('drain', () => {
62+
writeNextMessage(i + 1);
63+
});
64+
} else {
65+
process.nextTick(() => {
66+
writeNextMessage(i + 1);
67+
});
68+
}
69+
}
70+
71+
writeNextMessage(0);
72+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
const { createBenchmark } = require('../common');
2+
const { Readable } = require('stream');
3+
4+
const Debug = require('tedious/lib/debug');
5+
const MessageIO = require('tedious/lib/message-io');
6+
const { Packet } = require('tedious/lib/packet');
7+
8+
const bench = createBenchmark(main, {
9+
n: [100, 1000, 10000, 100000]
10+
});
11+
12+
function main({ n }) {
13+
const debug = new Debug();
14+
15+
const stream = Readable.from((async function*() {
16+
for (let i = 0; i < n; i++) {
17+
const packet = new Packet(2);
18+
packet.last(true);
19+
packet.addData(Buffer.from([1, 2, 3, 4, 5, 6, 7, 8, 9]));
20+
21+
yield packet.buffer;
22+
}
23+
})());
24+
25+
(async function() {
26+
bench.start();
27+
console.profile('read-message');
28+
29+
let total = 0;
30+
for (let i = 0; i < n; i++) {
31+
for await (const chunk of MessageIO.readMessage(stream, debug)) {
32+
total += chunk.length;
33+
}
34+
}
35+
36+
console.profileEnd('read-message');
37+
bench.end(n);
38+
})();
39+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
const { createBenchmark, createConnection } = require('../common');
2+
const { Duplex } = require('stream');
3+
4+
const Debug = require('tedious/lib/debug');
5+
const MessageIO = require('tedious/lib/message-io');
6+
7+
const bench = createBenchmark(main, {
8+
n: [100, 1000, 10000, 100000]
9+
});
10+
11+
function main({ n }) {
12+
const debug = new Debug();
13+
14+
const stream = new Duplex({
15+
read() {},
16+
write(chunk, encoding, callback) {
17+
// Just consume the data
18+
callback();
19+
}
20+
});
21+
22+
const payload = [
23+
Buffer.alloc(1024),
24+
Buffer.alloc(1024),
25+
Buffer.alloc(1024),
26+
Buffer.alloc(256),
27+
Buffer.alloc(256),
28+
Buffer.alloc(256),
29+
Buffer.alloc(256),
30+
];
31+
32+
(async function() {
33+
bench.start();
34+
console.profile('bench');
35+
36+
for (let i = 0; i <= n; i++) {
37+
await MessageIO.writeMessage(stream, debug, 8 + 1024, 2, payload);
38+
}
39+
40+
console.profileEnd('bench');
41+
bench.end(n);
42+
})();
43+
}

0 commit comments

Comments
 (0)