Skip to content

Commit cf666d8

Browse files
BridgeARevanlucas
authored andcommitted
benchmark: (net) use destructuring
PR-URL: #18250 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 88f4bf2 commit cf666d8

8 files changed

+142
-238
lines changed

benchmark/net/net-c2s-cork.js

+24-36
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
'use strict';
33

44
const common = require('../common.js');
5+
const net = require('net');
56
const PORT = common.PORT;
67

78
const bench = common.createBenchmark(main, {
@@ -10,17 +11,10 @@ const bench = common.createBenchmark(main, {
1011
dur: [5],
1112
});
1213

13-
var dur;
14-
var len;
15-
var type;
1614
var chunk;
1715
var encoding;
1816

19-
function main(conf) {
20-
dur = +conf.dur;
21-
len = +conf.len;
22-
type = conf.type;
23-
17+
function main({ dur, len, type }) {
2418
switch (type) {
2519
case 'buf':
2620
chunk = Buffer.alloc(len, 'x');
@@ -37,34 +31,6 @@ function main(conf) {
3731
throw new Error(`invalid type: ${type}`);
3832
}
3933

40-
server();
41-
}
42-
43-
const net = require('net');
44-
45-
function Writer() {
46-
this.received = 0;
47-
this.writable = true;
48-
}
49-
50-
Writer.prototype.write = function(chunk, encoding, cb) {
51-
this.received += chunk.length;
52-
53-
if (typeof encoding === 'function')
54-
encoding();
55-
else if (typeof cb === 'function')
56-
cb();
57-
58-
return true;
59-
};
60-
61-
// doesn't matter, never emits anything.
62-
Writer.prototype.on = function() {};
63-
Writer.prototype.once = function() {};
64-
Writer.prototype.emit = function() {};
65-
Writer.prototype.prependListener = function() {};
66-
67-
function server() {
6834
const writer = new Writer();
6935

7036
// the actual benchmark.
@@ -95,3 +61,25 @@ function server() {
9561
});
9662
});
9763
}
64+
65+
function Writer() {
66+
this.received = 0;
67+
this.writable = true;
68+
}
69+
70+
Writer.prototype.write = function(chunk, encoding, cb) {
71+
this.received += chunk.length;
72+
73+
if (typeof encoding === 'function')
74+
encoding();
75+
else if (typeof cb === 'function')
76+
cb();
77+
78+
return true;
79+
};
80+
81+
// doesn't matter, never emits anything.
82+
Writer.prototype.on = function() {};
83+
Writer.prototype.once = function() {};
84+
Writer.prototype.emit = function() {};
85+
Writer.prototype.prependListener = function() {};

benchmark/net/net-c2s.js

+25-38
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
'use strict';
33

44
const common = require('../common.js');
5+
const net = require('net');
56
const PORT = common.PORT;
67

78
const bench = common.createBenchmark(main, {
@@ -10,17 +11,10 @@ const bench = common.createBenchmark(main, {
1011
dur: [5],
1112
});
1213

13-
var dur;
14-
var len;
15-
var type;
1614
var chunk;
1715
var encoding;
1816

19-
function main(conf) {
20-
dur = +conf.dur;
21-
len = +conf.len;
22-
type = conf.type;
23-
17+
function main({ dur, len, type }) {
2418
switch (type) {
2519
case 'buf':
2620
chunk = Buffer.alloc(len, 'x');
@@ -37,10 +31,30 @@ function main(conf) {
3731
throw new Error(`invalid type: ${type}`);
3832
}
3933

40-
server();
41-
}
34+
const reader = new Reader();
35+
const writer = new Writer();
4236

43-
const net = require('net');
37+
// the actual benchmark.
38+
const server = net.createServer(function(socket) {
39+
socket.pipe(writer);
40+
});
41+
42+
server.listen(PORT, function() {
43+
const socket = net.connect(PORT);
44+
socket.on('connect', function() {
45+
bench.start();
46+
47+
reader.pipe(socket);
48+
49+
setTimeout(function() {
50+
const bytes = writer.received;
51+
const gbits = (bytes * 8) / (1024 * 1024 * 1024);
52+
bench.end(gbits);
53+
process.exit(0);
54+
}, dur * 1000);
55+
});
56+
});
57+
}
4458

4559
function Writer() {
4660
this.received = 0;
@@ -84,30 +98,3 @@ Reader.prototype.pipe = function(dest) {
8498
this.flow();
8599
return dest;
86100
};
87-
88-
89-
function server() {
90-
const reader = new Reader();
91-
const writer = new Writer();
92-
93-
// the actual benchmark.
94-
const server = net.createServer(function(socket) {
95-
socket.pipe(writer);
96-
});
97-
98-
server.listen(PORT, function() {
99-
const socket = net.connect(PORT);
100-
socket.on('connect', function() {
101-
bench.start();
102-
103-
reader.pipe(socket);
104-
105-
setTimeout(function() {
106-
const bytes = writer.received;
107-
const gbits = (bytes * 8) / (1024 * 1024 * 1024);
108-
bench.end(gbits);
109-
process.exit(0);
110-
}, dur * 1000);
111-
});
112-
});
113-
}

benchmark/net/net-pipe.js

+28-41
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
'use strict';
33

44
const common = require('../common.js');
5+
const net = require('net');
56
const PORT = common.PORT;
67

78
const bench = common.createBenchmark(main, {
@@ -10,17 +11,10 @@ const bench = common.createBenchmark(main, {
1011
dur: [5],
1112
});
1213

13-
var dur;
14-
var len;
15-
var type;
1614
var chunk;
1715
var encoding;
1816

19-
function main(conf) {
20-
dur = +conf.dur;
21-
len = +conf.len;
22-
type = conf.type;
23-
17+
function main({ dur, len, type }) {
2418
switch (type) {
2519
case 'buf':
2620
chunk = Buffer.alloc(len, 'x');
@@ -37,10 +31,33 @@ function main(conf) {
3731
throw new Error(`invalid type: ${type}`);
3832
}
3933

40-
server();
41-
}
34+
const reader = new Reader();
35+
const writer = new Writer();
4236

43-
const net = require('net');
37+
// the actual benchmark.
38+
const server = net.createServer(function(socket) {
39+
socket.pipe(socket);
40+
});
41+
42+
server.listen(PORT, function() {
43+
const socket = net.connect(PORT);
44+
socket.on('connect', function() {
45+
bench.start();
46+
47+
reader.pipe(socket);
48+
socket.pipe(writer);
49+
50+
setTimeout(function() {
51+
// multiply by 2 since we're sending it first one way
52+
// then then back again.
53+
const bytes = writer.received * 2;
54+
const gbits = (bytes * 8) / (1024 * 1024 * 1024);
55+
bench.end(gbits);
56+
process.exit(0);
57+
}, dur * 1000);
58+
});
59+
});
60+
}
4461

4562
function Writer() {
4663
this.received = 0;
@@ -84,33 +101,3 @@ Reader.prototype.pipe = function(dest) {
84101
this.flow();
85102
return dest;
86103
};
87-
88-
89-
function server() {
90-
const reader = new Reader();
91-
const writer = new Writer();
92-
93-
// the actual benchmark.
94-
const server = net.createServer(function(socket) {
95-
socket.pipe(socket);
96-
});
97-
98-
server.listen(PORT, function() {
99-
const socket = net.connect(PORT);
100-
socket.on('connect', function() {
101-
bench.start();
102-
103-
reader.pipe(socket);
104-
socket.pipe(writer);
105-
106-
setTimeout(function() {
107-
// multiply by 2 since we're sending it first one way
108-
// then then back again.
109-
const bytes = writer.received * 2;
110-
const gbits = (bytes * 8) / (1024 * 1024 * 1024);
111-
bench.end(gbits);
112-
process.exit(0);
113-
}, dur * 1000);
114-
});
115-
});
116-
}

benchmark/net/net-s2c.js

+24-36
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,10 @@ const bench = common.createBenchmark(main, {
1010
dur: [5]
1111
});
1212

13-
var dur;
14-
var len;
15-
var type;
1613
var chunk;
1714
var encoding;
1815

19-
function main(conf) {
20-
dur = +conf.dur;
21-
len = +conf.len;
22-
type = conf.type;
23-
16+
function main({ dur, len, type }) {
2417
switch (type) {
2518
case 'buf':
2619
chunk = Buffer.alloc(len, 'x');
@@ -37,7 +30,29 @@ function main(conf) {
3730
throw new Error(`invalid type: ${type}`);
3831
}
3932

40-
server();
33+
const reader = new Reader();
34+
const writer = new Writer();
35+
36+
// the actual benchmark.
37+
const server = net.createServer(function(socket) {
38+
reader.pipe(socket);
39+
});
40+
41+
server.listen(PORT, function() {
42+
const socket = net.connect(PORT);
43+
socket.on('connect', function() {
44+
bench.start();
45+
46+
socket.pipe(writer);
47+
48+
setTimeout(function() {
49+
const bytes = writer.received;
50+
const gbits = (bytes * 8) / (1024 * 1024 * 1024);
51+
bench.end(gbits);
52+
process.exit(0);
53+
}, dur * 1000);
54+
});
55+
});
4156
}
4257

4358
const net = require('net');
@@ -84,30 +99,3 @@ Reader.prototype.pipe = function(dest) {
8499
this.flow();
85100
return dest;
86101
};
87-
88-
89-
function server() {
90-
const reader = new Reader();
91-
const writer = new Writer();
92-
93-
// the actual benchmark.
94-
const server = net.createServer(function(socket) {
95-
reader.pipe(socket);
96-
});
97-
98-
server.listen(PORT, function() {
99-
const socket = net.connect(PORT);
100-
socket.on('connect', function() {
101-
bench.start();
102-
103-
socket.pipe(writer);
104-
105-
setTimeout(function() {
106-
const bytes = writer.received;
107-
const gbits = (bytes * 8) / (1024 * 1024 * 1024);
108-
bench.end(gbits);
109-
process.exit(0);
110-
}, dur * 1000);
111-
});
112-
});
113-
}

0 commit comments

Comments
 (0)