wrk Load Test Tool Wrapper
The `wrk` Node.js package serves as a wrapper for the popular `wrk` HTTP benchmarking command-line tool. It enables developers to programmatically execute load tests and parse the output directly within a Node.js environment, simplifying the integration of performance testing into applications or CI/CD pipelines. This package allows configuration of `wrk` parameters such as threads, connections, and test duration, and then processes the `wrk` tool's raw output into a structured JavaScript object containing detailed performance metrics like requests per second, latency statistics, and transfer rates. Currently at version 1.2.1, the package was last published in September 2020, indicating it is no longer actively maintained. Its primary utility lies in abstracting the `child_process` execution and output parsing, but it critically depends on the `wrk` CLI tool being pre-installed on the system and accessible in the PATH.
Common errors
-
Error: spawn wrk ENOENT
cause The `wrk` command-line tool is not installed on the system or is not found in the system's PATH environment variable. The `node-wrk` package is just a wrapper and does not install the `wrk` binary itself.fixEnsure the `wrk` CLI tool is installed and accessible globally. For example, on macOS: `brew install wrk`. On Linux, you might need to compile it from source (refer to the official `wrk` GitHub page). -
ERR_REQUIRE_ESM: require() of ES Module ... is not supported.
cause This error occurs when trying to use `require()` to import a package that is exclusively an ESM module. Although this package is CommonJS, this error might occur if `node-wrk` is integrated into a project that itself is configured as ESM-only and doesn't handle CJS interop correctly.fixWhile `node-wrk` is CJS, if your project is pure ESM, ensure Node.js is configured to allow CJS modules. Or, if you are mistakenly trying to import another ESM-only package with `require`, change it to `import`. -
TypeError: wrk is not a function
cause This usually happens if the import statement is incorrect, such as `import { wrk } from 'wrk';` or `import * as wrk from 'wrk';` in an ESM context, where the package actually exports a default function.fixFor CommonJS, use `const wrk = require('wrk');`. For ESM (via dynamic import), use `const { default: wrk } = await import('wrk');` to correctly access the default exported function.
Warnings
- breaking The `node-wrk` package is a wrapper for the external `wrk` command-line tool. The `wrk` CLI tool *must* be installed on your system and accessible via the system's PATH environment variable for this package to function. This is not an npm dependency.
- gotcha The `node-wrk` package is no longer actively maintained. Its last publish was in September 2020. This means it may not receive updates for new Node.js versions, security vulnerabilities, or new features from the underlying `wrk` CLI tool.
- gotcha This package is CommonJS-only (`require`). It does not provide native ECMAScript Module (ESM) exports. Attempting a direct `import wrk from 'wrk'` will fail without specific build configurations or dynamic import (`await import('wrk')`).
- gotcha The package does not ship with TypeScript type definitions, which can lead to a less type-safe development experience when used in TypeScript projects.
Install
-
npm install wrk -
yarn add wrk -
pnpm add wrk
Imports
- wrk
const wrk = require('wrk'); - wrk
import wrk from 'wrk';
const { default: wrk } = await import('wrk');
Quickstart
const wrk = require('wrk');
let conns = 1;
const results = [];
function benchmark() {
if (conns === 100) {
// In a real scenario, you'd process these results,
// e.g., save to a file, aggregate, or display.
console.log('--- Benchmark Complete ---');
results.forEach((res, index) => {
console.log(`
Test with ${res.connections} connections:`);
console.log(` Requests/Sec: ${res.requestsPerSec}`);
console.log(` Latency Avg: ${res.latencyAvg}`);
});
return;
}
console.log(`Running test with ${conns} connections...`);
wrk({
threads: 1,
connections: conns,
duration: '1s',
printLatency: true,
headers: { 'User-Agent': 'node-wrk-benchmark' },
url: 'http://localhost:3000/' // Ensure a server is running here, e.g., 'python3 -m http.server 3000'
}, function(err, out) {
if (err) {
console.error('wrk error:', err);
// Often 'spawn wrk ENOENT' if the wrk CLI tool is not installed or in PATH
return;
}
results.push(out);
conns++;
benchmark();
});
}
// To run this example, ensure the `wrk` CLI tool is installed (e.g., `brew install wrk` on macOS)
// and a simple HTTP server is running on http://localhost:3000.
// You can start a simple Python server with: `python3 -m http.server 3000`
benchmark();