|
| 1 | +import { availableParallelism } from 'node:os'; |
| 2 | +import { spawn } from 'node:child_process'; |
| 3 | +import { quote as shellQuote } from 'shell-quote'; |
| 4 | + |
| 5 | +export class AutocannonCLI { |
| 6 | + // NOTE: I don't use windows or have a windows machine to test on, so this |
| 7 | + // was just carried over from the previous implementation, someone |
| 8 | + // should go and test I didn't break this |
| 9 | + executable = process.platform === 'win32' ? 'autocannon.cmd' : 'autocannon'; |
| 10 | + isPresent = null; |
| 11 | + status = null; |
| 12 | + process = null; |
| 13 | + |
| 14 | + duration = 60; |
| 15 | + connections = 100; |
| 16 | + method = 'GET'; |
| 17 | + url = new URL('http://127.0.0.1:3000/'); |
| 18 | + headers = {}; |
| 19 | + body; |
| 20 | + |
| 21 | + constructor (opts = {}) { |
| 22 | + // autocannon options |
| 23 | + this.duration = opts.duration || this.duration; |
| 24 | + this.connections = opts.connections || this.connections; |
| 25 | + |
| 26 | + // request options |
| 27 | + this.method = opts.method || this.method; |
| 28 | + this.url = opts.url || this.url; |
| 29 | + this.headers = opts.headers || this.headers; |
| 30 | + this.body = opts.body || this.body; |
| 31 | + } |
| 32 | + |
| 33 | + async start (opts = {}) { |
| 34 | + // Check if present, but only once |
| 35 | + if (this.isPresent === null) { |
| 36 | + try { |
| 37 | + await this.spawn(['-h']); |
| 38 | + this.isPresent = true; |
| 39 | + } catch (e) { |
| 40 | + // Set the status to the error we got |
| 41 | + this.status = e; |
| 42 | + this.isPresent = false; |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + if (!this.isPresent) { |
| 47 | + this.status = this.status || 'not present'; |
| 48 | + throw new Error('executable not present', { |
| 49 | + cause: this.status |
| 50 | + }); |
| 51 | + } |
| 52 | + |
| 53 | + this.status = 'starting'; |
| 54 | + |
| 55 | + const args = [ |
| 56 | + // -d/--duration SEC |
| 57 | + '-d', this.duration, |
| 58 | + // -c/--connections NUM |
| 59 | + '-c', this.connections, |
| 60 | + // -w/--workers NUM |
| 61 | + '-w', Math.min(this.connections, availableParallelism() || 8), |
| 62 | + |
| 63 | + // -j/--json |
| 64 | + '-j', |
| 65 | + // -n/--no-progress |
| 66 | + '-n', |
| 67 | + |
| 68 | + // -m/--method METHOD |
| 69 | + '-m', opts.method || this.method |
| 70 | + ]; |
| 71 | + |
| 72 | + // -b/--body BODY |
| 73 | + const body = opts.body || this.body; |
| 74 | + if (body) { |
| 75 | + args.push('-b', typeof body === 'string' ? body : JSON.stringify(body)); |
| 76 | + } |
| 77 | + |
| 78 | + // -H/--headers K=V |
| 79 | + for (const [header, value] of Object.entries(opts.headers || this.headers)) { |
| 80 | + args.push('-H', `${header}=${value}`); |
| 81 | + } |
| 82 | + |
| 83 | + // url (positional) |
| 84 | + const url = opts.url ? new Url(opts.url, this.url) : this.url; |
| 85 | + args.push(url.toString()); |
| 86 | + |
| 87 | + // Run the process |
| 88 | + const stdout = await this.spawn(args); |
| 89 | + |
| 90 | + const result = JSON.parse(stdout); |
| 91 | + if (!result.requests.average) { |
| 92 | + throw new Error(`${this.executable} produced invalid JSON output`); |
| 93 | + } |
| 94 | + |
| 95 | + return result; |
| 96 | + } |
| 97 | + |
| 98 | + async spawn (args = [], opts = {}) { |
| 99 | + return new Promise((resolve, reject) => { |
| 100 | + const proc = spawn(`${this.executable} ${shellQuote(args)}`, { |
| 101 | + shell: true |
| 102 | + }, opts); |
| 103 | + |
| 104 | + let stderr = ''; |
| 105 | + proc.stderr.setEncoding('utf8'); |
| 106 | + proc.stderr.on('data', (chunk) => { |
| 107 | + stderr += chunk |
| 108 | + }); |
| 109 | + |
| 110 | + let stdout = ''; |
| 111 | + proc.stdout.setEncoding('utf8'); |
| 112 | + proc.stdout.on('data', (chunk) => { |
| 113 | + stdout += chunk |
| 114 | + }); |
| 115 | + |
| 116 | + const onError = (e, data) => { |
| 117 | + Object.assign(e, { stderr, stdout, ...data }); |
| 118 | + this.status = e; |
| 119 | + reject(e); |
| 120 | + } |
| 121 | + |
| 122 | + proc.once('error', (e) => onError(e)); |
| 123 | + |
| 124 | + proc.once('close', (code) => { |
| 125 | + if (code) { |
| 126 | + return onError(new Error(`${this.executable} failed with ${code}`, { code })); |
| 127 | + } |
| 128 | + resolve(stdout); |
| 129 | + }); |
| 130 | + |
| 131 | + }); |
| 132 | + } |
| 133 | +} |
0 commit comments