-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from bajtos/add-loopback
Add loopback 3.x and 4.x
- Loading branch information
Showing
16 changed files
with
223 additions
and
29 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -1,3 +1,6 @@ | ||
# Transpiled files | ||
lib/loopback-next.* | ||
|
||
# Logs | ||
logs | ||
*.log | ||
|
This file contains 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 |
---|---|---|
@@ -1,10 +1,44 @@ | ||
# async-frameworks | ||
A benchmark comparing performance of async/await in different HTTP frameworks | ||
|
||
- [email protected] | ||
- [email protected] + [email protected] | ||
- [email protected] | ||
- [email protected] | ||
- **Express** v4.16 | ||
- **Koa** v2.5 + koa-router v7.4 | ||
- **Fastify** v1.8 | ||
- **Hapi** v17.5 | ||
- **LoopBack** v3.21 | ||
- **LoopBack next**: core v0.11; repository v0.14; rest v0.19 | ||
|
||
## Results | ||
|
||
MacBookPro Mid 2015 | ||
Processor: 2.5 GHz Intel Core i7 | ||
Memory: 16 GB 1600 MHz DDR3 | ||
|
||
### Requests per seconds | ||
|
||
framework|rps | ||
-|-: | ||
hapi | 6029 | ||
fastify | 7926 | ||
koa | 7305 | ||
express | 5778 | ||
loopback | 3072 | ||
loopback-next | 2778 | ||
|
||
### Latency | ||
|
||
_Time to handle a request in milliseconds._ | ||
|
||
framework|latency | ||
-|-: | ||
hapi | 1.14 | ||
fastify | 0.93 | ||
koa | 1.02 | ||
express | 1.2 | ||
loopback | 2.69 | ||
loopback-next | 3.16 | ||
|
||
Async/await is not the bottleneck! | ||
|
||
## Usage | ||
|
||
|
@@ -42,16 +76,3 @@ Run the benchmark. | |
$ npm start | ||
``` | ||
|
||
## Results | ||
|
||
MacBookPro Mid 2015 | ||
Processor: 2.5 GHz Intel Core i7 | ||
Memory: 16 GB 1600 MHz DDR3 | ||
|
||
Average requests per seconds: | ||
- hapi: 6188.9 | ||
- fastify: 7800 | ||
- koa: 7334.1 | ||
- express: 6147.77 | ||
|
||
Async/await is not the bottleneck! |
This file contains 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 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 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 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 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 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 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 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,47 @@ | ||
'use strict'; | ||
|
||
const loopback = require('loopback'); | ||
const promisify = require('util').promisify; | ||
|
||
const app = loopback(); | ||
app.use(loopback.rest()); | ||
|
||
exports.start = promisify(function start(cb) { | ||
app.listen(0, function() { cb(null, this.address().port); }); | ||
}); | ||
|
||
/** Setup Product model and MongoDB connection **/ | ||
|
||
const Product = app.registry.createModel({ | ||
name: 'Product', | ||
properties: { | ||
_id: {type: String, id: true}, | ||
ean: {type: Number, required: true}, | ||
name: {type: String, required: true}, | ||
}, | ||
mongodb: { | ||
collection: 'products', | ||
}, | ||
}); | ||
|
||
app.dataSource('db', { | ||
connector: 'mongodb', | ||
useNewUrlParser: true, | ||
url: require('./db').url, | ||
}); | ||
|
||
app.model(Product, {dataSource: 'db'}); | ||
|
||
/** Setup REST API **/ | ||
|
||
Product.findByEan = function(ean) { | ||
return this.find({where: {ean}}); | ||
}; | ||
|
||
Product.remoteMethod('findByEan', { | ||
accepts: {arg: 'ean', type: 'number', required: true}, | ||
returns: {arg: 'result', type: Product, root: true}, | ||
http: {verb: 'get', path: '/:ean'} | ||
}); | ||
|
||
Product.disableRemoteMethodByName('findById'); |
This file contains 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 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 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,87 @@ | ||
import { get, param, RestApplication, RestBindings } from '@loopback/rest'; | ||
import { inject } from '../node_modules/@loopback/core'; | ||
import { DefaultCrudRepository, Entity, juggler, model, property, RepositoryMixin, repository } from '../node_modules/@loopback/repository'; | ||
|
||
const dbConfig = { | ||
connector: 'mongodb', | ||
url: require('../lib/db').url, | ||
}; | ||
|
||
class DbDataSource extends juggler.DataSource { | ||
static dataSourceName = 'db'; | ||
|
||
constructor( | ||
@inject('datasources.config.db', { optional: true }) | ||
dsConfig: object = dbConfig, | ||
) { | ||
super(dsConfig); | ||
} | ||
} | ||
|
||
@model({ | ||
settings: { | ||
mongodb: { | ||
collection: 'products', | ||
}, | ||
} | ||
}) | ||
class Product extends Entity { | ||
@property({ id: true }) | ||
_id: string | ||
|
||
@property({ required: true }) | ||
ean: number; | ||
|
||
@property({ required: true }) | ||
name: string; | ||
|
||
getId() { | ||
return this._id; | ||
} | ||
} | ||
|
||
class ProductRepository extends DefaultCrudRepository< | ||
Product, | ||
typeof Product.prototype._id | ||
> { | ||
constructor( | ||
@inject('datasources.db') protected datasource: juggler.DataSource, | ||
) { | ||
super(Product, datasource); | ||
} | ||
} | ||
|
||
class ProductController { | ||
constructor( | ||
@repository(ProductRepository) | ||
protected repo: ProductRepository, | ||
) { } | ||
|
||
@get('/products/{ean}') | ||
findByEan( | ||
@param.path.number('ean') | ||
ean: number | ||
) { | ||
return this.repo.find({where: {ean}}); | ||
} | ||
} | ||
|
||
class BenchApp extends RepositoryMixin(RestApplication) { | ||
constructor() { | ||
super({ | ||
rest: { port: 0 }, | ||
}); | ||
|
||
this.bind('datasources.config.db').to(dbConfig); | ||
this.dataSource(DbDataSource); | ||
this.repository(ProductRepository); | ||
this.controller(ProductController); | ||
} | ||
} | ||
|
||
const app = new BenchApp(); | ||
|
||
export async function start() { | ||
await app.start(); | ||
return app.restServer.get(RestBindings.PORT); | ||
} |
This file contains 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 @@ | ||
--exit |
This file contains 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 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 @@ | ||
{ | ||
"$schema": "http://json.schemastore.org/tsconfig", | ||
"compilerOptions": { | ||
"emitDecoratorMetadata": true, | ||
"experimentalDecorators": true, | ||
"noImplicitAny": true, | ||
"strictNullChecks": true, | ||
|
||
"outDir": "lib", | ||
|
||
"lib": ["es2018", "dom"], | ||
"module": "commonjs", | ||
"moduleResolution": "node", | ||
"target": "es2017", | ||
"sourceMap": true, | ||
"declaration": true | ||
|
||
}, | ||
"include": [ | ||
"src/loopback-next.ts" | ||
] | ||
} |