1// Measure the time it takes for the HTTP client to send a request body. 2'use strict'; 3 4const common = require('../common.js'); 5const http = require('http'); 6 7const bench = common.createBenchmark(main, { 8 dur: [5], 9 type: ['asc', 'utf', 'buf'], 10 len: [32, 256, 1024], 11 method: ['write', 'end'] 12}); 13 14function main({ dur, len, type, method }) { 15 let encoding; 16 let chunk; 17 switch (type) { 18 case 'buf': 19 chunk = Buffer.alloc(len, 'x'); 20 break; 21 case 'utf': 22 encoding = 'utf8'; 23 chunk = 'ü'.repeat(len / 2); 24 break; 25 case 'asc': 26 chunk = 'a'.repeat(len); 27 break; 28 } 29 30 let nreqs = 0; 31 const options = { 32 headers: { 'Connection': 'keep-alive', 'Transfer-Encoding': 'chunked' }, 33 agent: new http.Agent({ maxSockets: 1 }), 34 host: '127.0.0.1', 35 port: common.PORT, 36 path: '/', 37 method: 'POST' 38 }; 39 40 const server = http.createServer((req, res) => { 41 res.end(); 42 }); 43 server.listen(options.port, options.host, () => { 44 setTimeout(done, dur * 1000); 45 bench.start(); 46 pummel(); 47 }); 48 49 function pummel() { 50 const req = http.request(options, (res) => { 51 nreqs++; 52 pummel(); // Line up next request. 53 res.resume(); 54 }); 55 if (method === 'write') { 56 req.write(chunk, encoding); 57 req.end(); 58 } else { 59 req.end(chunk, encoding); 60 } 61 } 62 63 function done() { 64 bench.end(nreqs); 65 process.exit(0); 66 } 67} 68