|
| 1 | +import { createLightingChunkInputFromChunk, LightingSystem } from '@craftvale/core/server' |
| 2 | +import { parseCliFlagValue } from '@craftvale/core/shared' |
| 3 | +import { BLOCK_IDS, CHUNK_SIZE, CHUNK_VOLUME, createGeneratedChunk } from '@craftvale/core/shared' |
| 4 | + |
| 5 | +import type { CliBenchmark } from './types.ts' |
| 6 | + |
| 7 | +const DEFAULT_SEED = 1337 |
| 8 | +const DEFAULT_WARMUP_ROUNDS = 20 |
| 9 | +const DEFAULT_MEASURED_ROUNDS = 140 |
| 10 | +const DEFAULT_FIXTURE_COORDS = [ |
| 11 | + { x: -8, z: -6 }, |
| 12 | + { x: -7, z: -2 }, |
| 13 | + { x: -6, z: 1 }, |
| 14 | + { x: -5, z: 5 }, |
| 15 | + { x: -4, z: -8 }, |
| 16 | + { x: -3, z: -3 }, |
| 17 | + { x: -2, z: 2 }, |
| 18 | + { x: -1, z: 7 }, |
| 19 | + { x: 0, z: -7 }, |
| 20 | + { x: 1, z: -4 }, |
| 21 | + { x: 2, z: 0 }, |
| 22 | + { x: 3, z: 4 }, |
| 23 | + { x: 4, z: 8 }, |
| 24 | + { x: 5, z: -5 }, |
| 25 | + { x: 6, z: -1 }, |
| 26 | + { x: 7, z: 3 }, |
| 27 | +] as const |
| 28 | +const FEATURE_POINTS = [ |
| 29 | + [4, 4], |
| 30 | + [7, 9], |
| 31 | + [11, 6], |
| 32 | +] as const |
| 33 | + |
| 34 | +interface LightingBuffers { |
| 35 | + sky: Uint8Array |
| 36 | + block: Uint8Array |
| 37 | +} |
| 38 | + |
| 39 | +interface BenchmarkResult { |
| 40 | + label: string |
| 41 | + totalMs: number |
| 42 | + meanMs: number |
| 43 | + medianMs: number |
| 44 | + minMs: number |
| 45 | + maxMs: number |
| 46 | +} |
| 47 | + |
| 48 | +const parsePositiveIntegerFlag = ( |
| 49 | + argv: readonly string[], |
| 50 | + flagName: string, |
| 51 | + fallback: number, |
| 52 | +): number => { |
| 53 | + const value = parseCliFlagValue(argv, flagName) |
| 54 | + if (value === null) { |
| 55 | + return fallback |
| 56 | + } |
| 57 | + |
| 58 | + const parsed = Number.parseInt(value, 10) |
| 59 | + if (!Number.isFinite(parsed) || parsed <= 0) { |
| 60 | + throw new Error(`Expected --${flagName} to be a positive integer, got "${value}".`) |
| 61 | + } |
| 62 | + |
| 63 | + return parsed |
| 64 | +} |
| 65 | + |
| 66 | +const clampY = (value: number): number => Math.max(0, Math.min(255, value)) |
| 67 | + |
| 68 | +const createFixtureChunk = (coord: { x: number; z: number }, seed: number) => { |
| 69 | + const chunk = createGeneratedChunk(coord, seed) |
| 70 | + |
| 71 | + for (const [localX, localZ] of FEATURE_POINTS) { |
| 72 | + const columnIndex = localX + CHUNK_SIZE * localZ |
| 73 | + const surfaceY = chunk.heightmap[columnIndex] ?? 0 |
| 74 | + const glowstoneY = clampY(surfaceY + 1) |
| 75 | + const roofY = clampY(surfaceY + 3) |
| 76 | + const cavityY = clampY(surfaceY + 2) |
| 77 | + |
| 78 | + chunk.set(localX, glowstoneY, localZ, BLOCK_IDS.glowstone) |
| 79 | + if (localX + 1 < CHUNK_SIZE) { |
| 80 | + chunk.set(localX + 1, roofY, localZ, BLOCK_IDS.grass) |
| 81 | + chunk.set(localX + 1, cavityY, localZ, BLOCK_IDS.air) |
| 82 | + } |
| 83 | + if (localZ + 1 < CHUNK_SIZE) { |
| 84 | + chunk.set(localX, clampY(surfaceY + 1), localZ + 1, BLOCK_IDS.glass) |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + chunk.dirtyLight = true |
| 89 | + return chunk |
| 90 | +} |
| 91 | + |
| 92 | +const createBuffers = (count: number): LightingBuffers[] => |
| 93 | + Array.from({ length: count }, () => ({ |
| 94 | + sky: new Uint8Array(CHUNK_VOLUME), |
| 95 | + block: new Uint8Array(CHUNK_VOLUME), |
| 96 | + })) |
| 97 | + |
| 98 | +const measure = ( |
| 99 | + label: string, |
| 100 | + runRound: () => void, |
| 101 | + warmupRounds: number, |
| 102 | + measuredRounds: number, |
| 103 | +): BenchmarkResult => { |
| 104 | + for (let round = 0; round < warmupRounds; round += 1) { |
| 105 | + runRound() |
| 106 | + } |
| 107 | + |
| 108 | + const samples: number[] = [] |
| 109 | + for (let round = 0; round < measuredRounds; round += 1) { |
| 110 | + const startedAt = performance.now() |
| 111 | + runRound() |
| 112 | + samples.push(performance.now() - startedAt) |
| 113 | + } |
| 114 | + |
| 115 | + const totalMs = samples.reduce((sum, value) => sum + value, 0) |
| 116 | + const meanMs = totalMs / samples.length |
| 117 | + const sorted = [...samples].sort((left, right) => left - right) |
| 118 | + const medianMs = sorted[Math.floor(sorted.length / 2)] ?? 0 |
| 119 | + const minMs = sorted[0] ?? 0 |
| 120 | + const maxMs = sorted[sorted.length - 1] ?? 0 |
| 121 | + |
| 122 | + return { |
| 123 | + label, |
| 124 | + totalMs, |
| 125 | + meanMs, |
| 126 | + medianMs, |
| 127 | + minMs, |
| 128 | + maxMs, |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +const formatMs = (value: number): string => `${value.toFixed(3)} ms` |
| 133 | + |
| 134 | +const runLightingBenchmark = (argv: readonly string[]): void => { |
| 135 | + const warmupRounds = parsePositiveIntegerFlag(argv, 'warmup', DEFAULT_WARMUP_ROUNDS) |
| 136 | + const measuredRounds = parsePositiveIntegerFlag(argv, 'rounds', DEFAULT_MEASURED_ROUNDS) |
| 137 | + const seed = parsePositiveIntegerFlag(argv, 'seed', DEFAULT_SEED) |
| 138 | + const fixtureCount = parsePositiveIntegerFlag(argv, 'fixtures', DEFAULT_FIXTURE_COORDS.length) |
| 139 | + const fixtureCoords = DEFAULT_FIXTURE_COORDS.slice(0, fixtureCount) |
| 140 | + if (fixtureCoords.length === 0) { |
| 141 | + throw new Error('Lighting benchmark requires at least one fixture chunk.') |
| 142 | + } |
| 143 | + |
| 144 | + const fixtureChunks = fixtureCoords.map((coord) => createFixtureChunk(coord, seed)) |
| 145 | + const fixtureInputs = fixtureChunks.map((chunk) => createLightingChunkInputFromChunk(chunk)) |
| 146 | + const nativeBuffers = createBuffers(fixtureChunks.length) |
| 147 | + const nativeLighting = new LightingSystem() |
| 148 | + |
| 149 | + const runNativeRound = (): void => { |
| 150 | + for (let index = 0; index < fixtureInputs.length; index += 1) { |
| 151 | + const chunk = fixtureInputs[index]! |
| 152 | + const buffers = nativeBuffers[index]! |
| 153 | + buffers.sky.fill(0) |
| 154 | + buffers.block.fill(0) |
| 155 | + nativeLighting.relightChunk(chunk, buffers) |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + runNativeRound() |
| 160 | + |
| 161 | + const native = measure('native', runNativeRound, warmupRounds, measuredRounds) |
| 162 | + const perChunkNativeMs = native.meanMs / fixtureChunks.length |
| 163 | + |
| 164 | + console.log('Lighting benchmark') |
| 165 | + console.log( |
| 166 | + `Fixture: ${fixtureChunks.length} generated chunks, seed ${seed}, ${warmupRounds} warmup round(s), ${measuredRounds} measured round(s)`, |
| 167 | + ) |
| 168 | + console.log(`Native mean: ${formatMs(native.meanMs)} (${formatMs(perChunkNativeMs)} per chunk)`) |
| 169 | + console.log( |
| 170 | + `Native range: ${formatMs(native.minMs)} min / ${formatMs(native.medianMs)} median / ${formatMs(native.maxMs)} max`, |
| 171 | + ) |
| 172 | +} |
| 173 | + |
| 174 | +export const lightingBenchmark: CliBenchmark = { |
| 175 | + name: 'lighting', |
| 176 | + description: 'Measure native chunk relight throughput on deterministic generated chunk fixtures.', |
| 177 | + run: runLightingBenchmark, |
| 178 | +} |
0 commit comments