Skip to content

Commit 261a3a6

Browse files
committed
benchmark: run benchmarks in worker
1 parent da36298 commit 261a3a6

File tree

7 files changed

+79
-24
lines changed

7 files changed

+79
-24
lines changed

benchmark/worker.js

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
onmessage = function(event) {
2+
require(['./browser', `./${event.data.benchmark}-benchmark.js`], function(exec, benchmark) {
3+
exec.default(benchmark, event.data.method, event.data.time).then(function(result) {
4+
event.data.result = result;
5+
postMessage(event.data);
6+
});
7+
});
8+
};

demos/components/Benchmark.js

+42-17
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,63 @@
11
import React, {Component} from 'react';
2-
import runBenchmark from '../../benchmark/browser';
2+
import Loading from 'react-loading';
33

44
export default class Benchmarks extends Component {
55
constructor(props) {
66
super(props);
7-
this.state = {};
7+
this.state = {
8+
asmTime: '-',
9+
wasmTime: '-'
10+
};
811
}
9-
async onRun() {
10-
const asmTime = await runBenchmark(this.props.benchmark, 'asm', 5);
11-
this.setState({
12-
asmTime
13-
});
1412

15-
await wait();
13+
componentDidMount() {
14+
const Worker = require('worker-loader!../../benchmark/worker');
15+
this.worker = new Worker;
16+
this.worker.onmessage = event => {
17+
this.setState({
18+
[event.data.method === 'asm' ? 'asmTime' : 'wasmTime']: event.data.result
19+
});
20+
};
21+
}
1622

17-
const wasmTime = await runBenchmark(this.props.benchmark, 'wasm', 5);
23+
async onRun() {
24+
this.worker.postMessage({
25+
benchmark: this.props.benchmark,
26+
method: 'asm',
27+
time: 5
28+
});
29+
this.worker.postMessage({
30+
benchmark: this.props.benchmark,
31+
method: 'wasm',
32+
time: 5
33+
});
1834
this.setState({
19-
wasmTime
35+
asmTime: 'running',
36+
wasmTime: 'running'
2037
});
2138
}
39+
2240
render() {
41+
const {asmTime, wasmTime} = this.state;
42+
const disabled = asmTime === 'running' || wasmTime === 'running';
2343
return (
2444
<div>
2545
<h3>{this.props.name}</h3>
26-
asm: {this.state.asmTime} &nbsp;
27-
wasm: {this.state.wasmTime} &nbsp;
28-
<input type="button" value="Run" onClick={this.onRun.bind(this)} />
46+
<div style={{lineHeight: '32px', display: 'flex'}}>
47+
asm: {asmTime === 'running' ? <MySpinner /> : asmTime} &nbsp;
48+
wasm: {wasmTime === 'running' ? <MySpinner /> : wasmTime } &nbsp;
49+
<input type="button" className="btn btn-info" value="Run" onClick={this.onRun.bind(this)} disabled={disabled}/>
50+
</div>
2951
</div>
3052
)
3153
}
3254
}
3355

34-
function wait() {
35-
return new Promise(resolve => {
36-
setTimeout(resolve, 0);
37-
});
56+
57+
function MySpinner() {
58+
return (
59+
<div style={{display: 'inline-block', height: 32, width: 32}}>
60+
<Loading type="bubbles" width={32} height={32} color="black"/>
61+
</div>
62+
)
3863
}

demos/containers/App.js

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import Navigation from '../components/Navigation';
99
import {Route, HashRouter as Router} from 'react-router-dom';
1010
import Benchmarks from './Benchmarks';
1111

12+
1213
class App extends Component {
1314
componentWillMount() {
1415
const onResize = () => {

demos/containers/Benchmarks.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
import React from 'react';
22
import Benchmark from '../components/Benchmark';
3-
import irisCVBenchmark from '../../benchmark/iris/cross-validation-benchmark';
4-
import irisGSBenchmark from '../../benchmark/iris/grid-search-benchmark';
53

64
export default function() {
75
return (
86
<div>
9-
<Benchmark benchmark={irisCVBenchmark} name="Iris cross-validation"/>
10-
<Benchmark benchmark={irisGSBenchmark} name="Iris grid-search"/>
7+
<Benchmark benchmark="iris/cross-validation" name="Iris cross-validation"/>
8+
<Benchmark benchmark="iris/grid-search" name="Iris grid-search"/>
119
</div>
1210
);
1311
};

package.json

+5-2
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@
3838
"url": "https://github.com/mljs/libsvm/issues"
3939
},
4040
"homepage": "https://github.com/mljs/libsvm#readme",
41-
"dependencies": {},
41+
"dependencies": {
42+
"react-loading": "^0.1.2"
43+
},
4244
"devDependencies": {
4345
"babel-core": "^6.23.1",
4446
"babel-eslint": "^7.2.1",
@@ -90,6 +92,7 @@
9092
"url-loader": "^0.5.8",
9193
"webpack": "^2.2.1",
9294
"webpack-dev-server": "^2.4.1",
93-
"webpack-visualizer-plugin": "^0.1.11"
95+
"webpack-visualizer-plugin": "^0.1.11",
96+
"worker-loader": "^0.8.0"
9497
}
9598
}

webpack.common.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict';
22
const path = require('path');
3+
const webpack = require('webpack');
34

45
module.exports = {
56
entry: [
@@ -58,7 +59,16 @@ module.exports = {
5859
]
5960
},
6061
plugins: [
61-
62+
new webpack.LoaderOptionsPlugin({
63+
options: {
64+
worker: {
65+
output: {
66+
filename: "hash.worker.js",
67+
chunkFilename: "[id].hash.worker.js"
68+
}
69+
}
70+
}
71+
})
6272
],
6373
node: {
6474
fs: 'empty'

yarn.lock

+10
Original file line numberDiff line numberDiff line change
@@ -3980,6 +3980,10 @@ react-hot-loader@beta:
39803980
redbox-react "^1.2.5"
39813981
source-map "^0.4.4"
39823982

3983+
react-loading@^0.1.2:
3984+
version "0.1.2"
3985+
resolved "https://registry.yarnpkg.com/react-loading/-/react-loading-0.1.2.tgz#4da95b3d4b55904fd8294c1eb40f8512a6ca04f6"
3986+
39833987
react-proxy@^3.0.0-alpha.0:
39843988
version "3.0.0-alpha.1"
39853989
resolved "https://registry.yarnpkg.com/react-proxy/-/react-proxy-3.0.0-alpha.1.tgz#4400426bcfa80caa6724c7755695315209fa4b07"
@@ -5032,6 +5036,12 @@ wordwrap@~1.0.0:
50325036
version "1.0.0"
50335037
resolved "https://registry.yarnpkg.com/wordwrap/-/wordwrap-1.0.0.tgz#27584810891456a4171c8d0226441ade90cbcaeb"
50345038

5039+
worker-loader@^0.8.0:
5040+
version "0.8.0"
5041+
resolved "https://registry.yarnpkg.com/worker-loader/-/worker-loader-0.8.0.tgz#13582960dcd7d700dc829d3fd252a7561696167e"
5042+
dependencies:
5043+
loader-utils "^1.0.2"
5044+
50355045
wrap-ansi@^2.0.0:
50365046
version "2.1.0"
50375047
resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-2.1.0.tgz#d8fc3d284dd05794fe84973caecdd1cf824fdd85"

0 commit comments

Comments
 (0)