Skip to content

Commit 42f965d

Browse files
committed
Support benchmarks in Karma tests.
1 parent f4853b3 commit 42f965d

File tree

5 files changed

+27
-11
lines changed

5 files changed

+27
-11
lines changed

CHANGELOG.md

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

3+
- Support benchmarks in Karma tests.
4+
35
## 5.2.0 - 2021-04-07
46

57
### Changed

karma.conf.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,10 @@ module.exports = function(config) {
120120
}
121121
}
122122
}
123+
],
124+
noParse: [
125+
// avoid munging internal benchmark script magic
126+
/benchmark/
123127
]
124128
},
125129
node: {

tests/test-common.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
*/
44
/* eslint-disable indent */
55
const EarlReport = require('./earl-report');
6-
const benchmark = require('benchmark');
76
const join = require('join-path-js');
87
const rdfCanonize = require('rdf-canonize');
98
const {prependBase} = require('../lib/url');
@@ -13,6 +12,7 @@ module.exports = function(options) {
1312
'use strict';
1413

1514
const assert = options.assert;
15+
const benchmark = options.benchmark;
1616
const jsonld = options.jsonld;
1717

1818
const manifest = options.manifest || {
@@ -573,8 +573,8 @@ function addTest(manifest, test, tests) {
573573
throw Error('Unknown test type: ' + test.type);
574574
}
575575

576-
let benchResult = null
577-
if(options.benchmark) {
576+
let benchResult = null;
577+
if(options.benchmarkOptions) {
578578
const result = await runBenchmark({
579579
test,
580580
fn,

tests/test-karma.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@ const server = require('karma-server-side');
3535
const webidl = require('./test-webidl');
3636
const join = require('join-path-js');
3737

38+
// special benchmark setup
39+
const _ = require('lodash');
40+
const _process = require('process');
41+
const benchmark = require('benchmark');
42+
const Benchmark = benchmark.runInContext({_, _process});
43+
window.Benchmark = Benchmark;
44+
3845
const entries = [];
3946

4047
if(process.env.JSONLD_TESTS) {
@@ -75,20 +82,21 @@ if(process.env.JSONLD_TESTS) {
7582
entries.push(webidl);
7683
}
7784

78-
let benchmark = null;
85+
let benchmarkOptions = null;
7986
if(process.env.JSONLD_BENCHMARK) {
80-
benchmark = {};
87+
benchmarkOptions = {};
8188
if(!(['1', 'true'].includes(process.env.JSONLD_BENCHMARK))) {
8289
process.env.JSONLD_BENCHMARK.split(',').forEach(pair => {
8390
const kv = pair.split('=');
84-
benchmark[kv[0]] = kv[1];
91+
benchmarkOptions[kv[0]] = kv[1];
8592
});
8693
}
8794
}
8895

8996
const options = {
9097
nodejs: false,
9198
assert,
99+
benchmark,
92100
jsonld,
93101
/* eslint-disable-next-line no-unused-vars */
94102
exit: code => {
@@ -103,7 +111,7 @@ const options = {
103111
verboseSkip: process.env.VERBOSE_SKIP === 'true',
104112
bailOnError: process.env.BAIL === 'true',
105113
entries,
106-
benchmark,
114+
benchmarkOptions,
107115
readFile: filename => {
108116
return server.run(filename, function(filename) {
109117
const fs = serverRequire('fs-extra');

tests/test.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
* Copyright (c) 2011-2019 Digital Bazaar, Inc. All rights reserved.
2626
*/
2727
const assert = require('chai').assert;
28+
const benchmark = require('benchmark');
2829
const common = require('./test-common');
2930
const fs = require('fs-extra');
3031
const jsonld = require('..');
@@ -84,13 +85,13 @@ if(process.env.JSONLD_TESTS) {
8485
entries.push(path.resolve(_top, 'tests/node-document-loader-tests.js'));
8586
}
8687

87-
let benchmark = null;
88+
let benchmarkOptions = null;
8889
if(process.env.JSONLD_BENCHMARK) {
89-
benchmark = {};
90+
benchmarkOptions = {};
9091
if(!(['1', 'true'].includes(process.env.JSONLD_BENCHMARK))) {
9192
process.env.JSONLD_BENCHMARK.split(',').forEach(pair => {
9293
const kv = pair.split('=');
93-
benchmark[kv[0]] = kv[1];
94+
benchmarkOptions[kv[0]] = kv[1];
9495
});
9596
}
9697
}
@@ -100,6 +101,7 @@ const options = {
100101
path
101102
},
102103
assert,
104+
benchmark,
103105
jsonld,
104106
exit: code => process.exit(code),
105107
earl: {
@@ -110,7 +112,7 @@ const options = {
110112
verboseSkip: process.env.VERBOSE_SKIP === 'true',
111113
bailOnError: process.env.BAIL === 'true',
112114
entries,
113-
benchmark,
115+
benchmarkOptions,
114116
readFile: filename => {
115117
return fs.readFile(filename, 'utf8');
116118
},

0 commit comments

Comments
 (0)