// SPDX-FileCopyrightText: 2026 Paula Wisnievski // SPDX-License-Identifier: AGPL-3.0-or-later // how long verdigris takes on a stone floor, and how many set_block messages a player watching would get: placing a // 1000 wire zigzag one wire at a time, a lever switching it, a block placed and taken away beside it while it's // powered, a wire broken out of its middle and put back, a lever switching a 64×64 grid, and a lever switching a tower // of 50 torches. with the net index a wire took 0.5 ms to place however big its net, a block beside the powered zigzag // 0.1 ms and no wire messages, and switching took 15 to 20 ms for the zigzag and 50 to 60 ms for the grid, about 15 µs // a wire. walking the net on every change, a wire took 30 ms to place in the zigzag and 400 ms in the grid, a block // beside 20 ms, and switching 35 to 45 ms and 135 ms. the tower's 50 torches changing over 102 ticks took about 3 ms // more than the ticks did with nothing switched. repeater clocks come with repeaters // deno run -A tools/verdigris_bench.ts import type { Face } from "../common/mod_api/shared.ts"; import { AIR_ID } from "../common/protocol.ts"; import { block_states } from "../common/utils.ts"; import { stone_floor, test_game } from "../tests/helpers.ts"; type Pos = [number, number, number]; // alice on a stone floor at y 99 with nothing above it, placing what she holds on blocks from right by them async function floor() { const t = await test_game("mods"); stone_floor(t.game, 34); for (let x = -34; x <= 34; x++) { for (let z = -34; z <= 34; z++) { for (let y = 100; y < 103; y++) { if (t.game.world.get_block_id(x, y, z) !== AIR_ID) t.game.set_block(x, y, z, AIR_ID); } } } // every block that changes, as if a player saw them all let changes: string[] = []; const on_block_change = t.game.world.on_block_change; t.game.world.on_block_change = (x, y, z, id, state) => { changes.push(id); on_block_change?.(x, y, z, id, state); }; t.join(1, "alice"); t.send(1, { type: "move", x: 0.5, y: 100, z: 0.5, yaw: 0, pitch: 0 }); const player = t.game.players()[0]; t.game.tick(); t.take(1); const hold = (id: string) => { t.send(1, { type: "chat", text: `/give ${id} 64` }); t.send(1, { type: "select_slot", slot: player.inventory.to_synced_data().findIndex((item) => item?.id === id), }); }; const use = (x: number, y: number, z: number, face: Face) => { t.game.teleport(player, x + 0.5, y + 1, z + 0.5); t.send(1, { type: "use_block", x, y, z, face, hit: [0.5, 0.5, 0.5] }); }; // wire on the floor under each of cells, in order, and how long that took const lay = (cells: Pos[]) => { let ms = 0; cells.forEach(([x, , z], i) => { if (i % 64 === 0) hold("bworld:verdigris"); const start = performance.now(); use(x, 99, z, "top"); ms += performance.now() - start; }); t.take(1); return ms; }; // a lever on the floor at x z const lever = (x: number, z: number) => { hold("bworld:lever"); use(x, 99, z, "top"); t.take(1); return [x, 100, z] as Pos; }; // runs change count times, and how long each took and the set_block messages each sent, all and wire's const measure = (count: number, change: (i: number) => void) => { let ms = 0; let sent = 0; let wires = 0; for (let i = 0; i < count; i++) { const start = performance.now(); changes = []; change(i); ms += performance.now() - start; sent += changes.length; wires += changes.filter((id) => id === "bworld:verdigris_wire").length; } return `${(ms / count).toFixed(2)} ms, ${sent / count} set_block (${wires / count} wire)`; }; const states = (x: number, y: number, z: number) => block_states(t.game.world.get_block_value(x, y, z), t.game.world.get_block_info(x, y, z)!); return { t, hold, use, lay, lever, measure, states }; } // 1000 cells in rows of 40 along x, every other z, each row joined to the next at alternating ends function zigzag(): Pos[] { const cells: Pos[] = []; for (let row = 0; cells.length < 1000; row++) { const z = -25 + row * 2; for (let i = 0; i < 40 && cells.length < 1000; i++) cells.push([row % 2 ? 19 - i : -20 + i, 100, z]); if (cells.length < 1000) cells.push([row % 2 ? -20 : 19, 100, z + 1]); } return cells; } { const f = await floor(); const cells = zigzag(); const lever = f.lever(-21, -25); const placed = f.lay(cells); console.log( `placing ${cells.length} wires: ${(placed / cells.length).toFixed(2)} ms a wire, ${placed.toFixed(0)} ms`, ); console.log(` lever switching them: ${f.measure(20, () => f.use(...lever, "top"))}`); f.use(...lever, "top"); f.t.take(1); if (f.states(...cells.at(-1)!).power !== 15) throw new Error("the zigzag isn't powered"); // beside a wire on two rows console.log( ` a block beside them placed and taken away: ${ f.measure(20, (i) => f.t.game.set_block(0, 100, -24, i % 2 ? AIR_ID : "bworld:stone")) }`, ); const middle = cells[cells.length / 2]; console.log( ` a wire in the middle broken and put back: ${ f.measure(20, (i) => i % 2 ? f.use(middle[0], 99, middle[2], "top") : f.t.game.destroy_block(...middle)) }`, ); } { const f = await floor(); const cells: Pos[] = []; for (let z = -32; z < 32; z++) for (let x = -32; x < 32; x++) cells.push([x, 100, z]); const lever = f.lever(-33, -32); const placed = f.lay(cells); console.log( `placing a 64×64 grid: ${(placed / cells.length).toFixed(2)} ms a wire, ${(placed / 1000).toFixed(1)} s`, ); console.log(` lever switching it: ${f.measure(10, () => f.use(...lever, "top"))}`); } { const f = await floor(); // 50 torches, each on top of a block on top of the one below, and a lever on the side of the bottom block const height = 50; f.hold("bworld:verdigris_torch"); for (let i = 0; i < height; i++) { f.t.game.set_block(0, 100 + i * 2, 0, "bworld:stone"); f.use(0, 100 + i * 2, 0, "top"); } f.hold("bworld:lever"); f.use(0, 100, 0, "east"); // each torch changes 2 ticks after the one under it const ticks = height * 2 + 2; const run = () => { for (let i = 0; i < ticks; i++) f.t.game.tick(); }; run(); const top = () => f.states(0, 101 + (height - 1) * 2, 0).lit; const was = top(); const idle = f.measure(4, run); console.log(`a ${height} torch tower switched by a lever, over the ${ticks} ticks it takes:`); console.log(` ${f.measure(10, () => (f.use(1, 100, 0, "top"), run()))}, against ${idle} with nothing switched`); if (top() !== was) throw new Error("the top torch didn't change back"); } Deno.exit(0);