11cb0ef41Sopenharmony_ci// Copyright Joyent, Inc. and other Node contributors.
21cb0ef41Sopenharmony_ci//
31cb0ef41Sopenharmony_ci// Permission is hereby granted, free of charge, to any person obtaining a
41cb0ef41Sopenharmony_ci// copy of this software and associated documentation files (the
51cb0ef41Sopenharmony_ci// "Software"), to deal in the Software without restriction, including
61cb0ef41Sopenharmony_ci// without limitation the rights to use, copy, modify, merge, publish,
71cb0ef41Sopenharmony_ci// distribute, sublicense, and/or sell copies of the Software, and to permit
81cb0ef41Sopenharmony_ci// persons to whom the Software is furnished to do so, subject to the
91cb0ef41Sopenharmony_ci// following conditions:
101cb0ef41Sopenharmony_ci//
111cb0ef41Sopenharmony_ci// The above copyright notice and this permission notice shall be included
121cb0ef41Sopenharmony_ci// in all copies or substantial portions of the Software.
131cb0ef41Sopenharmony_ci//
141cb0ef41Sopenharmony_ci// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
151cb0ef41Sopenharmony_ci// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
161cb0ef41Sopenharmony_ci// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
171cb0ef41Sopenharmony_ci// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
181cb0ef41Sopenharmony_ci// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
191cb0ef41Sopenharmony_ci// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
201cb0ef41Sopenharmony_ci// USE OR OTHER DEALINGS IN THE SOFTWARE.
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ci'use strict';
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_ci// This test requires the program 'wrk'.
251cb0ef41Sopenharmony_ciconst common = require('../common');
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ciconst child_process = require('child_process');
281cb0ef41Sopenharmony_ciconst result = child_process.spawnSync('wrk', ['-h']);
291cb0ef41Sopenharmony_ciif (result.error && result.error.code === 'ENOENT')
301cb0ef41Sopenharmony_ci  common.skip('test requires `wrk` to be installed first');
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ciconst assert = require('assert');
331cb0ef41Sopenharmony_ciconst http = require('http');
341cb0ef41Sopenharmony_ciconst url = require('url');
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_ciconst body = 'hello world\n';
371cb0ef41Sopenharmony_ciconst server = http.createServer((req, res) => {
381cb0ef41Sopenharmony_ci  res.writeHead(200, {
391cb0ef41Sopenharmony_ci    'Content-Length': body.length,
401cb0ef41Sopenharmony_ci    'Content-Type': 'text/plain',
411cb0ef41Sopenharmony_ci  });
421cb0ef41Sopenharmony_ci  res.write(body);
431cb0ef41Sopenharmony_ci  res.end();
441cb0ef41Sopenharmony_ci});
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_cilet keepAliveReqSec = 0;
471cb0ef41Sopenharmony_cilet normalReqSec = 0;
481cb0ef41Sopenharmony_ci
491cb0ef41Sopenharmony_ci
501cb0ef41Sopenharmony_ciconst runAb = (opts, callback) => {
511cb0ef41Sopenharmony_ci  const args = [
521cb0ef41Sopenharmony_ci    '-c', opts.concurrent || 50,
531cb0ef41Sopenharmony_ci    '-t', opts.threads || 2,
541cb0ef41Sopenharmony_ci    '-d', opts.duration || '5s',
551cb0ef41Sopenharmony_ci  ];
561cb0ef41Sopenharmony_ci
571cb0ef41Sopenharmony_ci  if (!opts.keepalive) {
581cb0ef41Sopenharmony_ci    args.push('-H');
591cb0ef41Sopenharmony_ci    args.push('Connection: close');
601cb0ef41Sopenharmony_ci  }
611cb0ef41Sopenharmony_ci
621cb0ef41Sopenharmony_ci  args.push(url.format({ hostname: '127.0.0.1',
631cb0ef41Sopenharmony_ci                         port: opts.port, protocol: 'http' }));
641cb0ef41Sopenharmony_ci
651cb0ef41Sopenharmony_ci  const child = child_process.spawn('wrk', args);
661cb0ef41Sopenharmony_ci  child.stderr.pipe(process.stderr);
671cb0ef41Sopenharmony_ci  child.stdout.setEncoding('utf8');
681cb0ef41Sopenharmony_ci
691cb0ef41Sopenharmony_ci  let stdout;
701cb0ef41Sopenharmony_ci
711cb0ef41Sopenharmony_ci  child.stdout.on('data', (data) => stdout += data);
721cb0ef41Sopenharmony_ci
731cb0ef41Sopenharmony_ci  child.on('close', (code, signal) => {
741cb0ef41Sopenharmony_ci    if (code) {
751cb0ef41Sopenharmony_ci      console.error(code, signal);
761cb0ef41Sopenharmony_ci      process.exit(code);
771cb0ef41Sopenharmony_ci      return;
781cb0ef41Sopenharmony_ci    }
791cb0ef41Sopenharmony_ci
801cb0ef41Sopenharmony_ci    let matches = /Requests\/sec:\s*(\d+)\./i.exec(stdout);
811cb0ef41Sopenharmony_ci    const reqSec = parseInt(matches[1]);
821cb0ef41Sopenharmony_ci
831cb0ef41Sopenharmony_ci    matches = /Keep-Alive requests:\s*(\d+)/i.exec(stdout);
841cb0ef41Sopenharmony_ci    let keepAliveRequests;
851cb0ef41Sopenharmony_ci    if (matches) {
861cb0ef41Sopenharmony_ci      keepAliveRequests = parseInt(matches[1]);
871cb0ef41Sopenharmony_ci    } else {
881cb0ef41Sopenharmony_ci      keepAliveRequests = 0;
891cb0ef41Sopenharmony_ci    }
901cb0ef41Sopenharmony_ci
911cb0ef41Sopenharmony_ci    callback(reqSec, keepAliveRequests);
921cb0ef41Sopenharmony_ci  });
931cb0ef41Sopenharmony_ci};
941cb0ef41Sopenharmony_ci
951cb0ef41Sopenharmony_ciserver.listen(0, () => {
961cb0ef41Sopenharmony_ci  const port = server.address().port;
971cb0ef41Sopenharmony_ci  runAb({ keepalive: true, port: port }, (reqSec) => {
981cb0ef41Sopenharmony_ci    keepAliveReqSec = reqSec;
991cb0ef41Sopenharmony_ci
1001cb0ef41Sopenharmony_ci    runAb({ keepalive: false, port: port }, (reqSec) => {
1011cb0ef41Sopenharmony_ci      normalReqSec = reqSec;
1021cb0ef41Sopenharmony_ci      server.close();
1031cb0ef41Sopenharmony_ci    });
1041cb0ef41Sopenharmony_ci  });
1051cb0ef41Sopenharmony_ci});
1061cb0ef41Sopenharmony_ci
1071cb0ef41Sopenharmony_ciprocess.on('exit', () => {
1081cb0ef41Sopenharmony_ci  assert.strictEqual(
1091cb0ef41Sopenharmony_ci    normalReqSec > 50,
1101cb0ef41Sopenharmony_ci    true,
1111cb0ef41Sopenharmony_ci    `normalReqSec should be greater than 50, but got ${normalReqSec}`,
1121cb0ef41Sopenharmony_ci  );
1131cb0ef41Sopenharmony_ci  assert.strictEqual(
1141cb0ef41Sopenharmony_ci    keepAliveReqSec > 50,
1151cb0ef41Sopenharmony_ci    true,
1161cb0ef41Sopenharmony_ci    `keepAliveReqSec should be greater than 50, but got ${keepAliveReqSec}`,
1171cb0ef41Sopenharmony_ci  );
1181cb0ef41Sopenharmony_ci  assert.strictEqual(
1191cb0ef41Sopenharmony_ci    normalReqSec < keepAliveReqSec,
1201cb0ef41Sopenharmony_ci    true,
1211cb0ef41Sopenharmony_ci    'normalReqSec should be less than keepAliveReqSec, ' +
1221cb0ef41Sopenharmony_ci    `but ${normalReqSec} is greater than ${keepAliveReqSec}`,
1231cb0ef41Sopenharmony_ci  );
1241cb0ef41Sopenharmony_ci});
125