Skip to content

Commit 1595efa

Browse files
BridgeARMayaLekova
authored andcommittedMay 8, 2018
benchmark: (net) use destructuring
PR-URL: nodejs#18250 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 342acbf commit 1595efa

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-
}

‎benchmark/net/net-wrap-js-stream-passthrough.js

+18-29
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,12 @@ const bench = common.createBenchmark(main, {
1212
flags: ['--expose-internals']
1313
});
1414

15-
var dur;
16-
var len;
17-
var type;
1815
var chunk;
1916
var encoding;
20-
var JSStreamWrap; // Can only require internals inside main().
2117

22-
function main(conf) {
23-
dur = +conf.dur;
24-
len = +conf.len;
25-
type = conf.type;
26-
JSStreamWrap = require('internal/wrap_js_stream');
18+
function main({ dur, len, type }) {
19+
// Can only require internals inside main().
20+
const JSStreamWrap = require('internal/wrap_js_stream');
2721

2822
switch (type) {
2923
case 'buf':
@@ -41,7 +35,21 @@ function main(conf) {
4135
throw new Error(`invalid type: ${type}`);
4236
}
4337

44-
doBenchmark();
38+
const reader = new Reader();
39+
const writer = new Writer();
40+
41+
// the actual benchmark.
42+
const fakeSocket = new JSStreamWrap(new PassThrough());
43+
bench.start();
44+
reader.pipe(fakeSocket);
45+
fakeSocket.pipe(writer);
46+
47+
setTimeout(function() {
48+
const bytes = writer.received;
49+
const gbits = (bytes * 8) / (1024 * 1024 * 1024);
50+
bench.end(gbits);
51+
process.exit(0);
52+
}, dur * 1000);
4553
}
4654

4755
function Writer() {
@@ -86,22 +94,3 @@ Reader.prototype.pipe = function(dest) {
8694
this.flow();
8795
return dest;
8896
};
89-
90-
91-
function doBenchmark() {
92-
const reader = new Reader();
93-
const writer = new Writer();
94-
95-
// the actual benchmark.
96-
const fakeSocket = new JSStreamWrap(new PassThrough());
97-
bench.start();
98-
reader.pipe(fakeSocket);
99-
fakeSocket.pipe(writer);
100-
101-
setTimeout(function() {
102-
const bytes = writer.received;
103-
const gbits = (bytes * 8) / (1024 * 1024 * 1024);
104-
bench.end(gbits);
105-
process.exit(0);
106-
}, dur * 1000);
107-
}

‎benchmark/net/tcp-raw-c2s.js

+8-19
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,7 @@ const TCPConnectWrap = process.binding('tcp_wrap').TCPConnectWrap;
1919
const WriteWrap = process.binding('stream_wrap').WriteWrap;
2020
const PORT = common.PORT;
2121

22-
var dur;
23-
var len;
24-
var type;
25-
26-
function main(conf) {
27-
dur = +conf.dur;
28-
len = +conf.len;
29-
type = conf.type;
30-
server();
31-
}
32-
33-
34-
function fail(err, syscall) {
35-
throw util._errnoException(err, syscall);
36-
}
37-
38-
function server() {
22+
function main({ dur, len, type }) {
3923
const serverHandle = new TCP(TCPConstants.SERVER);
4024
var err = serverHandle.bind('127.0.0.1', PORT);
4125
if (err)
@@ -73,10 +57,15 @@ function server() {
7357
clientHandle.readStart();
7458
};
7559

76-
client();
60+
client(type, len);
61+
}
62+
63+
64+
function fail(err, syscall) {
65+
throw util._errnoException(err, syscall);
7766
}
7867

79-
function client() {
68+
function client(type, len) {
8069
var chunk;
8170
switch (type) {
8271
case 'buf':

‎benchmark/net/tcp-raw-pipe.js

+8-21
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,17 @@ const bench = common.createBenchmark(main, {
1414
dur: [5]
1515
});
1616

17+
function fail(err, syscall) {
18+
throw util._errnoException(err, syscall);
19+
}
20+
1721
const { TCP, constants: TCPConstants } = process.binding('tcp_wrap');
1822
const TCPConnectWrap = process.binding('tcp_wrap').TCPConnectWrap;
1923
const WriteWrap = process.binding('stream_wrap').WriteWrap;
2024
const PORT = common.PORT;
2125

22-
var dur;
23-
var len;
24-
var type;
25-
26-
function main(conf) {
27-
dur = +conf.dur;
28-
len = +conf.len;
29-
type = conf.type;
30-
server();
31-
}
32-
33-
function fail(err, syscall) {
34-
throw util._errnoException(err, syscall);
35-
}
36-
37-
function server() {
26+
function main({ dur, len, type }) {
27+
// Server
3828
const serverHandle = new TCP(TCPConstants.SERVER);
3929
var err = serverHandle.bind('127.0.0.1', PORT);
4030
if (err)
@@ -70,10 +60,7 @@ function server() {
7060
clientHandle.readStart();
7161
};
7262

73-
client();
74-
}
75-
76-
function client() {
63+
// Client
7764
var chunk;
7865
switch (type) {
7966
case 'buf':
@@ -91,9 +78,9 @@ function client() {
9178

9279
const clientHandle = new TCP(TCPConstants.SOCKET);
9380
const connectReq = new TCPConnectWrap();
94-
const err = clientHandle.connect(connectReq, '127.0.0.1', PORT);
9581
var bytes = 0;
9682

83+
err = clientHandle.connect(connectReq, '127.0.0.1', PORT);
9784
if (err)
9885
fail(err, 'connect');
9986

‎benchmark/net/tcp-raw-s2c.js

+7-18
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,7 @@ const TCPConnectWrap = process.binding('tcp_wrap').TCPConnectWrap;
1919
const WriteWrap = process.binding('stream_wrap').WriteWrap;
2020
const PORT = common.PORT;
2121

22-
var dur;
23-
var len;
24-
var type;
25-
26-
function main(conf) {
27-
dur = +conf.dur;
28-
len = +conf.len;
29-
type = conf.type;
30-
server();
31-
}
32-
33-
function fail(err, syscall) {
34-
throw util._errnoException(err, syscall);
35-
}
36-
37-
function server() {
22+
function main({ dur, len, type }) {
3823
const serverHandle = new TCP(TCPConstants.SERVER);
3924
var err = serverHandle.bind('127.0.0.1', PORT);
4025
if (err)
@@ -103,10 +88,14 @@ function server() {
10388
}
10489
};
10590

106-
client();
91+
client(dur);
92+
}
93+
94+
function fail(err, syscall) {
95+
throw util._errnoException(err, syscall);
10796
}
10897

109-
function client() {
98+
function client(dur) {
11099
const clientHandle = new TCP(TCPConstants.SOCKET);
111100
const connectReq = new TCPConnectWrap();
112101
const err = clientHandle.connect(connectReq, '127.0.0.1', PORT);

0 commit comments

Comments
 (0)
Please sign in to comment.