1'use strict'; 2 3const myModule = process.argv[2]; 4if (!['http', 'https', 'http2'].includes(myModule)) { 5 throw new Error(`Invalid module for benchmark test double: ${myModule}`); 6} 7 8let options; 9if (myModule === 'https') { 10 options = { rejectUnauthorized: false }; 11} 12 13const http = require(myModule); 14 15const duration = +process.env.duration; 16const url = process.env.test_url; 17 18const start = process.hrtime(); 19let throughput = 0; 20 21function request(res, client) { 22 res.resume(); 23 res.on('error', () => {}); 24 res.on('end', () => { 25 throughput++; 26 const [sec, nanosec] = process.hrtime(start); 27 const ms = sec * 1000 + nanosec / 1e6; 28 if (ms < duration * 1000) { 29 run(); 30 } else { 31 console.log(JSON.stringify({ throughput })); 32 if (client) { 33 client.destroy(); 34 process.exit(0); 35 } 36 } 37 }); 38} 39 40function run() { 41 if (http.get) { // HTTP or HTTPS 42 if (options) { 43 http.get(url, options, request); 44 } else { 45 http.get(url, request); 46 } 47 } else { // HTTP/2 48 const client = http.connect(url); 49 client.on('error', () => {}); 50 request(client.request(), client); 51 } 52} 53 54run(); 55