-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
45c4a13
commit ce0a0f0
Showing
7 changed files
with
223 additions
and
9 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,4 @@ | ||
node_modules | ||
lib | ||
.nyc_output | ||
benchmark/build |
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 |
---|---|---|
|
@@ -3,3 +3,4 @@ | |
npm-debug.log | ||
node_modules | ||
lib | ||
benchmark/build |
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,119 @@ | ||
/* eslint-disable no-console */ | ||
/* | ||
* | ||
* node run.js 10 | ||
* | ||
*/ | ||
const { | ||
object, | ||
string, | ||
} = require('joi'); | ||
const async = require('./build/async').default; // eslint-disable-line import/no-unresolved | ||
const promise = require('./build/promise').default; // eslint-disable-line import/no-unresolved | ||
|
||
const schema = { | ||
body: object().keys({ | ||
name: string().required(), | ||
tel: string().required(), | ||
addr: string().required(), | ||
email: string().email().required(), | ||
}), | ||
}; | ||
|
||
const options = { stripUnknown: true }; | ||
|
||
const asyncMiddleware = async(schema, options); | ||
const promiseMiddleware = promise(schema, options); | ||
|
||
const ctx = { | ||
request: { | ||
body: { | ||
name: 'CPH', | ||
tel: '0912345678', | ||
addr: '..', | ||
email: 't@t.t', | ||
}, | ||
}, | ||
}; | ||
|
||
const ROUND = +process.argv[2] || 100; | ||
|
||
const format = (ms) => { | ||
let remain = ms; | ||
let formated = ''; | ||
|
||
formated = `${ms % 1000}ms`; | ||
remain = Math.round(remain / 1000); | ||
if (remain < 1) return formated; | ||
|
||
formated = `${remain % 60}sec ${formated}`; | ||
remain = Math.round(remain / 60); | ||
if (remain < 1) return formated; | ||
|
||
formated = `${remain % 60}min ${formated}`; | ||
remain = Math.round(remain / 60); | ||
if (remain < 1) return formated; | ||
|
||
formated = `${remain % 24}hr ${formated}`; | ||
remain = Math.round(remain / 24); | ||
if (remain < 1) return formated; | ||
|
||
formated = `${remain}day ${formated}`; | ||
return formated; | ||
}; | ||
|
||
console.log(`${ROUND} rounds to run..`); | ||
|
||
const results = [[], [], []]; | ||
|
||
let p = Promise.resolve(); | ||
|
||
p.then(() => { | ||
console.log('Start to run async function...'); | ||
}); | ||
|
||
for (let i = 0; i < ROUND; i++) { | ||
let start; | ||
p = p | ||
.then(() => { | ||
start = Date.now(); | ||
}) | ||
.then(() => asyncMiddleware(ctx, () => Promise.resolve())) | ||
.then(() => { | ||
results[0].push(Date.now() - start); | ||
}); | ||
} | ||
|
||
p.then(() => { | ||
console.log('Start to run promise function...'); | ||
}); | ||
|
||
for (let i = 0; i < ROUND; i++) { | ||
let start; | ||
p = p | ||
.then(() => { | ||
start = Date.now(); | ||
}) | ||
.then(() => promiseMiddleware(ctx, () => Promise.resolve())) | ||
.then(() => { | ||
results[1].push(Date.now() - start); | ||
}); | ||
} | ||
|
||
p | ||
.then(() => { | ||
console.log(results[0].map(format)); | ||
const asyncSum = results[0].reduce((acc, next) => acc + next, 0); | ||
console.log('async Average: ', format(asyncSum / ROUND)); | ||
console.log(results[1].map(format)); | ||
const promiseSum = results[1].reduce((acc, next) => acc + next, 0); | ||
console.log('promise Average: ', format(promiseSum / ROUND)); | ||
console.log(results[2].map(format)); | ||
const fastAsyncSum = results[2].reduce((acc, next) => acc + next, 0); | ||
console.log('fast-async Average: ', format(fastAsyncSum / ROUND)); | ||
process.exit(0); | ||
}) | ||
.catch(err => { | ||
console.error(err); | ||
process.exit(1); | ||
}); |
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,44 @@ | ||
import { validate as _validate } from 'joi'; | ||
import thenify from 'thenify'; | ||
|
||
const validate = thenify(_validate); | ||
|
||
export { | ||
any, | ||
alternatives, | ||
array, | ||
boolean, | ||
binary, | ||
date, | ||
func, | ||
number, | ||
object, | ||
string, | ||
ref, | ||
isRef, | ||
} from 'joi'; | ||
|
||
function isContextOnlyKey(key) { | ||
return key === 'params'; | ||
} | ||
|
||
const validator = (schema, opts) => async (ctx, next) => { | ||
const keys = Object.keys(schema); | ||
for (let i = 0, len = keys.length; i < len; i++) { | ||
const key = keys[i]; | ||
const source = isContextOnlyKey(key) ? ctx : ctx.request; | ||
const validated = await validate( // eslint-disable-line no-param-reassign | ||
source[key], | ||
schema[key], | ||
opts | ||
); | ||
Object.defineProperty(source, [key], { | ||
get() { | ||
return validated; | ||
}, | ||
}); | ||
} | ||
await next(); | ||
}; | ||
|
||
export default validator; |
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,46 @@ | ||
import { validate as _validate } from 'joi'; | ||
import thenify from 'thenify'; | ||
|
||
const validate = thenify(_validate); | ||
|
||
export { | ||
any, | ||
alternatives, | ||
array, | ||
boolean, | ||
binary, | ||
date, | ||
func, | ||
number, | ||
object, | ||
string, | ||
ref, | ||
isRef, | ||
} from 'joi'; | ||
|
||
function isContextOnlyKey(key) { | ||
return key === 'params'; | ||
} | ||
|
||
const validator = (schema, opts) => (ctx, next) => { | ||
const keys = Object.keys(schema); | ||
const promises = []; | ||
for (let i = 0, len = keys.length; i < len; i++) { | ||
const key = keys[i]; | ||
const source = isContextOnlyKey(key) ? ctx : ctx.request; | ||
promises.push(validate( | ||
source[key], | ||
schema[key], | ||
opts | ||
).then(validated => { | ||
Object.defineProperty(source, [key], { | ||
get() { | ||
return validated; | ||
}, | ||
}); | ||
})); | ||
} | ||
return Promise.all(promises).then(() => next()); | ||
}; | ||
|
||
export default validator; |
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