Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create 'performance.js' to avoid code duplication
- Loading branch information
1 parent
17cb05c
commit e95c90e
Showing
4 changed files
with
39 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,16 @@ | ||
|
||
import * as mod from '../mod'; | ||
import { measure } from './performance'; | ||
|
||
onmessage = e => { | ||
|
||
performance.mark(`${e.data.method}-start`); | ||
const value = mod[e.data.method](...e.data.args); | ||
performance.mark(`${e.data.method}-end`); | ||
|
||
performance.measure(`${e.data.method}-measure`, `${e.data.method}-start`, `${e.data.method}-end`); | ||
|
||
const measures = performance.getEntriesByName(`${e.data.method}-measure`); | ||
const measure = measures[measures.length - 1]; | ||
const performance = measure(e.data.method, () => { | ||
return mod[e.data.method](...e.data.args) | ||
}) | ||
|
||
postMessage({ | ||
data: { worker: 'js', ...e.data }, | ||
value, | ||
measures: measures.map(e => e.toJSON()), | ||
measure: measure.toJSON(), | ||
}); | ||
data: { ...e.data, worker: 'js' }, | ||
performance | ||
}) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
|
||
export const measure = (name, fn) => { | ||
|
||
performance.mark(`${name}-start`); | ||
const fn_return = fn(); | ||
performance.mark(`${name}-end`); | ||
|
||
performance.measure(`${name}-measure`, `${name}-start`, `${name}-end`); | ||
|
||
const measures = performance.getEntriesByName(`${name}-measure`); | ||
const measure = measures[measures.length - 1]; | ||
|
||
return { | ||
fn_return, | ||
entries: measures.map(e => e.toJSON()), | ||
measure: measure.toJSON(), | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,20 @@ | ||
|
||
import wasm from '../../../pkg/wa_vs_js_benchmark_bg.wasm'; | ||
import * as _exp from '../../../pkg/wa_vs_js_benchmark'; | ||
|
||
var importObject = { | ||
'./wa_vs_js_benchmark': _exp | ||
} | ||
import { measure } from './performance'; | ||
|
||
onmessage = e => { | ||
|
||
WebAssembly.instantiateStreaming(fetch(wasm), importObject) | ||
.then(results => { | ||
|
||
performance.mark(`${e.data.method}-start`); | ||
const value = results.instance.exports[e.data.method](...e.data.args); | ||
performance.mark(`${e.data.method}-end`); | ||
WebAssembly.instantiateStreaming(fetch(wasm), { './wa_vs_js_benchmark': _exp }).then(results => { | ||
|
||
performance.measure(`${e.data.method}-measure`, `${e.data.method}-start`, `${e.data.method}-end`); | ||
const performance = measure(e.data.method, () => { | ||
return results.instance.exports[e.data.method](...e.data.args) | ||
}) | ||
|
||
const measures = performance.getEntriesByName(`${e.data.method}-measure`); | ||
const measure = measures[measures.length -1]; | ||
postMessage({ | ||
data: { ...e.data, worker: 'rs' }, | ||
performance | ||
}) | ||
|
||
postMessage({ | ||
data: { worker: 'rs', ...e.data }, | ||
value, | ||
measures: measures.map(e => e.toJSON()), | ||
measure: measure.toJSON(), | ||
}); | ||
}); | ||
}); | ||
} |