-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbenchmark-worker.js
128 lines (120 loc) · 3.5 KB
/
benchmark-worker.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import {WebIO} from 'https://cdn.skypack.dev/@gltf-transform/core'
import {wasm} from '../build/resample_wasm.esm.js';
import {wasm as simdWasm} from '../build/resample_simd.esm.js';
import {makeWrapper} from '../resample-wrapper.js';
import {resampleFast} from './js/resample-fast.js';
import {resample as resampleOpt} from "./js/resample-opt.js";
import {resample} from "./js/resample-orig.js";
async function benchmarkWasm(simd, memorySize, warmup, run) {
let buf = new Uint8Array(await fetch('./demo.glb')
.then(e=>e.arrayBuffer()));
let io = new WebIO();
let doc = await io.readBinary(buf);
const mod = simd ? simdWasm : wasm;
const {instance} = await WebAssembly.instantiate(mod);
if (memorySize) {
instance.exports.memory.grow(memorySize);
}
const w = makeWrapper(instance);
const optionHolder1 = {
w: w,
tolerance: 1.1920928955078125e-07,
weights: false,
stats: {
beforeLength: 0,
beforeFrames: 0,
timeEscaped: 0,
afterLength: 0,
afterFrames: 0,
}
};
// warmup
for (let i = 0; i < warmup; i++) {
const doc1 = doc.clone();
await doc1.transform(resampleFast(optionHolder1));
}
const optionHolder = {
w: w,
tolerance: 1.1920928955078125e-07,
weights: false,
stats: {
beforeLength: 0,
beforeFrames: 0,
timeEscaped: 0,
afterLength: 0,
afterFrames: 0,
}
};
// run
for (let i = 0; i < run; i++) {
const doc1 = doc.clone();
await doc1.transform(resampleFast(optionHolder));
}
return optionHolder.stats;
}
async function benchmarkJs(optimize, warmup, run) {
let buf = new Uint8Array(await fetch('./demo.glb')
.then(e=>e.arrayBuffer()));
let io = new WebIO();
let doc = await io.readBinary(buf);
const resampleFn =
optimize ? resampleOpt : resample;
const optionHolder1 = {
tolerance: 1.1920928955078125e-07,
stats: {
beforeLength: 0,
beforeFrames: 0,
timeEscaped: 0,
afterLength: 0,
afterFrames: 0,
}
};
// warmup
for (let i = 0; i < warmup; i++) {
const doc1 = doc.clone();
await doc1.transform(resampleFn(optionHolder1));
}
const optionHolder = {
tolerance: 1.1920928955078125e-07,
stats: {
beforeLength: 0,
beforeFrames: 0,
timeEscaped: 0,
afterLength: 0,
afterFrames: 0,
}
};
// run
for (let i = 0; i < run; i++) {
const doc1 = doc.clone();
await doc1.transform(resampleFn(optionHolder));
}
return optionHolder.stats;
}
async function benchmark(type, memorySize, warmup, run) {
let stats;
if (type === 'wasm' || type === 'simd') {
stats = await benchmarkWasm(type === 'simd', memorySize, warmup, run);
}
if (type === 'js' || type === 'jsOpt') {
stats = await benchmarkJs(type === 'jsOpt', warmup, run);
}
return stats;
}
onmessage = async (ev) => {
let {type, memorySize, warmup, run} = ev.data;
benchmark(type, memorySize, warmup, run).then(stats => {
if (stats) {
postMessage(stats);
} else {
postMessage({
err: 'unknown',
});
}
}, err => {
console.error(err);
postMessage({
err: '' + err,
});
});
};