2
2
'use strict' ;
3
3
4
4
const common = require ( '../common.js' ) ;
5
+ const net = require ( 'net' ) ;
5
6
const PORT = common . PORT ;
6
7
7
8
const bench = common . createBenchmark ( main , {
@@ -10,17 +11,10 @@ const bench = common.createBenchmark(main, {
10
11
dur : [ 5 ] ,
11
12
} ) ;
12
13
13
- var dur ;
14
- var len ;
15
- var type ;
16
14
var chunk ;
17
15
var encoding ;
18
16
19
- function main ( conf ) {
20
- dur = + conf . dur ;
21
- len = + conf . len ;
22
- type = conf . type ;
23
-
17
+ function main ( { dur, len, type } ) {
24
18
switch ( type ) {
25
19
case 'buf' :
26
20
chunk = Buffer . alloc ( len , 'x' ) ;
@@ -37,10 +31,33 @@ function main(conf) {
37
31
throw new Error ( `invalid type: ${ type } ` ) ;
38
32
}
39
33
40
- server ( ) ;
41
- }
34
+ const reader = new Reader ( ) ;
35
+ const writer = new Writer ( ) ;
42
36
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
+ }
44
61
45
62
function Writer ( ) {
46
63
this . received = 0 ;
@@ -84,33 +101,3 @@ Reader.prototype.pipe = function(dest) {
84
101
this . flow ( ) ;
85
102
return dest ;
86
103
} ;
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
- }
0 commit comments