summaryrefslogtreecommitdiff
path: root/test/pummel/test-keep-alive.js
blob: 896f0a1f68d28f4c6886071ba54c003362c8d126 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.

'use strict';

// This test requires the program 'wrk'.
const common = require('../common');

const child_process = require('child_process');
const result = child_process.spawnSync('wrk', ['-h']);
if (result.error && result.error.code === 'ENOENT')
  common.skip('test requires `wrk` to be installed first');

const assert = require('assert');
const http = require('http');
const url = require('url');

const body = 'hello world\n';
const server = http.createServer((req, res) => {
  res.writeHead(200, {
    'Content-Length': body.length,
    'Content-Type': 'text/plain'
  });
  res.write(body);
  res.end();
});

let keepAliveReqSec = 0;
let normalReqSec = 0;


const runAb = (opts, callback) => {
  const args = [
    '-c', opts.concurrent || 50,
    '-t', opts.threads || 2,
    '-d', opts.duration || '5s',
  ];

  if (!opts.keepalive) {
    args.push('-H');
    args.push('Connection: close');
  }

  args.push(url.format({ hostname: '127.0.0.1',
                         port: opts.port, protocol: 'http' }));

  const child = child_process.spawn('wrk', args);
  child.stderr.pipe(process.stderr);
  child.stdout.setEncoding('utf8');

  let stdout;

  child.stdout.on('data', (data) => stdout += data);

  child.on('close', (code, signal) => {
    if (code) {
      console.error(code, signal);
      process.exit(code);
      return;
    }

    let matches = /Requests\/sec:\s*(\d+)\./i.exec(stdout);
    const reqSec = parseInt(matches[1]);

    matches = /Keep-Alive requests:\s*(\d+)/i.exec(stdout);
    let keepAliveRequests;
    if (matches) {
      keepAliveRequests = parseInt(matches[1]);
    } else {
      keepAliveRequests = 0;
    }

    callback(reqSec, keepAliveRequests);
  });
};

server.listen(0, () => {
  const port = server.address().port;
  runAb({ keepalive: true, port: port }, (reqSec) => {
    keepAliveReqSec = reqSec;

    runAb({ keepalive: false, port: port }, (reqSec) => {
      normalReqSec = reqSec;
      server.close();
    });
  });
});

process.on('exit', () => {
  assert.strictEqual(
    normalReqSec > 50,
    true,
    `normalReqSec should be greater than 50, but got ${normalReqSec}`
  );
  assert.strictEqual(
    keepAliveReqSec > 50,
    true,
    `keepAliveReqSec should be greater than 50, but got ${keepAliveReqSec}`
  );
  assert.strictEqual(
    normalReqSec < keepAliveReqSec,
    true,
    'normalReqSec should be less than keepAliveReqSec, ' +
    `but ${normalReqSec} is greater than ${keepAliveReqSec}`
  );
});