mirror of
https://github.com/tauri-apps/tauri.git
synced 2024-12-22 10:11:32 +03:00
b9a9de6b64
* remove bench test lock Signed-off-by: David Lemarier <david@lemarier.ca> * use tauri commands to close the app Signed-off-by: David Lemarier <david@lemarier.ca>
54 lines
1.3 KiB
JavaScript
54 lines
1.3 KiB
JavaScript
// Create web worker
|
|
const THRESHOLD = 10000000
|
|
const worker = new Worker('worker.js')
|
|
/** @type {HTMLButtonElement} */
|
|
const start = document.getElementById('start')
|
|
/** @type {HTMLParagraphElement} */
|
|
const status = document.getElementById('status')
|
|
const results = document.getElementById('results')
|
|
|
|
const ITERATIONS = 1
|
|
|
|
let resolver
|
|
|
|
const onMessage = (message) => {
|
|
// Update the UI
|
|
let prefix = '[Calculating]'
|
|
|
|
if (message.data.status === 'done') {
|
|
// tell tauri that we are done
|
|
window.__TAURI__.invoke('app_completed_successfully');
|
|
}
|
|
|
|
status.innerHTML = `${prefix} Found <code>${message.data.count}</code> prime numbers in <code>${message.data.time}ms</code>`
|
|
|
|
if (message.data.status === 'done') {
|
|
resolver(message.data.time)
|
|
}
|
|
}
|
|
|
|
worker.addEventListener('message', onMessage)
|
|
|
|
const benchmark = () => {
|
|
return new Promise((resolve) => {
|
|
const startTime = Date.now()
|
|
resolver = resolve
|
|
worker.postMessage({ value: THRESHOLD, startTime })
|
|
})
|
|
}
|
|
|
|
const calculate = async () => {
|
|
let total = 0
|
|
|
|
for (let i = 0; i < ITERATIONS; i++) {
|
|
const result = await benchmark()
|
|
total += result
|
|
}
|
|
|
|
const average = total / ITERATIONS
|
|
|
|
results.innerText = `Average time: ${average}ms`
|
|
}
|
|
|
|
window.addEventListener('DOMContentLoaded', calculate)
|