Skip to content

armgit / BUI

Public Imported from GitHub
0

Blender's GPU Accelerated UI, Decoupled from it's original codebase. Supports Rust, Python, JS, React, C & C++. Twice as good as GPUI

svelte bench

Preetham Pemmasani4f05b7e2807d
diff --git a/bindings/node/bench/FirstFrame.svelte b/bindings/node/bench/FirstFrame.svelte
new file mode 100644
index 0000000..a38ad3f
--- /dev/null
+++ b/bindings/node/bench/FirstFrame.svelte
@@ -0,0 +1,12 @@
+<!-- bench/first-frame.js's React app, in Svelte 5 (bench/startup.js --svelte). -->
+<script>
+ let count = $state(0)
+</script>
+
+<main class="p-6 flex flex-col gap-3 bg-[#2b2b2b] text-[#e6e6e6]">
+ <h1 class="text-xl font-bold">Hello</h1>
+ <div class="flex items-center gap-3">
+ <span>Clicks: {count}</span>
+ <button onclick={() => count++}>Add one</button>
+ </div>
+</main>
diff --git a/bindings/node/bench/Grid.svelte b/bindings/node/bench/Grid.svelte
new file mode 100644
index 0000000..3a35b8c
--- /dev/null
+++ b/bindings/node/bench/Grid.svelte
@@ -0,0 +1,13 @@
+<!-- bench/cells.js --svelte: the grid of the React benchmark in Svelte 5. Cell 0
+ follows `t` (a rune), or BUI signals as stores (`--signals`). -->
+<script>
+ let { cells, cellStyle, label, gridStyle, expose, style0 = null, label0 = null } = $props()
+ let t = $state(0)
+ expose((v) => { t = v })
+ const rest = Array.from({ length: cells - 1 }, (_, i) => i + 1)
+</script>
+
+<div style={gridStyle}>
+ {#if style0}<div style={$style0}>{$label0}</div>{:else}<div style={cellStyle(0, t)}>{label(0, t)}</div>{/if}
+ {#each rest as i}<div style={cellStyle(i, 0)}>{label(i, 0)}</div>{/each}
+</div>
diff --git a/bindings/node/bench/cells.js b/bindings/node/bench/cells.js
index f08e485..32693e5 100644
--- a/bindings/node/bench/cells.js
+++ b/bindings/node/bench/cells.js
@@ -1,7 +1,9 @@
// The Rust benchmark's "one" mode from JavaScript: a 1000x800 window with a
// grid of N cells; every frame (16 ms) cell 0 changes colour and label.
-// node bench/cells.js [cells=3000] [--react [--signals]] [--gpu] [--seconds=5]
+// node bench/cells.js [cells=3000] [--react | --svelte] [--signals] [--gpu] [--seconds=5]
// --signals: cell 0's style and label are BUI signals; the grid renders once.
+// --svelte: bench/Grid.svelte (run with `node --import @blender-ui/svelte/register`,
+// `bun --preload @blender-ui/svelte/register`, or `bui run`); cell 0 follows a rune.
// --dev: React's development build. Runs on Node, Bun (`bun bench/cells.js`) or
// the QuickJS runtime (`node cli.js run bench/cells.js -- 3000 --gpu`).
// Prints frames per second, CPU per frame (the whole process: JavaScript,
@@ -14,6 +16,7 @@ const env = process.env
if (!args.includes('--dev')) env.NODE_ENV = 'production'
const cells = Number(args.find(a => /^\d+$/.test(a)) ?? 3000)
const useReact = args.includes('--react')
+const useSvelte = args.includes('--svelte')
const useSignals = args.includes('--signals')
const gpu = args.includes('--gpu')
const seconds = Number((args.find(a => a.startsWith('--seconds=')) ?? '--seconds=5').split('=')[1])
@@ -67,6 +70,22 @@ if (useReact) {
tick = useSignals
? (t) => batch(() => { style0.value = cellStyle(0, t); label0.value = label(0, t) })
: (t) => setT(t)
+} else if (useSvelte) {
+ const { render, signal, batch } = require('../../svelte')
+ const Grid = require('./Grid.svelte').default
+ let setT
+ const style0 = useSignals ? signal(cellStyle(0, 0)) : null
+ const label0 = useSignals ? signal(label(0, 0)) : null
+ const r = render(Grid, {
+ props: { cells, cellStyle, label, gridStyle, style0, label0, expose: (set) => { setT = set } },
+ title: 'BUI bench (Svelte)', width: W, height: H, gpu,
+ })
+ app = r.app
+ win = r.window
+ r.window.body.style = 'margin: 0'
+ tick = useSignals
+ ? (t) => batch(() => { style0.value = cellStyle(0, t); label0.value = label(0, t) })
+ : (t) => setT(t)
} else {
const { App } = require('..')
app = new App({ gpu })
@@ -131,7 +150,7 @@ setTimeout(() => {
running = false
clearInterval(sampler)
console.log(JSON.stringify({
- app: useReact ? (useSignals ? 'blenderui-react-signals' : 'blenderui-react') : 'blenderui-js',
+ app: useReact || useSvelte ? `blenderui-${useReact ? 'react' : 'svelte'}${useSignals ? '-signals' : ''}` : 'blenderui-js',
runtime: globalThis.__BUI_NATIVE__ ? 'quickjs (bui-runtime)' : typeof Bun !== 'undefined' ? `bun ${Bun.version}` : `node ${process.versions.node}`,
gpu,
cells,
diff --git a/bindings/node/bench/first-frame-report.js b/bindings/node/bench/first-frame-report.js
new file mode 100644
index 0000000..1eb64b9
--- /dev/null
+++ b/bindings/node/bench/first-frame-report.js
@@ -0,0 +1,9 @@
+// For the first-frame apps: once the first frame is on screen, print how long
+// that took (ms since the runtime started) and the resident memory then, and quit.
+'use strict'
+module.exports = function report ({ app, window }) {
+ window.requestAnimationFrame(() => {
+ console.log(`first-frame ${performance.now().toFixed(1)} rss ${(process.memoryUsage.rss() / 1048576).toFixed(1)}`)
+ app.quit()
+ })
+}
diff --git a/bindings/node/bench/first-frame-svelte.js b/bindings/node/bench/first-frame-svelte.js
new file mode 100644
index 0000000..62e471e
--- /dev/null
+++ b/bindings/node/bench/first-frame-svelte.js
@@ -0,0 +1,7 @@
+// bench/first-frame.js in Svelte 5 (bench/FirstFrame.svelte). Used by
+// `bench/startup.js --svelte`.
+'use strict'
+const { render } = require('@blender-ui/svelte')
+const App = require('./FirstFrame.svelte').default
+
+require('./first-frame-report')(render(App, { title: 'first frame', width: 360, height: 200, gpu: true }))
diff --git a/bindings/node/bench/first-frame.js b/bindings/node/bench/first-frame.js
index 9fb00fc..c691cb2 100644
--- a/bindings/node/bench/first-frame.js
+++ b/bindings/node/bench/first-frame.js
@@ -1,6 +1,6 @@
// A small React app that quits once its first frame is on screen, printing
// how long that took (ms since the runtime started) and the resident memory
-// then. Used by bench/startup.js.
+// then (first-frame-report.js). Used by bench/startup.js.
'use strict'
const { render, createElement: h, useState } = require('@blender-ui/react')
@@ -13,8 +13,4 @@ function App () {
h('button', { onClick: () => setCount(c => c + 1) }, 'Add one')))
}
-const { app, window } = render(h(App), { title: 'first frame', width: 360, height: 200, gpu: true })
-window.requestAnimationFrame(() => {
- console.log(`first-frame ${performance.now().toFixed(1)} rss ${(process.memoryUsage.rss() / 1048576).toFixed(1)}`)
- app.quit()
-})
+require('./first-frame-report')(render(h(App), { title: 'first frame', width: 360, height: 200, gpu: true }))
diff --git a/bindings/node/bench/startup.js b/bindings/node/bench/startup.js
index 2ae6517..76fbc9d 100644
--- a/bindings/node/bench/startup.js
+++ b/bindings/node/bench/startup.js
@@ -1,17 +1,20 @@
// Launch to first frame, for every way to run an app: the same React app
-// (bench/first-frame.js) on Node, on Bun, as a Bun executable and as a
+// (bench/first-frame.js; --svelte: its Svelte twin, first-frame-svelte.js) on Node, on Bun, as a Bun executable and as a
// QuickJS executable (`bui build`). Prints a JSON line per runtime: median
// and best of N launches, wall time from launch to exit, the app's own
// first-frame time and memory then, and the file size.
-// node bench/startup.js [runs=12]
+// node bench/startup.js [runs=12] [--svelte]
'use strict'
const fs = require('node:fs')
const os = require('node:os')
const path = require('node:path')
const { spawnSync } = require('node:child_process')
-const runs = Number(process.argv[2] ?? 12)
-const app = path.join(__dirname, 'first-frame.js')
+const svelte = process.argv.includes('--svelte')
+const runs = Number(process.argv.slice(2).find((a) => /^\d+$/.test(a)) ?? 12)
+const app = path.join(__dirname, svelte ? 'first-frame-svelte.js' : 'first-frame.js')
+// Svelte compiles its components on load (Node.js and Bun); `bui build` bundles them compiled
+const register = svelte ? require.resolve('@blender-ui/svelte/register') : null
const cli = path.join(__dirname, '..', 'cli.js')
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'bui-startup-'))
const exe = process.platform === 'win32' ? '.exe' : ''
@@ -26,8 +29,8 @@ const bunExe = spawnSync('bun', ['-e', 'process.stdout.write(process.execPath)']
{ encoding: 'utf8', shell: process.platform === 'win32' }).stdout.trim()
const variants = [
- ['Node.js', process.execPath, [app], null],
- ['Bun', bunExe, [app], null],
+ ['Node.js', process.execPath, svelte ? ['--import', require('node:url').pathToFileURL(register).href, app] : [app], null],
+ ['Bun', bunExe, svelte ? ['--preload', register, app] : [app], null],
['Bun executable (bui build --bun)', build(['--bun'], path.join(dir, `bun-app${exe}`)), [], 'self'],
['QuickJS executable (bui build)', build([], path.join(dir, `qjs-app${exe}`)), [], 'self'],
]
@@ -35,23 +38,28 @@ const variants = [
const median = (a) => { const s = [...a].sort((x, y) => x - y); return s[Math.floor(s.length / 2)] }
for (const [name, file, args, sized] of variants) {
const wall = [], first = [], rss = []
+ let failedExits = 0
for (let i = 0; i < runs + 1; i++) {
const t = process.hrtime.bigint()
const r = spawnSync(file, args, { encoding: 'utf8', env })
const ms = Number(process.hrtime.bigint() - t) / 1e6
const m = /first-frame ([\d.]+) rss ([\d.]+)/.exec(r.stdout)
- if (r.status !== 0 || !m) throw new Error(`${name}: ${r.stderr || r.stdout}`)
+ if (!m) throw new Error(`${name}: ${r.stderr || r.stdout}`)
if (i === 0) continue // the first launch warms the disk cache (and virus scanners)
- wall.push(ms)
+ // a failed exit (after the first frame) is counted, and its exit time left out
+ if (r.status !== 0) failedExits++
+ else wall.push(ms)
first.push(Number(m[1]))
rss.push(Number(m[2]))
}
console.log(JSON.stringify({
+ framework: svelte ? 'svelte' : 'react',
runtime: name,
launch_to_exit_ms: { median: +median(wall).toFixed(0), best: +Math.min(...wall).toFixed(0) },
first_frame_ms: { median: +median(first).toFixed(0), best: +Math.min(...first).toFixed(0) },
rss_at_first_frame_mb: +median(rss).toFixed(1),
size_mb: sized ? +(fs.statSync(file).size / 1048576).toFixed(1) : null,
+ failed_exits: failedExits,
}))
}
fs.rmSync(dir, { recursive: true, force: true })