}
}
- export async function test (api, size, runs, isDebug) {
+ async function sequenceDeriveSignVerify (api, blockHash, privateKey) {
+ let start = 0, end = 0, publicKey = null, signature = null, isValid = false
+ if (api === 'nano25519 (async)') {
+ start = performance.now()
+ publicKey = await derive(privateKey, api)
+ signature = await sign(blockHash, privateKey, publicKey, api)
+ isValid = await verify(signature, blockHash, publicKey, api)
+ end = performance.now()
+ } else {
+ start = performance.now()
+ publicKey = derive(privateKey, api)
+ signature = sign(blockHash, privateKey, publicKey, api)
+ isValid = verify(signature, blockHash, publicKey, api)
+ end = performance.now()
+ }
+ if (!isValid) {
+ throw new Error(`invalid result\nblock hash: ${blockHash}\nsignature: ${signature}\npublic key: ${publicKey}`)
+ }
+ if (!start || !end || start > end) {
+ throw new Error(`invalid timing\nstart: ${start}\nend: ${end}`)
+ }
+ return end - start
+ }
+
+ export async function test (api, size, runs) {
if (typeof size !== 'number' || size < 1) {
size = 1
}
// run tests
console.log(`%c${api}`, 'color:green', `Calculate truncated harmonic mean of the truncated arithmetic rate of signing random block hashes across ${runs} runs of ${size} samples.`)
const rates = []
- let start = 0, end = 0, publicKey = null, signature = null, isValid = false
for (let i = 0; i < runs; i++) {
await new Promise(r => setTimeout(r))
const times = []
const blockHash = random()
const privateKey = blockHash
try {
- start = performance.now()
- publicKey = await derive(privateKey, api)
- signature = await sign(blockHash, privateKey, publicKey, api)
- isValid = await verify(signature, blockHash, publicKey, api)
- end = performance.now()
+ const duration = await sequenceDeriveSignVerify(api, blockHash, privateKey)
+ times.push(duration)
} catch (err) {
document.getElementById('output').innerHTML += `Error: ${err.message}<br/>`
console.error(err)
return
}
- if (!isValid) {
- document.getElementById('status').innerHTML = `ERROR: invalid result\nblock hash: ${blockHash}\nsignature: ${signature}\npublic key: ${publicKey}`
- return
- }
- if (!start || !end || start > end) {
- document.getElementById('status').innerHTML = `ERROR: invalid timing\nstart: ${start}\nend: ${end}`
- return
- }
- times.push(end - start)
}
const avg = average(times, api)
const result = Object.values(avg)[0]
const { truncatedRate } = result
rates.push(truncatedRate)
- if (isDebug) document.getElementById('output').innerHTML += `Benchmark ${i + 1} score: ${truncatedRate} ops<br/>`
- if (isDebug) document.getElementById('summary').innerHTML += `${JSON.stringify(average(times, api), null, '\t')}<br/>`
}
const results = Object.values(average(rates, api))[0]
const { truncatedHarmonic } = results
const api = document.getElementById('api')
const size = document.getElementById('size')
const runs = document.getElementById('runs')
- const isDebug = document.getElementById('isDebug')
- test(api.value, +size.value, +runs.value, isDebug.checked)
+ test(api.value, +size.value, +runs.value)
.then(() => {
event.target.disabled = false
})
<input id="size" type="number" value="100" min="1" autofocus />
<label for="runs">Test Runs</label>
<input id="runs" type="number" value="100" min="1" autofocus />
- <span>
- <label for="isDebug">Debug?</label>
- <input id="isDebug" type="checkbox" />
- </span>
<button id="btnStartTest" disabled>Go</button>
<hr />
<h3 id="status">LOADING</h3>