From e2b18370f1ec5b973939a316605cdb6d85fec70d Mon Sep 17 00:00:00 2001 From: Max Richter Date: Thu, 2 May 2024 00:00:18 +0200 Subject: [PATCH] feat: improve benchmarking code --- app/src/lib/helpers/index.ts | 3 +- .../lib/settings/panels/BenchmarkPanel.svelte | 43 ++++++++++++++++--- 2 files changed, 40 insertions(+), 6 deletions(-) diff --git a/app/src/lib/helpers/index.ts b/app/src/lib/helpers/index.ts index f48d7a1..887d751 100644 --- a/app/src/lib/helpers/index.ts +++ b/app/src/lib/helpers/index.ts @@ -65,7 +65,7 @@ export function createNodePath({ export const debounce = (fn: Function, ms = 300) => { let timeoutId: ReturnType; - return function (this: any, ...args: any[]) { + return function(this: any, ...args: any[]) { clearTimeout(timeoutId); timeoutId = setTimeout(() => fn.apply(this, args), ms); }; @@ -126,3 +126,4 @@ export function humanizeNumber(number: number): string { const rounded = Math.round(number / base * 10) / 10; return rounded + suffixes[baseIndex]; } + diff --git a/app/src/lib/settings/panels/BenchmarkPanel.svelte b/app/src/lib/settings/panels/BenchmarkPanel.svelte index 14e4a20..0e1ef14 100644 --- a/app/src/lib/settings/panels/BenchmarkPanel.svelte +++ b/app/src/lib/settings/panels/BenchmarkPanel.svelte @@ -3,6 +3,14 @@ import { Integer } from "@nodes/ui"; import { writable } from "svelte/store"; + function calculateStandardDeviation(array: number[]) { + const n = array.length; + const mean = array.reduce((a, b) => a + b) / n; + return Math.sqrt( + array.map((x) => Math.pow(x - mean, 2)).reduce((a, b) => a + b) / n, + ); + } + export let run: () => Promise; let isRunning = false; @@ -11,7 +19,7 @@ let warmUp = writable(0); let warmUpAmount = 10; let state = ""; - let result = ""; + let result: { stdev: number; avg: number } | undefined; const copyContent = async (text: string) => { try { @@ -21,6 +29,11 @@ } }; + function handleCopy(ev: MouseEvent) { + const text = (ev.target as HTMLTextAreaElement).value; + copyContent(text); + } + async function benchmark() { if (isRunning) return; isRunning = true; @@ -44,10 +57,13 @@ await run(); samples = i; const b = performance.now(); + await new Promise((r) => setTimeout(r, 50)); results.push(b - a); - console.log(b - a); } - result = results.join(" "); + result = { + stdev: calculateStandardDeviation(results), + avg: results.reduce((a, b) => a + b) / results.length, + }; } @@ -65,9 +81,16 @@ > {#if result} - + click to copy + + + +
-
{/if} @@ -85,4 +108,14 @@ flex-direction: column; gap: 1em; } + textarea { + width: 100%; + height: 1em; + font-size: 1em; + padding: 0.5em; + border: solid thin var(--outline); + background: var(--layer-2); + box-sizing: border-box; + height: 2.5em; + }