#include "settlement/core/world.hpp" #include #include #include #include #include #include #include #include namespace { using namespace settlement; using Clock = std::chrono::steady_clock; using Microseconds = std::chrono::duration; constexpr int warmup_ticks = 32; constexpr int measured_ticks = 240; int parse_number(std::string_view argument) { int number = 0; const auto end = argument.data() + argument.size(); const auto result = std::from_chars(argument.data(), end, number); return result.ec == std::errc{} && result.ptr == end ? number : 0; } void populate(World& world, int residents) { world.start_scenario({79, 79}); const auto send = [&](CommandKind kind, GridPosition cell, std::uint8_t value = 0, AgentId resident = 0) { const auto result = world.apply_command({colony_host, world.next_command_sequence(colony_host), kind, cell, value, {}, resident}); if (result.status != CommandStatus::Accepted) throw std::runtime_error("benchmark command rejected"); }; constexpr Profession roles[] = { Profession::Farmer, Profession::Farmer, Profession::Farmer, Profession::Farmer, Profession::Woodcutter, Profession::Woodcutter, Profession::Miner, Profession::Builder, Profession::Builder, Profession::Builder, }; for (int sector = 0; sector < residents / 10; ++sector) { const int x = 10 * (sector % 8); const int y = 10 * (sector / 8); const auto pantry = world.add_storage({x + 4, y + 4}, 80, StorageKind::Pantry); world.stock_storage(pantry, ItemType::Berries, 20); const auto materials = world.add_storage({x + 4, y + 5}, 110); world.stock_storage(materials, ItemType::Wood, 2); static_cast(world.add_storage({x + 7, y + 5}, 110)); for (int index = 0; index < 10; ++index) { const auto id = world.spawn_agent({x + index % 5, y + 6 + index / 5}); send(CommandKind::AssignProfession, {}, static_cast(roles[index]), id); } for (int offset = 0; offset < 2; ++offset) { send(CommandKind::PlaceBerryField, {x + offset, y + 1}); send(CommandKind::PlaceFlaxField, {x + offset, y + 3}); send(CommandKind::PlaceTreePlot, {x + 6 + offset, y + 2}); send(CommandKind::PlaceLoom, {x + 3 + offset, y + 3}); send(CommandKind::PlaceBed, {x + 6 + offset, y + 7}); } const GridPosition rock{x + 8, y + 4}; world.set_blocked(rock); send(CommandKind::PlaceMine, rock); } } } // namespace int main(int argc, char** argv) try { const int residents = argc == 3 ? parse_number(argv[1]) : 0; const int workers = argc == 3 ? parse_number(argv[2]) : 0; if (residents < 10 || residents > 500 || residents % 10 != 0 || workers < static_cast(min_worker_count) || workers > 32) { std::cerr << "Usage: settlement_colony_benchmark RESIDENTS WORKERS (10..500 by 10, 2..32)\n"; return 2; } World world(2026, 80, 80, static_cast(workers)); populate(world, residents); for (int tick = 0; tick < warmup_ticks; ++tick) world.step(); const auto baseline_paths = world.path_searches(); const auto baseline_nodes = world.path_nodes_visited(); const auto baseline_hits = world.assignment_path_cache_hits(); TickProfile profile; std::vector ticks; ticks.reserve(measured_ticks); for (int tick = 0; tick < measured_ticks; ++tick) { const auto start = Clock::now(); world.step(&profile); ticks.push_back(Microseconds(Clock::now() - start).count()); } std::sort(ticks.begin(), ticks.end()); std::uint64_t cloth = 0; for (const auto& storage : world.storages()) cloth += storage.item_count(ItemType::Cloth); const auto average = [](auto duration) { return Microseconds(duration).count() / measured_ticks; }; std::cout << std::fixed << std::setprecision(2) << "residents=" << residents << '\n' << "workers=" << workers << '\n' << "p50_us=" << ticks[measured_ticks / 2] << '\n' << "p95_us=" << ticks[(measured_ticks * 95 + 99) / 100 - 1] << '\n' << "growth_needs_us=" << average(profile.growth_and_needs) << '\n' << "resource_us=" << average(profile.resource_assignment) << '\n' << "production_us=" << average(profile.production_assignment) << '\n' << "other_jobs_us=" << average(profile.generic_assignment) << '\n' << "advancement_us=" << average(profile.advancement) << '\n' << "path_searches=" << world.path_searches() - baseline_paths << '\n' << "path_cache_hits=" << world.assignment_path_cache_hits() - baseline_hits << '\n' << "logical_path_nodes=" << world.path_nodes_visited() - baseline_nodes << '\n' << "assigned_beds=" << world.scenario_progress().assigned_beds << '\n' << "cloth_in_stock=" << cloth << '\n' << "meals_eaten=" << world.meals_eaten() << '\n'; return 0; } catch (const std::exception& error) { std::cerr << error.what() << '\n'; return 1; }