Add exact periodic neighborhood queries and measured scan baseline
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
#include "evo/simulation/spatial.hpp"
|
||||
|
||||
#include <array>
|
||||
#include <chrono>
|
||||
#include <iostream>
|
||||
#include <stdexcept>
|
||||
|
||||
int main() try {
|
||||
constexpr std::uint64_t seed = 123456;
|
||||
std::cout << "{\"benchmark\":\"spatial_scan\",\"seed\":" << seed
|
||||
<< ",\"queries\":128,\"compiler\":\""
|
||||
#if defined(__clang__)
|
||||
<< "Clang " << __clang_version__
|
||||
#elif defined(_MSC_VER)
|
||||
<< "MSVC " << _MSC_VER
|
||||
#else
|
||||
<< "GCC " << __VERSION__
|
||||
#endif
|
||||
<< "\",\"build\":\""
|
||||
#if defined(NDEBUG)
|
||||
<< "release"
|
||||
#else
|
||||
<< "debug"
|
||||
#endif
|
||||
<< "\"}\n";
|
||||
for (const auto count : {1'000U, 10'000U, 100'000U}) {
|
||||
for (const auto clustered : {false, true}) {
|
||||
evo::SimulationWorld world{seed};
|
||||
evo::Random random{seed};
|
||||
const auto coordinate = [&] {
|
||||
return evo::Millimetres{
|
||||
static_cast<std::uint32_t>(random.next() % (clustered ? 32U : 1024U))};
|
||||
};
|
||||
for (std::uint32_t index = 0; index < count; ++index) {
|
||||
if (!world.create_entity({coordinate(), coordinate()})) {
|
||||
throw std::runtime_error("benchmark setup failed");
|
||||
}
|
||||
}
|
||||
std::array<evo::Position2, 128> centers{};
|
||||
for (auto& center : centers) {
|
||||
center = {coordinate(), coordinate()};
|
||||
}
|
||||
for (const auto radius : {0U, 16U, 128U, 725U}) {
|
||||
for (int sample = -1; sample < 3; ++sample) {
|
||||
std::uint64_t hash = 14695981039346656037ULL;
|
||||
std::uint64_t matches{};
|
||||
const auto start = std::chrono::steady_clock::now();
|
||||
for (const auto center : centers) {
|
||||
const auto found =
|
||||
evo::query_neighbors(world, center, evo::Millimetres{radius});
|
||||
if (!found) {
|
||||
throw std::runtime_error("benchmark query failed");
|
||||
}
|
||||
matches += found->size();
|
||||
hash = (hash ^ found->size()) * 1099511628211ULL;
|
||||
for (const auto id : *found) {
|
||||
hash = (hash ^ id.value) * 1099511628211ULL;
|
||||
}
|
||||
}
|
||||
const auto elapsed = std::chrono::duration<double, std::milli>(
|
||||
std::chrono::steady_clock::now() - start)
|
||||
.count();
|
||||
if (sample >= 0) {
|
||||
std::cout << "{\"entities\":" << count
|
||||
<< ",\"clustered\":" << (clustered ? "true" : "false")
|
||||
<< ",\"radius_mm\":" << radius << ",\"sample\":" << sample
|
||||
<< ",\"milliseconds\":" << elapsed << ",\"matches\":" << matches
|
||||
<< ",\"checksum\":\"" << std::hex << hash << std::dec << "\"}\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
} catch (...) {
|
||||
return 1;
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
#include "evo/simulation/scheduler.hpp"
|
||||
#include "evo/simulation/spatial.hpp"
|
||||
#include "evo/simulation/world.hpp"
|
||||
#include "evo/telemetry/sinks.hpp"
|
||||
|
||||
@@ -220,6 +221,97 @@ void telemetry_is_host_owned() {
|
||||
CHECK(log.str() == "{\"level\":\"error\",\"event\":\"invalid_snapshot\"}\n");
|
||||
}
|
||||
|
||||
std::vector<evo::EntityId> spatial_reference(const evo::SimulationWorld& world,
|
||||
evo::Position2 center, evo::Millimetres radius) {
|
||||
std::vector<evo::EntityId> result;
|
||||
for (const auto& entity : world.entities()) {
|
||||
auto nearest = std::numeric_limits<std::uint64_t>::max();
|
||||
// Independent oracle: enumerate nine translated images, no minimum-image formula.
|
||||
for (const auto x_shift : {-1024, 0, 1024}) {
|
||||
for (const auto y_shift : {-1024, 0, 1024}) {
|
||||
const auto dx = static_cast<std::int64_t>(entity.position.x.value) + x_shift -
|
||||
static_cast<std::int64_t>(center.x.value);
|
||||
const auto dy = static_cast<std::int64_t>(entity.position.y.value) + y_shift -
|
||||
static_cast<std::int64_t>(center.y.value);
|
||||
nearest = std::min(nearest, static_cast<std::uint64_t>(dx * dx + dy * dy));
|
||||
}
|
||||
}
|
||||
if (nearest <= static_cast<std::uint64_t>(radius.value) * radius.value) {
|
||||
result.push_back(entity.id);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
void spatial_boundaries() {
|
||||
evo::SimulationWorld world{1};
|
||||
const std::array positions{evo::Position2{},
|
||||
evo::Position2{},
|
||||
evo::Position2{{1023}, {0}},
|
||||
evo::Position2{{0}, {1023}},
|
||||
evo::Position2{{1023}, {1023}},
|
||||
evo::Position2{{3}, {4}},
|
||||
evo::Position2{{512}, {512}}};
|
||||
for (const auto position : positions) {
|
||||
CHECK(world.create_entity(position));
|
||||
}
|
||||
const auto expect = [&](std::uint32_t radius, const std::vector<evo::EntityId>& ids) {
|
||||
const auto found = evo::query_neighbors(world, {}, evo::Millimetres{radius});
|
||||
CHECK(found && *found == ids);
|
||||
};
|
||||
expect(0, {{1}, {2}});
|
||||
expect(1, {{1}, {2}, {3}, {4}});
|
||||
expect(2, {{1}, {2}, {3}, {4}, {5}});
|
||||
expect(5, {{1}, {2}, {3}, {4}, {5}, {6}});
|
||||
expect(724, {{1}, {2}, {3}, {4}, {5}, {6}});
|
||||
expect(725, {{1}, {2}, {3}, {4}, {5}, {6}, {7}});
|
||||
expect(std::numeric_limits<std::uint32_t>::max(), {{1}, {2}, {3}, {4}, {5}, {6}, {7}});
|
||||
for (const auto center : {evo::Position2{{1024}, {}}, evo::Position2{{}, {1024}}}) {
|
||||
const auto invalid = evo::query_neighbors(world, center, evo::Millimetres{1});
|
||||
CHECK(!invalid && invalid.error() == evo::Error::invalid_position);
|
||||
CHECK(!evo::query_neighbors(evo::SimulationWorld{0}, center, {}));
|
||||
}
|
||||
const auto empty = evo::query_neighbors(evo::SimulationWorld{0}, {}, {});
|
||||
CHECK(empty && empty->empty());
|
||||
}
|
||||
|
||||
void spatial_properties() {
|
||||
for (std::uint64_t seed = 0; seed < 32; ++seed) {
|
||||
evo::SimulationWorld world{seed};
|
||||
evo::Random random{seed};
|
||||
const auto coordinate = [&] {
|
||||
return evo::Millimetres{static_cast<std::uint32_t>(random.next() % 1024)};
|
||||
};
|
||||
for (int index = 0; index < 127; ++index) {
|
||||
CHECK(world.create_entity({coordinate(), coordinate()}));
|
||||
}
|
||||
for (int phase = 0; phase < 3; ++phase) {
|
||||
const auto before = world.serialize();
|
||||
for (int query = 0; query < 24; ++query) {
|
||||
const evo::Position2 center{coordinate(), coordinate()};
|
||||
const evo::Millimetres radius{static_cast<std::uint32_t>(random.next() % 800)};
|
||||
const auto actual = evo::query_neighbors(world, center, radius);
|
||||
CHECK(actual && *actual == spatial_reference(world, center, radius));
|
||||
CHECK(std::ranges::is_sorted(*actual));
|
||||
}
|
||||
CHECK(world.serialize() == before);
|
||||
if (phase == 0) {
|
||||
const std::array commands{evo::Command{1, evo::RemoveEntity{{1}}},
|
||||
evo::Command{2, evo::CreateEntity{}}};
|
||||
CHECK(evo::advance(world, evo::Tick{1}, commands));
|
||||
} else {
|
||||
auto restored = evo::SimulationWorld::restore(before);
|
||||
CHECK(restored);
|
||||
world = std::move(*restored);
|
||||
}
|
||||
}
|
||||
}
|
||||
const auto full = fixture(1, evo::max_entities);
|
||||
const auto all = evo::query_neighbors(full, {}, {});
|
||||
CHECK(all && all->size() == evo::max_entities);
|
||||
CHECK(all->front().value == 1 && all->back().value == evo::max_entities);
|
||||
}
|
||||
|
||||
void scheduled_order_and_recovery() {
|
||||
for (std::uint64_t seed = 0; seed < 32; ++seed) {
|
||||
auto expected = fixture(seed, 2);
|
||||
@@ -340,7 +432,9 @@ int main(int argc, char**) try {
|
||||
Test{"canonical empty state", wire_layout_and_empty_state},
|
||||
Test{"host telemetry", telemetry_is_host_owned},
|
||||
Test{"scheduled order and recovery", scheduled_order_and_recovery},
|
||||
Test{"atomic scheduler rejection", scheduled_rejection}};
|
||||
Test{"atomic scheduler rejection", scheduled_rejection},
|
||||
Test{"spatial boundaries", spatial_boundaries},
|
||||
Test{"spatial oracle properties", spatial_properties}};
|
||||
int failures = 0;
|
||||
for (const auto& test : tests) {
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user