From f7223129af6acb57d9083c925b3b8b051c140b41 Mon Sep 17 00:00:00 2001 From: Erich Schubert Date: Sat, 26 Oct 2024 13:45:55 +0200 Subject: [PATCH] add /bench_bulk_get_node_raw --- games/devtest/mods/benchmarks/init.lua | 111 +++++++++++++++++++++++++ 1 file changed, 111 insertions(+) diff --git a/games/devtest/mods/benchmarks/init.lua b/games/devtest/mods/benchmarks/init.lua index 8f6bb1ee4..5f324697b 100644 --- a/games/devtest/mods/benchmarks/init.lua +++ b/games/devtest/mods/benchmarks/init.lua @@ -155,6 +155,117 @@ core.register_chatcommand("bench_bulk_get_node", { end, }) +core.register_chatcommand("bench_bulk_swap_node", { + params = "", + description = "Benchmark: Bulk-get 99×99×99 nodes with raw API", + func = function(name, param) + local player = core.get_player_by_name(name) + if not player then + return false, "No player." + end + local pos_list = get_positions_cube(player:get_pos()) + local function bench() + local start_time = core.get_us_time() + for i=1,#pos_list do + local pos_i = pos_list[i] + local nid = core.get_node_raw(pos_i.x, pos_i.y, pos_i.z) + -- Make sure the name lookup is never optimized away. + -- Table allocation might still be omitted. But only accessing + -- the name of a node is a common pattern anyways. + if nid == -42 then + error("should never happen") + end + end + return core.get_us_time() - start_time + end + + core.chat_send_player(name, "Benchmarking core.get_node_raw. Warming up ...") + bench() + + core.chat_send_player(name, "Warming up finished, now benchmarking ...") + local result_us = bench() + + local msg = string.format("Benchmark results: core.get_node_raw loop 1: %.2f ms", + result_us / 1000) + return true, msg + end, +}) + +core.register_chatcommand("bench_bulk_get_node_raw_lookup", { + params = "", + description = "Benchmark: Bulk-get 99×99×99 nodes with raw API and lookup names", + func = function(name, param) + local player = core.get_player_by_name(name) + if not player then + return false, "No player." + end + local pos_list = get_positions_cube(player:get_pos()) + local function bench() + local start_time = core.get_us_time() + for i=1,#pos_list do + local pos_i = pos_list[i] + local nid = core.get_node_raw(pos_i.x, pos_i.y, pos_i.z) + local name = core.get_name_from_content_id(nid) + -- Make sure the name lookup is never optimized away. + -- Table allocation might still be omitted. But only accessing + -- the name of a node is a common pattern anyways. + if name == "benchmarks:nonexistent_node" then + error("should never happen") + end + end + return core.get_us_time() - start_time + end + + core.chat_send_player(name, "Benchmarking core.get_node_raw+get_name_from_content_id. Warming up ...") + bench() + + core.chat_send_player(name, "Warming up finished, now benchmarking ...") + local result_us = bench() + + local msg = string.format("Benchmark results: core.get_node_raw+get_name_from_content_id loop 1: %.2f ms", + result_us / 1000) + return true, msg + end, +}) + +core.register_chatcommand("bench_bulk_get_node_vmanip", { + params = "", + description = "Benchmark: Bulk-get 99×99×99 nodes with voxel manipulator", + func = function(name, param) + local player = core.get_player_by_name(name) + if not player then + return false, "No player." + end + local pos = player:get_pos() + local function bench() + local start_time = core.get_us_time() + local vm = core.get_voxel_manip(pos:offset(1,1,1), pos:offset(100,100,100)) + local data = vm:get_data() + local mid_time = core.get_us_time() + for i=1,99*99*99 do + local nid = data[i] + -- Make sure the name lookup is never optimized away. + -- Table allocation might still be omitted. But only accessing + -- the name of a node is a common pattern anyways. + if nid == -42 then + error("should never happen") + end + end + return core.get_us_time() - start_time, mid_time - start_time + end + + core.chat_send_player(name, "Benchmarking core.get_voxel_vmanip+get_data+loop . Warming up ...") + bench() + + core.chat_send_player(name, "Warming up finished, now benchmarking ...") + local result_us, get_data_us = bench() + + local msg = string.format("Benchmark results: core.get_voxel_vmanip+get_data+loop loop 1: %.2f ms of which get_data() %.2f ms", + result_us / 1000, get_data_us / 1000) + return true, msg + end, +}) + core.register_chatcommand("bench_bulk_swap_node", { params = "", description = "Benchmark: Bulk-swap 99×99×99 stone nodes",