| 18 | }); |
| 19 | |
| 20 | function main({ dur, len, type }) { |
| 21 | const { |
| 22 | TCP, |
| 23 | TCPConnectWrap, |
| 24 | constants: TCPConstants, |
| 25 | } = common.binding('tcp_wrap'); |
| 26 | const { WriteWrap } = common.binding('stream_wrap'); |
| 27 | const PORT = common.PORT; |
| 28 | |
| 29 | const serverHandle = new TCP(TCPConstants.SERVER); |
| 30 | let err = serverHandle.bind('127.0.0.1', PORT); |
| 31 | if (err) |
| 32 | fail(err, 'bind'); |
| 33 | |
| 34 | err = serverHandle.listen(511); |
| 35 | if (err) |
| 36 | fail(err, 'listen'); |
| 37 | |
| 38 | serverHandle.onconnection = function(err, clientHandle) { |
| 39 | if (err) |
| 40 | fail(err, 'connect'); |
| 41 | |
| 42 | // The meat of the benchmark is right here: |
| 43 | bench.start(); |
| 44 | let bytes = 0; |
| 45 | |
| 46 | setTimeout(() => { |
| 47 | // report in Gb/sec |
| 48 | bench.end((bytes * 8) / (1024 * 1024 * 1024)); |
| 49 | process.exit(0); |
| 50 | }, dur * 1000); |
| 51 | |
| 52 | clientHandle.onread = function(buffer) { |
| 53 | // We're not expecting to ever get an EOF from the client. |
| 54 | // Just lots of data forever. |
| 55 | if (!buffer) |
| 56 | fail('read'); |
| 57 | |
| 58 | // Don't slice the buffer. The point of this is to isolate, not |
| 59 | // simulate real traffic. |
| 60 | bytes += buffer.byteLength; |
| 61 | }; |
| 62 | |
| 63 | clientHandle.readStart(); |
| 64 | }; |
| 65 | |
| 66 | client(type, len); |
| 67 | |
| 68 | function fail(err, syscall) { |
| 69 | throw util._errnoException(err, syscall); |
| 70 | } |
| 71 | |
| 72 | function client(type, len) { |
| 73 | let chunk; |
| 74 | switch (type) { |
| 75 | case 'buf': |
| 76 | chunk = Buffer.alloc(len, 'x'); |
| 77 | break; |