|
| 1 | +#!/usr/bin/env lua |
| 2 | +--[[ |
| 3 | + Benchmark: run every engine at all supported ELO settings (100 ELO steps) |
| 4 | + vs random moves. Play maximum of 4 engine moves per test; measure average |
| 5 | + thinking time per move. Every 10 tests print a ranking table (engine, elo, |
| 6 | + avg ms, time/elo) sorted by time/elo. |
| 7 | + Usage: lua bin/benchmark.lua |
| 8 | +]] |
| 9 | + |
| 10 | +local scriptPath = debug.getinfo(1, "S").source:match("@?(.*/)") |
| 11 | +if not scriptPath then scriptPath = "./" end |
| 12 | +local projectRoot = scriptPath .. "../" |
| 13 | +package.path = projectRoot .. "src/?.lua;src/?/init.lua;lib/?.lua;?.lua;" .. (package.path or "") |
| 14 | + |
| 15 | +DeltaChess = DeltaChess or {} |
| 16 | +require("init") |
| 17 | + |
| 18 | +local EngineRunner = DeltaChess.EngineRunner |
| 19 | +local Engines = DeltaChess.Engines |
| 20 | +local Constants = DeltaChess.Constants |
| 21 | +local Board = DeltaChess.Board |
| 22 | +local MoveGen = DeltaChess.MoveGen |
| 23 | + |
| 24 | +local ELO_STEP = 100 |
| 25 | +local MAX_ENGINE_MOVES = 4 |
| 26 | + |
| 27 | +-- Build list of { engineId, elo } for every engine at 100 ELO steps (min to max) |
| 28 | +local function buildTests() |
| 29 | + local tests = {} |
| 30 | + for id, engine in pairs(Engines.Registry or {}) do |
| 31 | + if type(engine.GetEloRange) == "function" then |
| 32 | + local ok, range = pcall(function() return engine:GetEloRange() end) |
| 33 | + if ok and range and range[1] and range[2] then |
| 34 | + local minElo, maxElo = range[1], range[2] |
| 35 | + for elo = minElo, maxElo, ELO_STEP do |
| 36 | + table.insert(tests, { engineId = id, elo = elo }) |
| 37 | + end |
| 38 | + end |
| 39 | + end |
| 40 | + end |
| 41 | + return tests |
| 42 | +end |
| 43 | + |
| 44 | +-- Pick a random legal move from current board position. Returns UCI string or nil. |
| 45 | +local function randomLegalMove(board) |
| 46 | + local legal = board:LegalMoves() |
| 47 | + if not legal or #legal == 0 then return nil end |
| 48 | + local idx = math.random(1, #legal) |
| 49 | + return MoveGen.MoveToUci(legal[idx]) |
| 50 | +end |
| 51 | + |
| 52 | +-- Run one test: engine vs random, up to MAX_ENGINE_MOVES engine moves; return average thinking time in ms or nil on error. |
| 53 | +-- Runs synchronously via Scheduler(next) => next(). |
| 54 | +local function runOneTest(engineId, elo) |
| 55 | + local board = Board.New() |
| 56 | + local times = {} -- list of thinking times in seconds (from os.clock()) |
| 57 | + local engineMoveCount = 0 |
| 58 | + local done = false |
| 59 | + local errMsg = nil |
| 60 | + |
| 61 | + local function playRandomMove() |
| 62 | + local uci = randomLegalMove(board) |
| 63 | + if not uci then return true end -- game over (no legal moves) |
| 64 | + local ok = board:MakeMoveUci(uci) |
| 65 | + if not ok then return true end |
| 66 | + return false |
| 67 | + end |
| 68 | + |
| 69 | + local function doEngineMove(cont) |
| 70 | + if done then if cont then cont() end return end |
| 71 | + if board:IsEnded() then |
| 72 | + done = true |
| 73 | + if cont then cont() end |
| 74 | + return |
| 75 | + end |
| 76 | + if engineMoveCount >= MAX_ENGINE_MOVES then |
| 77 | + done = true |
| 78 | + if cont then cont() end |
| 79 | + return |
| 80 | + end |
| 81 | + |
| 82 | + local fen = board:GetFen() |
| 83 | + local t0 = os.clock() |
| 84 | + |
| 85 | + EngineRunner.Create(engineId) |
| 86 | + :Fen(fen) |
| 87 | + :Elo(elo) |
| 88 | + :Scheduler(function(next) next() end) |
| 89 | + :OnComplete(function(res, err) |
| 90 | + local t1 = os.clock() |
| 91 | + if err or not res or not res.move then |
| 92 | + errMsg = err and (err.message or tostring(err)) or "no move" |
| 93 | + done = true |
| 94 | + if cont then cont() end |
| 95 | + return |
| 96 | + end |
| 97 | + local ok = board:MakeMoveUci(res.move) |
| 98 | + if not ok then |
| 99 | + errMsg = "illegal move" |
| 100 | + done = true |
| 101 | + if cont then cont() end |
| 102 | + return |
| 103 | + end |
| 104 | + engineMoveCount = engineMoveCount + 1 |
| 105 | + table.insert(times, t1 - t0) |
| 106 | + |
| 107 | + if board:IsEnded() or engineMoveCount >= MAX_ENGINE_MOVES then |
| 108 | + done = true |
| 109 | + if cont then cont() end |
| 110 | + return |
| 111 | + end |
| 112 | + -- Random reply |
| 113 | + if playRandomMove() then |
| 114 | + done = true |
| 115 | + if cont then cont() end |
| 116 | + return |
| 117 | + end |
| 118 | + doEngineMove(cont) |
| 119 | + end) |
| 120 | + :Run() |
| 121 | + end |
| 122 | + |
| 123 | + -- Engine plays white (first move); runs synchronously via Scheduler(next) next() |
| 124 | + doEngineMove(function() end) |
| 125 | + |
| 126 | + if errMsg then return nil, errMsg end |
| 127 | + if #times == 0 then return nil, "no moves" end |
| 128 | + local sum = 0 |
| 129 | + for _, t in ipairs(times) do sum = sum + t end |
| 130 | + return (sum / #times) * 1000 -- average ms |
| 131 | +end |
| 132 | + |
| 133 | +-- Results: { engineId, elo, avgTimeMs, timePerElo } |
| 134 | +local results = {} |
| 135 | + |
| 136 | +local function printRanking() |
| 137 | + local list = {} |
| 138 | + for _, r in ipairs(results) do |
| 139 | + list[#list + 1] = { |
| 140 | + engineId = r.engineId, |
| 141 | + elo = r.elo, |
| 142 | + avgTimeMs = r.avgTimeMs, |
| 143 | + timePerElo = r.timePerElo, |
| 144 | + } |
| 145 | + end |
| 146 | + table.sort(list, function(a, b) return a.timePerElo < b.timePerElo end) |
| 147 | + io.write("\n--- Ranking (time/elo, lower is better) ---\n") |
| 148 | + io.write(string.format(" %-24s %6s %12s %12s\n", "Engine", "ELO", "Avg(ms)", "Time/ELO")) |
| 149 | + io.write(" " .. string.rep("-", 56) .. "\n") |
| 150 | + for i, row in ipairs(list) do |
| 151 | + io.write(string.format(" %2d. %-20s %6d %12.2f %12.4f\n", |
| 152 | + i, row.engineId, row.elo, row.avgTimeMs, row.timePerElo)) |
| 153 | + end |
| 154 | + io.write("\n") |
| 155 | +end |
| 156 | + |
| 157 | +local tests = buildTests() |
| 158 | +if #tests == 0 then |
| 159 | + io.stderr:write("No engines with GetEloRange found.\n") |
| 160 | + os.exit(1) |
| 161 | +end |
| 162 | + |
| 163 | +math.randomseed(os.time()) |
| 164 | +io.write("Benchmark: " .. #tests .. " tests (each engine at 100 ELO steps, 4 moves vs random).\n\n") |
| 165 | + |
| 166 | +for i, t in ipairs(tests) do |
| 167 | + io.write("[" .. i .. "/" .. #tests .. "] " .. t.engineId .. " @" .. t.elo .. " ... ") |
| 168 | + io.flush() |
| 169 | + local avgMs, err = runOneTest(t.engineId, t.elo) |
| 170 | + if not avgMs then |
| 171 | + io.write("FAIL (" .. tostring(err) .. ")\n") |
| 172 | + else |
| 173 | + local timePerElo = avgMs / t.elo |
| 174 | + table.insert(results, { |
| 175 | + engineId = t.engineId, |
| 176 | + elo = t.elo, |
| 177 | + avgTimeMs = avgMs, |
| 178 | + timePerElo = timePerElo, |
| 179 | + }) |
| 180 | + io.write(string.format("%.2f ms avg\n", avgMs)) |
| 181 | + end |
| 182 | + if i % 10 == 0 and #results > 0 then |
| 183 | + printRanking() |
| 184 | + end |
| 185 | +end |
| 186 | + |
| 187 | +if #results > 0 then |
| 188 | + io.write("Done. Final ") |
| 189 | + printRanking() |
| 190 | +end |
0 commit comments