loadtest

repository·main·Indexed 23 days ago

https://github.com/alexfernandez/loadtest

A tool for running load tests against HTTP and WebSockets URLs, available as a standalone CLI or a programmatic API for automated testing. It features an ab-compatible interface, support for constant requests per second (RPS), multi-process mode via the Node.js cluster module, and a bundled test server for performance measurement. Version 8.2.1.

Tokens
9.4K
Snippets
15
Records
61
Agent score
80%

What's inside loadtest

  1. Performance capabilities of loadtest with TCP sockets

    main

    When using TCP sockets with keep-alive and multiple cores, loadtest can achieve significant performance, reaching up to 100 krps (requests per second) locally. This allows it to compete with high-performance tools like ab, autocannon, and wrk.

    Note: As of the current version, several options for TCP sockets are not yet implemented, such as secure connections via HTTPS.

  2. Boost performance using TCP sockets

    main
    You can significantly improve the performance of loadtest by using raw TCP sockets instead of the standard HTTP module. This is achieved by adding the --tcp flag to your command line invocation. This approach uses the Node.js net module to bypass some of the overhead associated with the http module.
  3. Install loadtest globally via npm

    main

    To use loadtest as a command-line tool, install it globally. On Ubuntu or macOS, you may need to use sudo to grant the necessary permissions.

    # npm install -g loadtest
    
    # On Ubuntu or Mac OS X systems:
    $ sudo npm install -g loadtest
  4. Use keep-alive for improved throughput

    main
    Enabling keep-alive (using the -k option) improves performance by reusing the same connection for multiple requests instead of opening a new socket for every single request. For high-performance testing, combining keep-alive with the --tcp flag provides the best results within the loadtest tool.
  5. Install and run the TCP performance benchmark

    main

    To run the synthetic TCP performance benchmark on Ubuntu 22.04 LTS, first install Node.js via nodesource, then clone the repository and execute the benchmark script. Note that the benchmark bin/tcp-performance.js is designed to start a test server on up to three cores and run the loadtest on a single core to ensure the server can handle the load.

    # Install Node.js
    sudo apt install nodejs
    
    # Download repo and run benchmark
    git clone https://github.com/alexfernandez/loadtest
    cd loadtest
    npm install
    node bin/tcp-performance.js
  6. Run load tests using TCP sockets

    main

    To achieve higher performance by bypassing the standard HTTP implementation and using raw TCP sockets, use the --tcp flag with the loadtest CLI. This mode is particularly effective for high-throughput benchmarking.

    Note: When using --tcp, performance is significantly higher than the standard HTTP mode, but it may have different behaviors regarding header handling and connection management.

    $ node bin/loadtest.js http://localhost:7357 --cores 1 --tcp
  7. Set a constant requests per second (RPS) rate

    main

    Unlike Apache ab, loadtest allows you to specify a fixed rate of requests per second using the --rps option. When using --rps, the --concurrency option is ignored as clients are created on demand to maintain the rate. Note that --rps is not supported for websockets.

    loadtest -c 10 --rps 200 http://mysite.com/
  8. Attach custom data to requests using `request.labels`

    main

    To access custom data within the statusCallback, you can assign values to request.labels inside the requestGenerator. This data will then be available in the statusCallback via result.labels.

    const options = {
        url: 'http://localhost:8000',
        requestGenerator: (params, options, client, callback) => {
            const randomInputData = Math.random().toString().substr(2, 8);
            const message = JSON.stringify({ randomInputData });
            const request = client(options, callback);
            
            // Attach custom data to labels
            request.labels = randomInputData;
            
            request.write(message);
            return request;
        },
        statusCallback: (error, result) => {
            // Access the custom data via result.labels
            console.log('Custom label:', result.labels);
        }
    };
  9. Run basic load tests with the CLI

    main

    Use the loadtest command to simulate load on a URL. Supported protocols are http://, https://, and ws://.

    Basic syntax: loadtest [-n requests] [-c concurrency] [-k] URL

    Key parameters:

    • -n, --maxRequests: Total number of requests to send.
    • -c, --concurrency: Number of concurrent clients (default is 10).
    • -k, --keepalive: Use keep-alive connections (recommended for most tests).
    • -t, --maxSeconds: Maximum time to run the test in seconds (default is 10).
  10. Use the bundled test server

    main

    The testserver-loadtest command runs a simple server that returns 'OK' to help you measure the performance limits of loadtest itself. It provides real-time stats on requests per second and latency.

    Available options:

    • --delay ms: Wait specified milliseconds before answering.
    • --error 5xx: Return a specific error code for every request.
    • --percent yy: Return an error for only the specified % of requests.
    • --cores number: Number of cores to use (defaults to half available).