mt-polygon-simplification/benchmarking/src/index.js

120 lines
3.3 KiB
JavaScript
Raw Normal View History

2019-07-14 20:37:26 +02:00
import React, { Component } from 'react'
import ReactDOM from 'react-dom'
import { data } from '../../data/large.js'
import { Form } from './components/form.js'
import { Chart } from './components/chart.js'
import { Progress } from './components/progress.js'
import { BenchmarkSuite } from './benchmarks/BenchmarkSuite.js'
import { generateCases } from './benchmarkCases/index.js'
import { IterationsBenchmark } from './benchmarks/IterationBenchmark.js'
import { BenchmarkJSBenchmark } from './benchmarks/BenchmarkJSBenchmark.js'
import { OpsPerTimeBenchmark } from './benchmarks/OpsPerTimeBenchmark.js'
2019-07-19 15:15:05 +02:00
import simplifyJS from '../../lib/simplify-js-alternative/simplify.js'
import { simplifyWasm } from '../../lib/simplify-wasm/index.js'
export async function simplifyMapper(labels, highQual) {
let items = labels.map(tol =>
simplifyWasm(data, tol, highQual).then(arr => arr.length)
)
return await Promise.all(items)
}
2019-07-14 20:37:26 +02:00
class App extends Component {
constructor(props) {
super(props)
this.handleformChange = this.handleformChange.bind(this)
this.handleFormSubmit = this.handleFormSubmit.bind(this)
this.state = {
stats: {},
formState: { tolRange: [] },
totalCount: 0,
2019-07-19 15:15:05 +02:00
finishedCount: 0,
numNodes: []
2019-07-14 20:37:26 +02:00
}
this.suite = new BenchmarkSuite(new IterationsBenchmark(10))
this.suite.onCycle = (i, count, stats) => {
this.setState({
finishedCount: i,
totalCount: count,
stats: stats
})
}
}
async handleFormSubmit() {
this.runBenchmarks()
}
2019-07-19 15:15:05 +02:00
async handleformChange(formState) {
2019-07-14 20:37:26 +02:00
const { tolStart, tolEnd, tolStep } = formState
const tolRange = []
for (let i = tolStart; i < tolEnd; i += tolStep) {
tolRange.push(i)
2019-07-19 15:15:05 +02:00
if (tolStep === 0) break
if (tolRange.length > 200) break
2019-07-14 20:37:26 +02:00
}
2019-07-19 15:15:05 +02:00
this.setState({
formState: { ...formState, tolRange },
numNodes: await simplifyMapper(tolRange, formState.highQual)
})
2019-07-14 20:37:26 +02:00
}
async runBenchmarks() {
this.suite.reset()
let benchtype
console.log(this.state.formState.benchMethod)
switch (this.state.formState.benchMethod) {
case 'benchmarkJS':
benchtype = new BenchmarkJSBenchmark()
break
case 'iteration':
benchtype = new IterationsBenchmark(this.state.formState.iterations)
break
case 'opsPerTime':
benchtype = new OpsPerTimeBenchmark(this.state.formState.timeToRun)
break
default:
console.warn('benchmark type "' + this.state.benchtype + '" not known')
benchtype = new IterationsBenchmark(10)
}
this.suite.setBenchmarkType(benchtype)
this.suite.setCases(generateCases(data, this.state.formState))
updateStatus('Running')
await this.suite.run()
updateStatus('Finished')
}
render() {
return (
2019-07-19 15:15:05 +02:00
<>
2019-07-14 20:37:26 +02:00
<Form
onFormChange={this.handleformChange}
onFormSubmit={this.handleFormSubmit}
/>
<Progress
totalCount={this.state.totalCount}
finishedCount={this.state.finishedCount}
/>
<Chart
labels={this.state.formState.tolRange}
highQual={this.state.formState.highQual}
data={this.state.stats}
2019-07-19 15:15:05 +02:00
numNodes={this.state.numNodes}
2019-07-14 20:37:26 +02:00
/>
2019-07-19 15:15:05 +02:00
</>
2019-07-14 20:37:26 +02:00
)
}
}
ReactDOM.render(<App />, document.getElementsByTagName('main')[0])
const updateStatus = status => {
console.log(status)
}