|
| 1 | +-- Tests for the multi-core calculation support (Modules/ParallelRunner, |
| 2 | +-- Modules/PowerCalcTasks, CalcWorker.lua). |
| 3 | +-- |
| 4 | +-- Real worker threads can't run headless (LaunchSubScript is stubbed), so |
| 5 | +-- these tests run the worker entry points (computeBatch) in-process on the |
| 6 | +-- same build, round-trip the payloads through JSON exactly as the thread |
| 7 | +-- boundary would, merge them, and require results identical to the |
| 8 | +-- single-core code path. |
| 9 | +local dkjson = require "dkjson" |
| 10 | + |
| 11 | +local function jsonRoundTrip(payload) |
| 12 | + return dkjson.decode(dkjson.encode(payload)) |
| 13 | +end |
| 14 | + |
| 15 | +local function numEq(a, b) |
| 16 | + if a == nil and b == nil then |
| 17 | + return true |
| 18 | + end |
| 19 | + if type(a) ~= "number" or type(b) ~= "number" then |
| 20 | + return a == b |
| 21 | + end |
| 22 | + if a ~= a and b ~= b then |
| 23 | + return true -- both NaN |
| 24 | + end |
| 25 | + return math.abs(a - b) <= math.max(1e-9, 1e-9 * math.max(math.abs(a), math.abs(b))) |
| 26 | +end |
| 27 | + |
| 28 | +describe("TestParallelTasks", function() |
| 29 | + before_each(function() |
| 30 | + newBuild() |
| 31 | + end) |
| 32 | + |
| 33 | + it("number sentinels survive the JSON boundary", function() |
| 34 | + local enc = PowerCalcTasks.encodeNumber |
| 35 | + local dec = PowerCalcTasks.decodeNumber |
| 36 | + assert.are.equals(5.25, dec(jsonRoundTrip({ v = enc(5.25) }).v)) |
| 37 | + assert.are.equals(math.huge, dec(jsonRoundTrip({ v = enc(math.huge) }).v)) |
| 38 | + assert.are.equals(-math.huge, dec(jsonRoundTrip({ v = enc(-math.huge) }).v)) |
| 39 | + assert.are.equals(0, dec(jsonRoundTrip({ v = enc(0/0) }).v)) -- NaN guarded to 0 |
| 40 | + assert.are.equals(nil, dec(nil)) |
| 41 | + end) |
| 42 | + |
| 43 | + it("nodePower worker batches merge to the same result as single-core PowerBuilder", function() |
| 44 | + local calcsTab = build.calcsTab |
| 45 | + calcsTab.powerStat = data.powerStatList[4] -- Combined DPS |
| 46 | + calcsTab.nodePowerMaxDepth = 4 |
| 47 | + |
| 48 | + -- Single-core reference (direct call; outside a coroutine it never yields) |
| 49 | + calcsTab:PowerBuilder() |
| 50 | + local refPower = { } |
| 51 | + for nodeId, node in pairs(build.spec.nodes) do |
| 52 | + refPower[nodeId] = { |
| 53 | + singleStat = node.power and node.power.singleStat, |
| 54 | + pathPower = node.power and node.power.pathPower, |
| 55 | + } |
| 56 | + end |
| 57 | + local refPowerMax = copyTable(calcsTab.powerMax) |
| 58 | + |
| 59 | + -- Parallel simulation: enumerate, batch exactly as production does |
| 60 | + -- (including authoritative path data), compute, JSON round trip, merge |
| 61 | + local cand = calcsTab:EnumeratePowerCandidates() |
| 62 | + assert.True(cand.total > 0) |
| 63 | + local batches = calcsTab:BuildPowerBatches(cand, 3) |
| 64 | + local common = { powerStatIndex = 4, nodePowerMaxDepth = 4 } |
| 65 | + local payloads = { } |
| 66 | + for i, batch in ipairs(batches) do |
| 67 | + payloads[i] = jsonRoundTrip(PowerCalcTasks.nodePower.computeBatch(build, common, batch, "", nil)) |
| 68 | + -- Worker payloads carry a baseline for divergence detection |
| 69 | + assert.is_table(payloads[i].baseline) |
| 70 | + end |
| 71 | + calcsTab:MergeParallelPower(payloads, build.outputRevision) |
| 72 | + |
| 73 | + for nodeId, ref in pairs(refPower) do |
| 74 | + local node = build.spec.nodes[nodeId] |
| 75 | + assert.True(numEq(ref.singleStat, node.power and node.power.singleStat), |
| 76 | + "singleStat mismatch on node "..tostring(nodeId)) |
| 77 | + assert.True(numEq(ref.pathPower, node.power and node.power.pathPower), |
| 78 | + "pathPower mismatch on node "..tostring(nodeId)) |
| 79 | + end |
| 80 | + for key, value in pairs(refPowerMax) do |
| 81 | + assert.True(numEq(value, calcsTab.powerMax[key]), "powerMax mismatch on "..key) |
| 82 | + end |
| 83 | + end) |
| 84 | + |
| 85 | + it("nodePower merge discards results from a stale build revision", function() |
| 86 | + local calcsTab = build.calcsTab |
| 87 | + calcsTab.powerBuildFlag = false |
| 88 | + calcsTab:MergeParallelPower({ }, build.outputRevision - 1) |
| 89 | + assert.True(calcsTab.powerBuildFlag) |
| 90 | + end) |
| 91 | + |
| 92 | + it("notable measureOne matches the worker batch path", function() |
| 93 | + local itemsTab = build.itemsTab |
| 94 | + itemsTab.displayItem = new("Item", "Rarity: RARE\nTest Amulet\nJade Amulet\n+30 to Dexterity") |
| 95 | + itemsTab.anointEnchantSlot = 1 |
| 96 | + |
| 97 | + -- Collect a handful of anointable notables |
| 98 | + local nodeIds = { } |
| 99 | + for nodeId, node in pairs(build.spec.tree.nodes) do |
| 100 | + if node.recipe and #node.recipe >= 1 and node.modKey ~= "" then |
| 101 | + table.insert(nodeIds, nodeId) |
| 102 | + if #nodeIds >= 15 then |
| 103 | + break |
| 104 | + end |
| 105 | + end |
| 106 | + end |
| 107 | + assert.True(#nodeIds > 0) |
| 108 | + |
| 109 | + -- Single-core reference via the shared measureOne |
| 110 | + local sortDetail = PowerCalcTasks.findSortDetail("CombinedDPS") |
| 111 | + local calcFunc = build.calcsTab:GetMiscCalculator() |
| 112 | + local itemType = itemsTab.displayItem.base.type |
| 113 | + local calcBase = calcFunc({ repSlotName = itemType, repItem = itemsTab:anointItem(nil) }) |
| 114 | + local ref = { } |
| 115 | + for _, nodeId in ipairs(nodeIds) do |
| 116 | + ref[nodeId] = PowerCalcTasks.notable.measureOne(itemsTab, calcFunc, calcBase, sortDetail, itemType, build.spec.tree.nodes[nodeId]) |
| 117 | + end |
| 118 | + |
| 119 | + -- Worker path |
| 120 | + local payload = jsonRoundTrip(PowerCalcTasks.notable.computeBatch(build, |
| 121 | + { sortMode = "CombinedDPS", anointEnchantSlot = 1 }, |
| 122 | + { nodeIds = nodeIds }, |
| 123 | + itemsTab.displayItem:BuildRaw(), nil)) |
| 124 | + for _, nodeId in ipairs(nodeIds) do |
| 125 | + local workerPower = PowerCalcTasks.decodeNumber(payload.results[tostring(nodeId)]) |
| 126 | + assert.True(numEq(ref[nodeId], workerPower), "measuredPower mismatch on node "..tostring(nodeId)) |
| 127 | + end |
| 128 | + end) |
| 129 | + |
| 130 | + it("itemdb measureOne matches the worker batch path", function() |
| 131 | + -- Item DBs load over frames in the background |
| 132 | + local guard = 0 |
| 133 | + while main.onFrameFuncs["LoadItems"] and guard < 1000 do |
| 134 | + runCallback("OnFrame") |
| 135 | + guard = guard + 1 |
| 136 | + end |
| 137 | + local names = { } |
| 138 | + for name in pairs(main.uniqueDB.list) do |
| 139 | + table.insert(names, name) |
| 140 | + if #names >= 10 then |
| 141 | + break |
| 142 | + end |
| 143 | + end |
| 144 | + assert.True(#names > 0) |
| 145 | + |
| 146 | + local itemsTab = build.itemsTab |
| 147 | + local sortDetail = PowerCalcTasks.findSortDetail("Life") |
| 148 | + local calcFunc = build.calcsTab:GetMiscCalculator() |
| 149 | + local ref = { } |
| 150 | + for _, name in ipairs(names) do |
| 151 | + ref[name] = PowerCalcTasks.itemdb.measureOne(itemsTab, calcFunc, sortDetail, sortDetail.sortMode, false, main.uniqueDB.list[name]) |
| 152 | + end |
| 153 | + |
| 154 | + local payload = jsonRoundTrip(PowerCalcTasks.itemdb.computeBatch(build, |
| 155 | + { sortMode = "Life", dbType = "UNIQUE" }, |
| 156 | + { names = names }, "", nil)) |
| 157 | + for _, name in ipairs(names) do |
| 158 | + local workerPower = PowerCalcTasks.decodeNumber(payload.results[name]) |
| 159 | + assert.True(numEq(ref[name], workerPower), "measuredPower mismatch on item "..name) |
| 160 | + end |
| 161 | + end) |
| 162 | + |
| 163 | + it("compare descriptors compute identically through the worker entry point", function() |
| 164 | + local primaryFile = io.open("../spec/TestBuilds/3.13/OccVortex.xml", "r") |
| 165 | + local compareFile = io.open("../spec/TestBuilds/3.13/Mirage Archer Toxic Rain.xml", "r") |
| 166 | + assert.is_not_nil(primaryFile) |
| 167 | + assert.is_not_nil(compareFile) |
| 168 | + local primaryXml = primaryFile:read("*a") |
| 169 | + primaryFile:close() |
| 170 | + local compareXml = compareFile:read("*a") |
| 171 | + compareFile:close() |
| 172 | + |
| 173 | + loadBuildFromXML(primaryXml, "Primary") |
| 174 | + for i = 1, 5 do |
| 175 | + runCallback("OnFrame") |
| 176 | + end |
| 177 | + local compareTab = build.compareTab |
| 178 | + local compareEntry = new("CompareEntry", compareXml, "Comparison") |
| 179 | + compareEntry.xmlText = compareXml |
| 180 | + local powerStat = data.powerStatList[4] -- Combined DPS |
| 181 | + local categories = { treeNodes = true, items = true, skillGems = true, supportGems = true, config = true } |
| 182 | + |
| 183 | + -- Single-core reference (run the real coroutine to completion) |
| 184 | + local co = coroutine.create(function() |
| 185 | + compareTab:ComparePowerBuilder(compareEntry, powerStat, categories) |
| 186 | + end) |
| 187 | + repeat |
| 188 | + local res, errMsg = coroutine.resume(co) |
| 189 | + assert.True(res, errMsg) |
| 190 | + until coroutine.status(co) == "dead" |
| 191 | + local refResults = compareTab.comparePowerResults |
| 192 | + assert.True(#refResults > 0) |
| 193 | + |
| 194 | + -- Parallel simulation |
| 195 | + local descriptors = compareTab:EnumerateComparePowerCandidates(compareEntry, categories) |
| 196 | + for i, desc in ipairs(descriptors) do |
| 197 | + desc.i = i |
| 198 | + end |
| 199 | + local calcFunc, calcBase = compareTab.calcs.getMiscCalculator(build) |
| 200 | + local fmtCtx = compareTab:MakeComparePowerFormatContext(compareEntry, powerStat, calcBase) |
| 201 | + local resultByIndex = { } |
| 202 | + for _, slice in ipairs(ParallelRunner:PartitionList(descriptors, 3)) do |
| 203 | + local payload = jsonRoundTrip(PowerCalcTasks.compare.computeBatch(build, |
| 204 | + { powerStatIndex = 4 }, { descriptors = slice }, compareXml, nil)) |
| 205 | + for _, res in ipairs(payload.results) do |
| 206 | + resultByIndex[res.i] = res |
| 207 | + end |
| 208 | + end |
| 209 | + local parResults = { } |
| 210 | + for i, desc in ipairs(descriptors) do |
| 211 | + local res = resultByIndex[i] |
| 212 | + if res and res.ok and res.impact then |
| 213 | + local row = compareTab:BuildComparePowerRow(desc, PowerCalcTasks.decodeNumber(res.impact), res.extra or { }, fmtCtx) |
| 214 | + if row then |
| 215 | + table.insert(parResults, row) |
| 216 | + end |
| 217 | + end |
| 218 | + end |
| 219 | + |
| 220 | + assert.are.equals(#refResults, #parResults) |
| 221 | + for i = 1, #refResults do |
| 222 | + assert.are.equals(refResults[i].category, parResults[i].category) |
| 223 | + assert.are.equals(refResults[i].name, parResults[i].name) |
| 224 | + assert.True(numEq(refResults[i].impact, parResults[i].impact), "impact mismatch on row "..i) |
| 225 | + assert.are.equals(refResults[i].combinedImpactStr, parResults[i].combinedImpactStr) |
| 226 | + end |
| 227 | + end) |
| 228 | +end) |
0 commit comments