1'use strict'; 2 3const common = require('../common'); 4 5const bench = common.createBenchmark(main, { 6 len: [4, 8, 16, 32], 7 n: [1e5] 8}, { 9 flags: ['--expose-internals', '--no-warnings'] 10}); 11 12function main({ len, n }) { 13 const { HTTPParser } = common.binding('http_parser'); 14 const REQUEST = HTTPParser.REQUEST; 15 const kOnHeaders = HTTPParser.kOnHeaders | 0; 16 const kOnHeadersComplete = HTTPParser.kOnHeadersComplete | 0; 17 const kOnBody = HTTPParser.kOnBody | 0; 18 const kOnMessageComplete = HTTPParser.kOnMessageComplete | 0; 19 const CRLF = '\r\n'; 20 21 function processHeader(header, n) { 22 const parser = newParser(REQUEST); 23 24 bench.start(); 25 for (let i = 0; i < n; i++) { 26 parser.execute(header, 0, header.length); 27 parser.initialize(REQUEST, {}); 28 } 29 bench.end(n); 30 } 31 32 function newParser(type) { 33 const parser = new HTTPParser(); 34 parser.initialize(type, {}); 35 36 parser.headers = []; 37 38 parser[kOnHeaders] = function() { }; 39 parser[kOnHeadersComplete] = function() { }; 40 parser[kOnBody] = function() { }; 41 parser[kOnMessageComplete] = function() { }; 42 43 return parser; 44 } 45 46 let header = `GET /hello HTTP/1.1${CRLF}Content-Type: text/plain${CRLF}`; 47 48 for (let i = 0; i < len; i++) { 49 header += `X-Filler${i}: ${Math.random().toString(36).substr(2)}${CRLF}`; 50 } 51 header += CRLF; 52 53 processHeader(Buffer.from(header), n); 54} 55