Skip to content

Commit

Permalink
Merge pull request #1642 from kanongil/more-benchmarks
Browse files Browse the repository at this point in the history
Additional tests
  • Loading branch information
Marsup committed Nov 11, 2018
2 parents f1a4072 + 2522316 commit 7c442eb
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 11 deletions.
18 changes: 15 additions & 3 deletions benchmarks/bench.js
Expand Up @@ -6,6 +6,7 @@ const Bossy = require('bossy');
const Chalk = require('chalk');
const CliTable = require('cli-table');
const D3 = require('d3-format');
const Hoek = require('hoek');

const definition = {
c: {
Expand Down Expand Up @@ -45,11 +46,22 @@ const Suite = new Benchmark.Suite('joi');

const test = ([name, initFn, testFn]) => {

const [schema, value] = initFn();
Suite.add(name, () => {
const [schema, valid, invalid] = initFn();

testFn(schema, value);
Hoek.assert(valid === undefined || testFn(schema, valid).error === null, 'validation must not fail for: ' + name);
Hoek.assert(invalid === undefined || testFn(schema, invalid).error !== null, 'validation must fail for: ' + name);

Suite.add(name + (valid !== undefined ? ' (valid)' : ''), () => {

testFn(schema, valid);
});

if (invalid !== undefined) {
Suite.add(name + ' (invalid)', () => {

testFn(schema, invalid);
});
}
};

require('./suite').forEach(test);
Expand Down
6 changes: 3 additions & 3 deletions benchmarks/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion benchmarks/package.json
Expand Up @@ -10,6 +10,7 @@
"bossy": "^4.0.3",
"chalk": "^2.4.1",
"cli-table": "^0.3.1",
"d3-format": "^1.3.2"
"d3-format": "^1.3.2",
"hoek": "^6.0.2"
}
}
51 changes: 47 additions & 4 deletions benchmarks/suite.js
Expand Up @@ -12,11 +12,12 @@ module.exports = [
.valid(['debug', 'info', 'notice'])
.required()
}).unknown(false),
{ id: '1', level: 'info' }
{ id: '1', level: 'info' },
{ id: '2', level: 'warning' }
],
(schema, value) => {

schema.validate(value, { convert: false });
return schema.validate(value, { convert: false });
}
],
[
Expand All @@ -28,11 +29,12 @@ module.exports = [
.valid(['debug', 'info', 'notice'])
.required()
}).unknown(false).options({ convert: false }),
{ id: '1', level: 'info' }
{ id: '1', level: 'info' },
{ id: '2', level: 'warning' }
],
(schema, value) => {

schema.validate(value);
return schema.validate(value);
}
],
[
Expand Down Expand Up @@ -64,5 +66,46 @@ module.exports = [
.default(() => 'foo', 'Def')
.optional();
}
],
[
'Schema creation with long valid() list',
() => {

const list = [];
for (let i = 10000; i < 50000; i++) {
list.push(i.toString());
}
return [list.filter(x => !['12345', '23456', '34567', '456789'].includes(x))];
},
(list) => {

Joi.object().keys({
foo: Joi.string().valid(list)
});
}
],
[
'String with long valid() list',
() => {

const list = [];
for (let i = 10000; i < 50000; i++) {
list.push(i.toString());
}

const schema = Joi.string().valid(list);

let i = 0;
const value = () => {

return `${10000 + (++i % 40000)}`;
};

return [ schema, value, () => '5000' ];
},
(schema, value) => {

return schema.validate(value());
}
]
];

0 comments on commit 7c442eb

Please sign in to comment.