Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@sitespeed.io/throttle

sitespeedio19.4kMIT5.0.1

Throttle your connection

latency, throttle, bandwith, webperf, perfmatters

readme

Simulate slow network connections on Linux and Mac OS X

Linux OSX Docker

Inspired by tylertreat/Comcast, the connectivity setting in the WPTAgent and sltc.

Throttle uses pfctl on Mac and tc on Linux to simulate different network speeds. On Linux you also need ip for Throttle to work (install using sudo apt-get install -y net-tools).

You can set the download/upload speed and/or RTT. Upload/download is in kbit/s and RTT in ms.

Use with latest NodeJS LTS.

Install

npm install @sitespeed.io/throttle -g

On OSX, add these lines to /etc/pf.conf if they don't exist, to prevent the pfctl: Syntax error in config file: pf rules not loaded error when you try to run throttle

pf_enable="YES"
pflog_enable="YES"

On Linux you need to make sure ip and route is installed (install using sudo apt-get install -y net-tools).

Start simulate a slower network connection

Here is an example for running with 3G connectivity. Remember: Throttle will use sudo so your user will need sudo rights.

throttle --up 330 --down 780 --rtt 200

Pre made profiles

To make it easier we have pre made profiles, check them out by throttle --help:

--profile         Premade profiles, set to one of the following
                     3g: up:768 down:1600 rtt:150
                     3gfast: up:768 down:1600 rtt:75
                     3gslow: up:400 down:400 rtt:200
                     2g: up:256 down:280 rtt:400
                     cable: up:1000 down:5000 rtt:14
                     dsl: up:384 down:1500 rtt:14
                     3gem: up:400 down:400 rtt:200
                     4g: up:9000 down:9000 rtt:85
                     lte: up:12000 down:12000 rtt:35
                     edge: up:200 down:240 rtt:35
                     dial: up:30 down:49 rtt:60
                     fois: up:5000 down:20000 rtt:2

You can start throttle with one of the premade profiles:

throttle --profile 3gslow

or even simpler

throttle 3gslow

Add packet loss

By default there's no packet loss. That is by design: If you want to use Throttle and have the same network speed, packet loss is no good. However if you want to simalate a really crappy network you probably want to add packet loss. You do that with the --packetLoss option. You set the packet loss in percent.

throttle --profile 3gslow --packetLoss 5

Use a configuration file

You can also use a configuration file with your settings. Use --config to map your config file to throttle.

config.json

{
    "up": 330 ,
    "down": 200,
    "rtt": 1000
}

And then run:

throttle --config config.json

Stop simulate the network

Stopping is as easy as giving the parameter stop to throttle.

throttle --stop

or

throttle stop

Add delay on your localhost

This is useful if you test a local web server or run WebPageReplay and want to add some latency to your tests.

throttle --rtt 200 --localhost

Stop adding delay on localhost

throttle --stop --localhost

Use directly in NodeJS

import throttle from '@sitespeed.io/throttle'
// Returns a promise
throttle.start({up: 360, down: 780, rtt: 200}).then(() => ...

or

import throttle from '@sitespeed.io/throttle'
// Returns a promise
const options = {up: 360, down: 780, rtt: 200};
await throttle.start(options);
// Do your thing and then stop
await throttle.stop();

Log all commands

You can log all the commands that sets up the throttling by setting LOG_THROTTLE=true.

LOG_THROTTLE=true throttle 3gslow

or

throttle 3gslow --log

Run in Docker (on Linux)

Make sure to run sudo modprobe ifb numifbs=1 before you start the container.

And then when you actually start your Docker container, give it the right privileges with --cap-add=NET_ADMIN.

changelog

CHANGELOG - throttle

5.0.1 - 2024-08-26

Fixed

  • Smarter ifb0 handling on Linux #93.

5.0.0 - 2022-07-02

Changed

  • Dropped support for NodeJS 12 since it's unmaintaned.

Tech

  • Code cleanup using eslint-plugin-unicorn.

    4.0.3 - 2022-06-29

    Fixed

  • Fixed --version so it actually shows the version again #75.

    4.0.2 - 2022-06-21

    Fixed

  • The E6 Module convertion in 4.0 broke version check (--version) making installs failed. PR #74 is a temporaty fix for that.

4.0.1 - 2022-06-17

Fixed

  • Removed the dependecy of route. Using ip route instead and we already have ip as a requirement.

    4.0.0 - 2022-04-14

    Changed

  • Converting the project to ES6 module. If you run Throttle by command line it will work exactly as before except that we dropped support for NodeJS 10 #67. You can read Sindre Sorhus ESM package guide.

Fixed

  • Updated dev dependencies to latest versions #68

3.1.1 - 2022-04-19

Fixed

  • Updated minimist dependency.

    3.1.0 - 2021-12-26

    Added

  • Add support for setting packet loss. Add --packetLoss and set the loss in percentage.

    3.0.0 - 2021-08-09

Changed

  • When you run on Mac OS we changed so localhost traffic is not throttled by default. To throttle on localhost use --localhost. This makes sence if you use WebDriver to drive your browser on your local machine. And Mac OS now works the same as Linux.

    2.1.2 - 2021-07-29

    Fixed

  • Fix breakage when multiple default routes are declared , thank you Andy Richardson for PR #62.

    2.1.1 - 2021-03-19

    Fixed

  • A better check for missing net-tools on Linux, thank you Radu Micu for the PR #56.

    2.1.0 - 2021-03-10

    Added

  • Added support for config files. Use --config config.json to read configuration setup from a file.

    2.0.2 - 2020-09-10

    Fixed

  • Make the install as small as possible.

2.0.1 - 2020-08-22

Fixed

  • Removed the execa dependency to make sure we have minimal dependencies #53.

2.0.0 - 2020-08-14

Changed

  • You can now set one of up/down/rtt if you like as introduced by Iñaki Baz Castillo - thank you! Implemented in #46. This also remove the default profile meaning there's a change in behavior if you run throttle without any parameters. Before a default profile was used but now you get an error (you know need to set a profile or set up/down or rtt).

Added

  • You can use --log to log all networks commands #45.
  • Added support to add delay on for localhost on OS X. Use --rtt 100 --localhost #51.

Fixed

  • Add missing await when removing the ingress if removing root fails for tc #47.
  • Better error handling in the CLI #48. Make sure that the exit code is > 0 if setting the throttling fails.

Tech

  • Removed configuration file for pfctl to make it easier to set up dynamic dummynet #49

1.1.0 2020-07-29

Fixed

  • Updated execa to 4.0.3.
  • Removed log message "using default profile"

Added

  • You can log all the commands that sets up the throttling by setting LOG_THROTTLE=true #44.

1.0.3 2020-06-20

Fixed

  • Updated minimist

1.0.2 2020-03-09

Fixed

  • Fixed dependency tree and npm-shrinkwrap to only hold dependencies for production.

1.0.1 2020-02-03

Fixed

  • Another fix to 2g to get that right, thank you Matt! #37.

1.0.0 2020-02-03

Changed

  • 2g speed was updated to become more usable and follow WPT #36 - thank you Matt Hobbs for the PR.

Added

  • More pre-made profiles: dsl, 3gem, 4g, lte, edge, dial, fois #36 - thank you Matt Hobbs for the PR.

0.5.4 2019-08-26

Fixed

  • Another execa fix, hopefully fixing the last thing + added more tests in Travis.

0.5.3 2019-08-26

Fixed

  • Over optimistic Execa upgraded caused Throttle to stop working #32.

0.5.2 2019-08-26

Fixed

  • Updated dependencies #31.

0.5.1 2019-04-23

Fixed

0.5.0 2018-12-07

Added

  • Simplified profile/stop/help. You can now start with: throttle $profile and stop with throttle stop

0.4.3 2018-09-01

Fixed

  • Upload throttling was wrong on Mac OS X, thank you Paul for the PR.

0.4.2 2018-05-30

Fixed

  • Catching when ifb has already been setup.

0.4.1 2018-05-30

Fixed

  • Another go at trying to try/catch failing settings.

0.4.0 2018-05-30

Added

  • Rewrite to async/await from promises

0.3.0 2018-04-12

Fixed

  • Ensure setup has completed fully before returning when starting throttling with tc.
  • Always return Promises from start() and stop(), even in case of errors.
  • Typo in the CLI help
  • Simpler way to set connectivity with tc

0.2.0 2017-10-31

Added

  • You can now see the version with --version
  • You can now use pre defined profiles.

0.1.0 2017-10-13

Fixed

  • Always remove filters before we try to set them.