Skip to content

Commit b64f260

Browse files
committed
Add benchmark tests and its runner.
1 parent c539363 commit b64f260

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

__benches__/index.test.js

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/* index.test.js - Benchmark tests for React store context hooks functionality.
2+
* Copyright (c) 2019 - 2021 Richard Huang <rickypc@users.noreply.github.com>
3+
*
4+
* This Source Code Form is subject to the terms of the Mozilla Public
5+
* License, v. 2.0. If a copy of the MPL was not distributed with this
6+
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
7+
8+
import proxyquire from 'proxyquire';
9+
import run from './runner.js';
10+
11+
const Component = () => null;
12+
const context = {
13+
get() {},
14+
remove() {},
15+
set() {},
16+
sets() {},
17+
};
18+
19+
const {
20+
isEmpty,
21+
useStore,
22+
useStores,
23+
withStore,
24+
} = proxyquire('../src/index.js', {
25+
react: {
26+
createContext: () => context,
27+
useCallback: (cb) => cb,
28+
useContext: () => context,
29+
useState() {},
30+
},
31+
});
32+
33+
(async () => {
34+
await run('isEmpty', () => isEmpty(false));
35+
await run('useStore', () => useStore('key', 'default'));
36+
await run('useStores', () => useStores({ key1: 'value1' }));
37+
await run('withStore', () => withStore(Component));
38+
})();

__benches__/runner.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/* runner.js - Benchmark test runner for React store context hooks.
2+
* Copyright (c) 2019 - 2021 Richard Huang <rickypc@users.noreply.github.com>
3+
*
4+
* This Source Code Form is subject to the terms of the Mozilla Public
5+
* License, v. 2.0. If a copy of the MPL was not distributed with this
6+
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
7+
8+
import Benchmark from 'benchmark';
9+
10+
Benchmark.options.maxTime = 0;
11+
Benchmark.options.minSamples = 5;
12+
13+
export default (name, fn) => new Promise((resolve) => ((new Benchmark(name,
14+
{ defer: true, fn: async (d) => d.resolve(await fn()) }))
15+
.on('complete', (evt) => {
16+
process.stdout.write(`${evt.target}\n`);
17+
resolve();
18+
})
19+
.on('error', (evt) => {
20+
throw evt.target.error;
21+
})
22+
.run({ async: true })));

0 commit comments

Comments
 (0)