Skip to content

Commit

Permalink
Merge pull request #156 from pelias/rate-limit
Browse files Browse the repository at this point in the history
feat: Add a rate limit feature
  • Loading branch information
orangejulius authored Jul 19, 2018
2 parents 16208ef + ebf54af commit 45f0f57
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 4 deletions.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ maximum were achieved. The weighting of individual parts of the test can be adju
**Note:** fuzzy-tester requires NPM version 2 or greater. The NPM team
[recommends](http://blog.npmjs.org/post/85484771375/how-to-install-npm) you update NPM using NPM
itself with `sudo npm install -g npm`.
## Usage
## Example Usage

```
// in the root directory of the repo containing the tests
Expand All @@ -26,6 +26,16 @@ fuzzy-tester -e prod
fuzzy-tester -t dev
```


## Command Line Parameters

* `--help` show help :)
* `-e` Select an envronment from `pelias.json` to run tests against. A list of valid environments will be printed if an invalid value or no value is passed
* `-o` Select an output mode. Valid values are `terminal` (default), `csv`, `json`, and `autocomplete` (see below)
* `-q` Enable quiet mode. Only test failures (not successes) are printed
* `-t` Select a test 'type' to filter on. This is a free-text value that can be added to each test, to allow running a subset of tests
* `-r` Set a limit of the number of requests that can be sent per second when running tests. This is useful to avoid overloading a small Pelias server

## Test Case Files
Test-cases are expected to live in `test_cases/`, and are split into test
*suites* in individual JSON files. Each file must contain the following
Expand Down
2 changes: 1 addition & 1 deletion bin/fuzzy-tester
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ var analyze_results = require( '../lib/analyze_results' );
var urls = gather_test_urls(config, testSuites);

// request all urls
request_urls(urls, function processResponses(responses) {
request_urls(config, urls, function processResponses(responses) {
// responses is a simple object where the keys are urls, and the value is the
// entire response from fetching that url
// test cases can have many URLs (in autocomplete mode), and the same url
Expand Down
6 changes: 6 additions & 0 deletions lib/processArguments.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ function setUpCommander() {
endpointHelp,
'prod'
)
.option(
'-r, --rate <int>',
'The maxium number of requests per second (RPS) to allow',
100
)
.option(
'-o, --output <type>',
outputGeneratorHelp,
Expand Down Expand Up @@ -113,6 +118,7 @@ function getConfig() {
url: apiUrl,
name: commander.endpoint
},
rate: commander.rate,
outputGenerator: outputGenerator,
testType: commander.testType,
testSuites: testSuites,
Expand Down
5 changes: 3 additions & 2 deletions lib/request_urls.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,14 @@ function shouldRetryRequest(res) {
return false;
}

function request_urls(urls, callback) {
function request_urls(config, urls, callback) {
var total_length = urls.length;
var responses = {};
var httpAgent = new http.Agent({keepAlive: true, maxSockets: 1});
var httpsAgent = new https.Agent({keepAlive: true, maxSockets: 1});
var intervalId;
var test_interval = new ExponentialBackoff(1, 5, 1, 20000);
const interval = 1000 / config.rate; // the number of miliseconds to delay between requests
var test_interval = new ExponentialBackoff(interval, 5, interval, 20000);
var delay = test_interval.getBackoff();

var getOneUrl = function (){
Expand Down

0 comments on commit 45f0f57

Please sign in to comment.