Skip to content

Commit 5f2ccab

Browse files
committed
Add initial benchmarking support.
1 parent ce41d4a commit 5f2ccab

File tree

7 files changed

+109
-11
lines changed

7 files changed

+109
-11
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# jsonld ChangeLog
22

3+
### Added
4+
- Initial support for benchmarking.
5+
36
## 1.1.0 - 2018-09-05
47

58
### Added

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,14 @@ To generate earl reports:
363363
# generate the earl report for the browser
364364
EARL=earl-firefox.jsonld npm run test-karma -- --browser Firefox
365365

366+
Benchmarks
367+
----------
368+
369+
Benchmarks can be created from any manifest that the test system supports.
370+
Use a command line with a test suite and a benchmark flag:
371+
372+
JSONLD_TESTS=/tmp/benchmark-manifest.jsonld JSONLD_BENCHMARK=1 npm test
373+
366374
[Digital Bazaar]: http://digitalbazaar.com/
367375
[JSON-LD]: http://json-ld.org/
368376
[JSON-LD 1.0]: http://www.w3.org/TR/2014/REC-json-ld-20140116/

karma.conf.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ module.exports = function(config) {
6565
plugins: [
6666
new webpack.DefinePlugin({
6767
'process.env.JSONLD_TESTS': JSON.stringify(process.env.JSONLD_TESTS),
68+
'process.env.JSONLD_BENCHMARK':
69+
JSON.stringify(process.env.JSONLD_BENCHMARK),
6870
'process.env.TEST_ROOT_DIR': JSON.stringify(__dirname),
6971
'process.env.EARL': JSON.stringify(process.env.EARL),
7072
'process.env.BAIL': JSON.stringify(process.env.BAIL)

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
"babel-plugin-transform-object-rest-spread": "^6.26.0",
4343
"babel-preset-env": "^1.6.1",
4444
"babel-preset-node6-es6": "^11.2.5",
45+
"benchmark": "^2.1.4",
4546
"browserify": "^15.2.0",
4647
"chai": "^4.1.2",
4748
"commander": "^2.14.1",

tests/test-common.js

Lines changed: 61 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* Copyright (c) 2011-2017 Digital Bazaar, Inc. All rights reserved.
88
*/
99
const EarlReport = require('./earl-report');
10+
const benchmark = require('benchmark');
1011
const join = require('join-path-js');
1112

1213
module.exports = function(options) {
@@ -269,14 +270,22 @@ function addTest(manifest, test, tests) {
269270
test.manifest = manifest;
270271
const description = test_id + ' ' + (test.purpose || test.name);
271272

272-
tests.push({
273-
title: description + ' (promise)',
274-
f: makeFn({useCallbacks: false})
275-
});
276-
tests.push({
277-
title: description + ' (callback)',
278-
f: makeFn({useCallbacks: true})
279-
});
273+
if(options.benchmark) {
274+
// only promises
275+
tests.push({
276+
title: description,
277+
f: makeFn({useCallbacks: false})
278+
});
279+
} else {
280+
tests.push({
281+
title: description + ' (promise)',
282+
f: makeFn({useCallbacks: false})
283+
});
284+
tests.push({
285+
title: description + ' (callback)',
286+
f: makeFn({useCallbacks: true})
287+
});
288+
}
280289

281290
function makeFn({useCallbacks}) {
282291
return function(done) {
@@ -340,8 +349,7 @@ function addTest(manifest, test, tests) {
340349
});
341350

342351
const fn = testInfo.fn;
343-
let params = testInfo.params;
344-
params = params.map(function(param) {return param(test);});
352+
const params = testInfo.params.map(param => param(test));
345353
const callback = function(err, result) {
346354
Promise.resolve().then(() => {
347355
if(isNegativeTest(test)) {
@@ -353,6 +361,43 @@ function addTest(manifest, test, tests) {
353361
}
354362
return testInfo.compare(test, result);
355363
}
364+
}).then(() => {
365+
if(options.benchmark) {
366+
// pre-load params to avoid doc loader and parser timing
367+
const benchParams = testInfo.params.map(param => param(test, {
368+
load: true
369+
}));
370+
return Promise.all(benchParams);
371+
}
372+
}).then(values => {
373+
if(options.benchmark) {
374+
return new Promise((resolve, reject) => {
375+
const suite = new benchmark.Suite();
376+
suite.add({
377+
name: test.name,
378+
defer: true,
379+
fn: deferred => {
380+
jsonld[fn].apply(null, values).then(() => {
381+
deferred.resolve();
382+
});
383+
}
384+
});
385+
suite
386+
.on('start', e => {
387+
self.timeout((e.target.maxTime + 2) * 1000);
388+
})
389+
.on('cycle', e => {
390+
console.log(String(e.target));
391+
})
392+
.on('error', err => {
393+
reject(new Error(err));
394+
})
395+
.on('complete', e => {
396+
resolve();
397+
})
398+
.run({async: true});
399+
});
400+
}
356401
}).then(() => {
357402
if(options.earl.report) {
358403
options.earl.report.addAssertion(test, true);
@@ -458,10 +503,15 @@ function readManifestEntry(manifest, entry) {
458503
}
459504

460505
function readTestUrl(property) {
461-
return function(test) {
506+
return function(test, options) {
462507
if(!test[property]) {
463508
return null;
464509
}
510+
if(options && options.load) {
511+
// always load
512+
return joinPath(test.dirname, test[property])
513+
.then(readJson);
514+
}
465515
return test.manifest.baseIri + test[property];
466516
};
467517
}

tests/test-karma.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99
* EARL=filename
1010
* Bail with tests fail:
1111
* BAIL=true
12+
* Benchmark mode:
13+
* Basic:
14+
* JSONLD_BENCHMARK=1
15+
* With options:
16+
* JSONLD_BENCHMARK=key1=value1,key2=value2,...
1217
*
1318
* @author Dave Longley
1419
* @author David I. Lehn
@@ -64,6 +69,17 @@ if(process.env.JSONLD_TESTS) {
6469
entries.push(webidl);
6570
}
6671

72+
let benchmark = null;
73+
if(process.env.JSONLD_BENCHMARK) {
74+
benchmark = {};
75+
if(!(['1', 'true'].includes(process.env.JSONLD_BENCHMARK))) {
76+
process.env.JSONLD_BENCHMARK.split(',').forEach(pair => {
77+
const kv = pair.split('=');
78+
benchmark[kv[0]] = kv[1];
79+
});
80+
}
81+
}
82+
6783
const options = {
6884
nodejs: false,
6985
assert: assert,
@@ -82,6 +98,7 @@ const options = {
8298
},
8399
bailOnError: process.env.BAIL === 'true',
84100
entries: entries,
101+
benchmark: benchmark,
85102
readFile: filename => {
86103
return server.run(filename, function(filename) {
87104
const fs = serverRequire('fs-extra');

tests/test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99
* EARL=filename
1010
* Bail with tests fail:
1111
* BAIL=true
12+
* Benchmark mode:
13+
* Basic:
14+
* JSONLD_BENCHMARK=1
15+
* With options:
16+
* JSONLD_BENCHMARK=key1=value1,key2=value2,...
1217
*
1318
* @author Dave Longley
1419
* @author David I. Lehn
@@ -54,6 +59,17 @@ if(process.env.JSONLD_TESTS) {
5459
//entries.push(path.resolve(_top, 'tests/node-document-loader-tests.js'));
5560
}
5661

62+
let benchmark = null;
63+
if(process.env.JSONLD_BENCHMARK) {
64+
benchmark = {};
65+
if(!(['1', 'true'].includes(process.env.JSONLD_BENCHMARK))) {
66+
process.env.JSONLD_BENCHMARK.split(',').forEach(pair => {
67+
const kv = pair.split('=');
68+
benchmark[kv[0]] = kv[1];
69+
});
70+
}
71+
}
72+
5773
const options = {
5874
nodejs: {
5975
path: path
@@ -67,6 +83,7 @@ const options = {
6783
},
6884
bailOnError: process.env.BAIL === 'true',
6985
entries: entries,
86+
benchmark: benchmark,
7087
readFile: filename => {
7188
return fs.readFile(filename, 'utf8');
7289
},

0 commit comments

Comments
 (0)