-
Notifications
You must be signed in to change notification settings - Fork 1.1k
feat: add two simple benchmarks #1583
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| package-lock=false |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| }, | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 */, | ||
| }, | ||
| 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)); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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`); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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?