|
1 | 1 | 'use strict';
|
2 | 2 |
|
3 | 3 | const child_process = require('child_process');
|
| 4 | +const path = require('path'); |
| 5 | +const fs = require('fs'); |
4 | 6 |
|
5 | 7 | // The port used by servers and wrk
|
6 | 8 | exports.PORT = process.env.PORT || 12346;
|
7 | 9 |
|
8 |
| -function AutocannonBenchmarker() { |
9 |
| - this.name = 'autocannon'; |
10 |
| - this.autocannon_exe = process.platform === 'win32' ? |
11 |
| - 'autocannon.cmd' : |
12 |
| - 'autocannon'; |
13 |
| - const result = child_process.spawnSync(this.autocannon_exe, ['-h']); |
14 |
| - this.present = !(result.error && result.error.code === 'ENOENT'); |
15 |
| -} |
| 10 | +class AutocannonBenchmarker { |
| 11 | + constructor() { |
| 12 | + this.name = 'autocannon'; |
| 13 | + this.executable = process.platform === 'win32' ? |
| 14 | + 'autocannon.cmd' : |
| 15 | + 'autocannon'; |
| 16 | + const result = child_process.spawnSync(this.executable, ['-h']); |
| 17 | + this.present = !(result.error && result.error.code === 'ENOENT'); |
| 18 | + } |
16 | 19 |
|
17 |
| -AutocannonBenchmarker.prototype.create = function(options) { |
18 |
| - const args = [ |
19 |
| - '-d', options.duration, |
20 |
| - '-c', options.connections, |
21 |
| - '-j', |
22 |
| - '-n', |
23 |
| - `http://127.0.0.1:${options.port}${options.path}` |
24 |
| - ]; |
25 |
| - const child = child_process.spawn(this.autocannon_exe, args); |
26 |
| - return child; |
27 |
| -}; |
| 20 | + create(options) { |
| 21 | + const args = [ |
| 22 | + '-d', options.duration, |
| 23 | + '-c', options.connections, |
| 24 | + '-j', |
| 25 | + '-n', |
| 26 | + `http://127.0.0.1:${options.port}${options.path}` |
| 27 | + ]; |
| 28 | + const child = child_process.spawn(this.executable, args); |
| 29 | + return child; |
| 30 | + } |
28 | 31 |
|
29 |
| -AutocannonBenchmarker.prototype.processResults = function(output) { |
30 |
| - let result; |
31 |
| - try { |
32 |
| - result = JSON.parse(output); |
33 |
| - } catch (err) { |
34 |
| - // Do nothing, let next line handle this |
| 32 | + processResults(output) { |
| 33 | + let result; |
| 34 | + try { |
| 35 | + result = JSON.parse(output); |
| 36 | + } catch (err) { |
| 37 | + return undefined; |
| 38 | + } |
| 39 | + if (!result || !result.requests || !result.requests.average) { |
| 40 | + return undefined; |
| 41 | + } else { |
| 42 | + return result.requests.average; |
| 43 | + } |
35 | 44 | }
|
36 |
| - if (!result || !result.requests || !result.requests.average) { |
37 |
| - return undefined; |
38 |
| - } else { |
39 |
| - return result.requests.average; |
| 45 | +} |
| 46 | + |
| 47 | +class WrkBenchmarker { |
| 48 | + constructor() { |
| 49 | + this.name = 'wrk'; |
| 50 | + this.executable = 'wrk'; |
| 51 | + const result = child_process.spawnSync(this.executable, ['-h']); |
| 52 | + this.present = !(result.error && result.error.code === 'ENOENT'); |
| 53 | + } |
| 54 | + |
| 55 | + create(options) { |
| 56 | + const args = [ |
| 57 | + '-d', options.duration, |
| 58 | + '-c', options.connections, |
| 59 | + '-t', 8, |
| 60 | + `http://127.0.0.1:${options.port}${options.path}` |
| 61 | + ]; |
| 62 | + const child = child_process.spawn(this.executable, args); |
| 63 | + return child; |
40 | 64 | }
|
41 |
| -}; |
42 | 65 |
|
43 |
| -function WrkBenchmarker() { |
44 |
| - this.name = 'wrk'; |
45 |
| - this.regexp = /Requests\/sec:[ \t]+([0-9.]+)/; |
46 |
| - const result = child_process.spawnSync('wrk', ['-h']); |
47 |
| - this.present = !(result.error && result.error.code === 'ENOENT'); |
| 66 | + processResults(output) { |
| 67 | + const throughputRe = /Requests\/sec:[ \t]+([0-9.]+)/; |
| 68 | + const match = output.match(throughputRe); |
| 69 | + const throughput = match && +match[1]; |
| 70 | + if (!isFinite(throughput)) { |
| 71 | + return undefined; |
| 72 | + } else { |
| 73 | + return throughput; |
| 74 | + } |
| 75 | + } |
48 | 76 | }
|
49 | 77 |
|
50 |
| -WrkBenchmarker.prototype.create = function(options) { |
51 |
| - const args = [ |
52 |
| - '-d', options.duration, |
53 |
| - '-c', options.connections, |
54 |
| - '-t', 8, |
55 |
| - `http://127.0.0.1:${options.port}${options.path}` |
56 |
| - ]; |
57 |
| - const child = child_process.spawn('wrk', args); |
58 |
| - return child; |
59 |
| -}; |
| 78 | +/** |
| 79 | + * Simple, single-threaded benchmarker for testing if the benchmark |
| 80 | + * works |
| 81 | + */ |
| 82 | +class TestDoubleBenchmarker { |
| 83 | + constructor() { |
| 84 | + this.name = 'test-double'; |
| 85 | + this.executable = path.resolve(__dirname, '_test-double-benchmarker.js'); |
| 86 | + this.present = fs.existsSync(this.executable); |
| 87 | + } |
| 88 | + |
| 89 | + create(options) { |
| 90 | + const child = child_process.fork(this.executable, { |
| 91 | + silent: true, |
| 92 | + env: { |
| 93 | + duration: options.duration, |
| 94 | + connections: options.connections, |
| 95 | + path: `http://127.0.0.1:${options.port}${options.path}` |
| 96 | + } |
| 97 | + }); |
| 98 | + return child; |
| 99 | + } |
60 | 100 |
|
61 |
| -WrkBenchmarker.prototype.processResults = function(output) { |
62 |
| - const match = output.match(this.regexp); |
63 |
| - const result = match && +match[1]; |
64 |
| - if (!isFinite(result)) { |
65 |
| - return undefined; |
66 |
| - } else { |
67 |
| - return result; |
| 101 | + processResults(output) { |
| 102 | + let result; |
| 103 | + try { |
| 104 | + result = JSON.parse(output); |
| 105 | + } catch (err) { |
| 106 | + return undefined; |
| 107 | + } |
| 108 | + return result.throughput; |
68 | 109 | }
|
69 |
| -}; |
| 110 | +} |
70 | 111 |
|
71 |
| -const http_benchmarkers = [new WrkBenchmarker(), new AutocannonBenchmarker()]; |
| 112 | +const http_benchmarkers = [ |
| 113 | + new WrkBenchmarker(), |
| 114 | + new AutocannonBenchmarker(), |
| 115 | + new TestDoubleBenchmarker() |
| 116 | +]; |
72 | 117 |
|
73 | 118 | const benchmarkers = {};
|
74 | 119 |
|
|
0 commit comments