npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

workload

v2.4.3

Published

Sends HTTP requests to a server to mimic a natual load

Downloads

228

Readme

workload

Sends HTTP requests to a server to mimic a natual load.

The requests are randomized using weights, so certain requests appear more often than others.

It's also possible to mimic regular working hours so that the average number of requests are lower at night and in the weekends.

This module is not intended to benchmark an HTTP server. If that's your use-case I suggest you take a look at autocannon instead. This module is meant to help you simulate a real-world workload over a longer period of time.

Build status js-standard-style sponsor

Installation

For use from the command line, install globally:

npm install workload --global

For use programmatically:

npm install workload --save

CLI Usage

Example making max 60 requests per minute to 5 different URL's with different weights:

workload --max 60 \
  POST,http://example.com/signup,"Hello World" \
  10,http://example.com/ \
  2,http://example.com/foo \
  4,http://example.com/bar \
  8,http://example.com/baz

Run workload --help for all options.

Programmatic Usage

var Workload = require('workload')

var workload = new Workload({
  max: 30, // make a request once every 2 seconds maximum
  filter: Workload.stdFilters.workingHours,
  requests: [
    {weight: 1, url: 'http://example.com/signup', method: 'POST', body: '...'},
    {weight: 10, url: 'http://example.com/'},
    {weight: 2, url: 'http://example.com/foo'},
    {weight: 4, url: 'http://example.com/bar'},
    {weight: 8, url: 'http://example.com/baz'}
  ]
})

// stop after 1 minute
setTimeout(function () {
  workload.stop()
}, 60000)

API

var workload = new Workload(options)

Create a new workloader. The workload object is an EventEmitter.

The constructor takes the following options:

  • requests - An array of request objects (see below)
  • max - The maximum number of requests to make per minute (defaults to 12)
  • headers - An object containing the default HTTP headers to use for each request
  • filter - An optional filter single function (shorthand for filters: [filter]) - see Filters for details
  • filters - An optional array of filter functions which will be called sequentially - see Filters for details

Each request object can contain the following properties:

  • url - The URL to request
  • method - The HTTP method to use (defaults to GET)
  • weight - The chance that this request will be performed compared to the other requests (defaults to 1)
  • headers - A object containing HTTP headers to use for the request (overrules options.headers)
  • body - Entity body for PATCH, POST and PUT requests. Must be a Buffer, String or ReadStream. If json is true, then body must be a JSON-serializable object
  • json - Sets body to JSON representation of value and adds Content-type: application/json header. Additionally, parses the response body as JSON
  • For additional options, see the options accepted by the request module.

Event: error

Emitted if an error occurs during one of the requests.

Event: visit

Emitted every time a request have been successfully performed. An object with the following properties is emitted:

  • request - The request options used when making the request
  • response - The response object (an http.IncomingMessage instance)
  • body - The body of the response

workload.stop()

Stop making requests.

Workload.stdFilters.workdays

This filter lowers the chances of a request being made during weekends.

Workload.stdFilters.workingHours

This filter lowers the chances of a request being made during weekends and at night.

Workload.stdFilters.expand

This filter expands braces in URL's and picks a random matching URL.

For instance, given a request with a URL of http://example.com/foo/{1..10} this filter will replace the {1..10} part of the URL with a random number between 1 and 10. So the actual requested URL might be /foo/4.

Filters

A filter is a function that will either lower the chance of a request being made or modify the request in some way.

The function is called every time a request is ready to be made. It's passed the request object and a callback. Whether or not it calls the callback determins if the request is performed or not.

It's also possible to modify the request inside a filter function by manipulating the request object and passing it as an argument to the callback.

Example filter function that only makes requests between 6am and 7am:

function (request, next) {
  var hour = (new Date()).getHours()
  if (hour === 6) next()
}

Example filter function that modifies the request URL:

function (request, next) {
  request.url += '/' + Math.random()
  next(request)
}

Acknowledgements

This project was kindly sponsored by Opbeat.

License

MIT