Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ api-docs
**/*.tgz
packages/*/dist*
examples/*/dist*
benchmark/dist*
**/package
.sandbox
packages/cli/generators/datasource/connectors.json
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ packages/cli/generators/*/templates
**/.sandbox
packages/*/dist*
examples/*/dist*
benchmark/dist*
sandbox/**/*
*.json
CHANGELOG.md
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"**/CVS": true,
".nyc_output": true,
"coverage": true,
"benchmark/dist*": true,
"packages/*/dist*": true,
"packages/*/api-docs": true,
"examples/*/dist*": true,
Expand Down
1 change: 1 addition & 0 deletions benchmark/.npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
25 changes: 25 additions & 0 deletions benchmark/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Copyright (c) IBM Corp. 2018. All Rights Reserved.
Node module: @loopback/benchmark
This project is licensed under the MIT License, full text below.

--------

MIT license

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
59 changes: 59 additions & 0 deletions benchmark/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# benchmark

Benchmarks for LoopBack framework.

## Results

MacBookPro Mid 2015 Processor: 2.5 GHz Intel Core i7 Memory: 16 GB 1600 MHz DDR3

### Requests per seconds

_Average number of requests handled every second._

| scenario | rps |
| ----------------- | ---: |
| find all todos | 4569 |
| create a new todo | 348 |

### Latency

_Average time to handle a request in milliseconds._

| scenario | latency |
| ----------------- | ------: |
| find all todos | 1.68 |
| create a new todo | 28.27 |

## Basic use

Install all dependencies.

```
$ npm install
```

Run the tests to verify the benchmarked scenarios are working correctly.

```
$ npm test
```

Run the benchmark.

```
$ npm start
```

## Contributions

- [Guidelines](https://github.com/strongloop/loopback-next/blob/master/docs/CONTRIBUTING.md)
- [Join the team](https://github.com/strongloop/loopback-next/issues/110)

## Contributors

See
[all contributors](https://github.com/strongloop/loopback-next/graphs/contributors).

## License

MIT
18 changes: 18 additions & 0 deletions benchmark/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright IBM Corp. 2018. All Rights Reserved.
// Node module: @loopback/benchmark
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

const bench = require('@loopback/dist-util').loadDist(__dirname);

module.exports = bench;

if (require.main === module) {
bench.main().then(
success => process.exit(0),
err => {
console.error('Cannot run the benchmark.', err);
process.exit(1);
},
);
}
6 changes: 6 additions & 0 deletions benchmark/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Copyright IBM Corp. 2018. All Rights Reserved.
// Node module: @loopback/benchmark
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

export * from './src';
62 changes: 62 additions & 0 deletions benchmark/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
{
"name": "@loopback/benchmark",
"version": "0.1.0",
"private": true,
"description": "Benchmarks measuring performance of our framework.",
"keywords": [
"loopback",
"performance",
"benchmark"
],
"main": "index.js",
"engines": {
"node": ">=8.9"
},
"scripts": {
"build:all-dist": "npm run build:dist8 && npm run build:dist10",
"build": "lb-tsc",
"build:dist8": "lb-tsc es2017",
"build:dist10": "lb-tsc es2018",
"clean": "lb-clean dist*",
"pretest": "npm run clean && npm run build",
"test": "lb-mocha \"DIST/test\"",
"prestart": "npm run build",
"start": "node .",
"prepublishOnly": "npm run test"
},
"repository": {
"type": "git"
},
"author": "",
"license": "MIT",
"files": [
"README.md",
"index.js",
"index.d.ts",
"dist*/src",
"dist*/index*",
"src"
],
"dependencies": {
"@loopback/dist-util": "^0.3.6",
"@loopback/example-todo": "^0.15.0",
"@types/byline": "^4.2.31",
"@types/debug": "0.0.30",
"@types/p-event": "^1.3.0",
"@types/request-promise-native": "^1.0.15",
"autocannon": "^2.4.1",
"byline": "^5.0.0",
"debug": "^3.1.0",
"request": "^2.88.0",
"request-promise-native": "^1.0.5"
},
"devDependencies": {
"@loopback/build": "^0.6.14",
"@loopback/testlab": "^0.11.3",
"@types/mocha": "^5.0.0",
"@types/node": "^10.1.1",
"mocha": "^5.1.1",
"p-event": "^2.1.0",
"source-map-support": "^0.5.5"
}
}
45 changes: 45 additions & 0 deletions benchmark/src/autocannon.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright IBM Corp. 2018. All Rights Reserved.
// Node module: @loopback/benchmark
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

import * as assert from 'assert';
import {promisify} from 'util';
const autocannon = promisify(require('autocannon'));

export interface EndpointStats {
requestsPerSecond: number;
latency: number;
}

export class Autocannon {
constructor(protected url: string, protected duration: number) {}

async execute(
title: string,
urlPath: string,
options?: object,
): Promise<EndpointStats> {
const defaults = {
url: this.buildUrl(urlPath),
duration: this.duration,
title,
};
const config = Object.assign(defaults, options);
const data = await autocannon(config);
assert.equal(
data.non2xx,
0,
'No request should have failed with non-2xx status code.',
);
const stats: EndpointStats = {
requestsPerSecond: data.requests.average,
latency: data.latency.average,
};
return stats;
}

protected buildUrl(urlPath: string) {
return this.url + urlPath;
}
}
121 changes: 121 additions & 0 deletions benchmark/src/benchmark.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
// Copyright IBM Corp. 2018. All Rights Reserved.
// Node module: @loopback/benchmark
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

import * as byline from 'byline';
import {ChildProcess, spawn} from 'child_process';
import * as pEvent from 'p-event';
import {Autocannon, EndpointStats} from './autocannon';
import {Client} from './client';
import {scenarios} from './scenarios';

const debug = require('debug')('loopback:benchmark');

export interface Scenario {
setup(client: Client): Promise<void>;
execute(autocannon: Autocannon): Promise<EndpointStats>;
}

export type ScenarioFactory = new () => Scenario;

export interface Options {
/**
* How long to run the benchmark - time in seconds.
* Default: 30 seconds.
*/
duration: number;
}

export interface Result {
[scenario: string]: EndpointStats;
}

export type AutocannonFactory = (url: string) => Autocannon;

export class Benchmark {
private options: Options;
private worker: ChildProcess;
private url: string;

// Customization points
public cannonFactory: AutocannonFactory;
public logger: (title: string, stats: EndpointStats) => void;

constructor(options?: Partial<Options>) {
this.options = Object.assign(
{
duration: 30 /* seconds */,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we up the number of concurrent connections from a default of 10 to 50 / 100?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be honest, my knowledge of performance testing of HTTP is limited and I cannot asses possible ramifications of changing the number of concurrent connections from 10 to 50 or 100. I assume the authors of autocannon have choosen a default value that should work well for majority of users.

What is your reasoning for changing this config option?

},
options,
);
this.logger = function() {};
this.cannonFactory = url => new Autocannon(url, this.options.duration);
}

async run(): Promise<Result> {
const result: Result = {};
for (const name in scenarios) {
result[name] = await this.runScenario(name, scenarios[name]);
}
return result;
}

async runScenario(
name: string,
scenarioFactory: ScenarioFactory,
): Promise<EndpointStats> {
debug('Starting scenario %j', name);
const {worker, url} = await startWorker();
debug('Worker started - pid=%s url=%s', worker.pid, url);

const client = new Client(url);
const autocannon = this.cannonFactory(url);

const runner = new scenarioFactory();
debug('Setting up the scenario');
await runner.setup(client);
debug('Pinging the app');
await client.ping();
debug('Starting the stress test.');
const result = await runner.execute(autocannon);
debug('Stats: %j', result);

closeWorker(worker);
debug('Worker stopped, done.');

this.logger(name, result);

return result;
}
}

function startWorker() {
return new Promise<{worker: ChildProcess; url: string}>((resolve, reject) => {
const child = spawn(
process.execPath,
['--expose-gc', require.resolve('./worker')],
{
stdio: ['pipe', 'pipe', 'inherit'],
},
);

child.once('error', reject);
child.once('exit', (code, signal) =>
reject(new Error(`Child exited with code ${code} signal ${signal}`)),
);

const reader = byline.createStream(child.stdout);
reader.once('data', line => resolve({worker: child, url: line.toString()}));
reader.on('data', line => debug('[worker] %s', line.toString()));
});
}

async function closeWorker(worker: ChildProcess) {
worker.kill();
await pEvent(worker, 'close');
}

function sleep(ms: number) {
return new Promise(resolve => setTimeout(resolve, ms));
}
22 changes: 22 additions & 0 deletions benchmark/src/client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright IBM Corp. 2018. All Rights Reserved.
// Node module: @loopback/benchmark
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

import * as request from 'request-promise-native';
import {Todo} from '@loopback/example-todo';

export class Client {
constructor(private url: string) {}

createTodo(data: Partial<Todo>) {
return request.post(`${this.url}/todos`, {
json: true,
body: data,
});
}

ping() {
return request.get(`${this.url}/todos`);
}
}
Loading