Skip to content

Commit 8fd007d

Browse files
committed
Add benchmark test
1 parent adbeac7 commit 8fd007d

5 files changed

Lines changed: 70 additions & 5 deletions

File tree

package-lock.json

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"devDependencies": {
2020
"babel-plugin-syntax-async-functions": "^6.5.0",
2121
"babel-preset-env": "^1.5.2",
22+
"benchmark": "^2.1.4",
2223
"browser-sync": "^2.26.3",
2324
"browserify": "^16.2.3",
2425
"chai": "^4.2.0",
@@ -42,7 +43,7 @@
4243
"vinyl-source-stream": "^2.0.0"
4344
},
4445
"scripts": {
45-
"test": "mocha ./test",
46+
"test": "mocha ./test/index",
4647
"setup": "npm i -g gulp-cli",
4748
"build-docs": "./node_modules/.bin/jsdoc -c jsdoc.json src -r -d doc --debug && gulp injectCSS",
4849
"make": "gulp build && gulp beautify && gulp minify"

src/core/gpu.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ class GPU extends GPUCore {
3131
this._canvas = settings.canvas || null;
3232
this._webGl = settings.webGl || null;
3333

34-
if (!mode || mode === 'webgl-validator') {
34+
if (mode === 'cpu') {
35+
detectedMode = 'cpu';
36+
} else if (mode === 'webgl' || mode === 'webgl-validator') {
3537
const context = createNodeContext(512, 512);
3638
const canvas = createCanvas(512, 512);
3739
detectedMode = mode || 'webgl';

test/benchmark.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
const GPU = require('../src/index.js');
2+
const Benchmark = require('benchmark');
3+
4+
const suite = new Benchmark.Suite;
5+
6+
7+
const gpuRunner = new GPU({ mode: 'webgl' });
8+
const cpuRunner = new GPU({ mode: 'cpu' });
9+
10+
const size = 200;
11+
12+
const myGPUFunc = gpuRunner
13+
.createKernel(function compute() {
14+
const i = this.thread.x;
15+
const j = 0.89;
16+
return i + j;
17+
})
18+
// .setOutputToTexture(true)
19+
.setOutput([size, size, size]);
20+
21+
const myCPUFunc = cpuRunner
22+
.createKernel(function compute() {
23+
const i = this.thread.x;
24+
const j = 0.89;
25+
return i + j;
26+
})
27+
.setOutput([size, size, size]);
28+
29+
30+
31+
32+
// add tests
33+
suite.add('cpu', function() {
34+
myGPUFunc();
35+
})
36+
.add('gpu', function() {
37+
myCPUFunc();
38+
})
39+
// add listeners
40+
.on('cycle', function(event) {
41+
console.log(String(event.target));
42+
})
43+
.on('complete', function() {
44+
console.log('Fastest is ' + this.filter('fastest').map('name'));
45+
})
46+
// run async
47+
.run();

test/index.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
1-
const path = require('path');
21
const { expect } = require('chai');
32

43
const GPU = require('../src/index.js');
54

65

76
describe('Test Node GPU', () => {
87
it('should find gpu', () => {
9-
const gpu = new GPU();
8+
const gpu = new GPU({ mode: 'webgl' });
109

1110
const myFunc = gpu.createKernel(function compute() {
1211
const i = this.thread.x;
1312
const j = 0.89;
1413
return i + j;
15-
}).setOutput([100]);
14+
}).setOutput([1000, 1000]);
1615

1716
console.log(myFunc());
1817

0 commit comments

Comments
 (0)