Files
aevolution/tests/benchmarks/resolution_benchmark.cpp
T

161 lines
6.5 KiB
C++

#include "../fixtures/resolution_model.hpp"
#include <chrono>
#include <cstdio>
#include <exception>
#include <iostream>
#include <string_view>
namespace {
using namespace evo::fixture;
constexpr std::uint64_t ticks = 10'000;
struct Peaks {
std::size_t rows{};
std::size_t payload{};
std::size_t capacity{};
template <typename T> void observe(const std::vector<T>& values) {
rows = std::max(rows, values.size());
payload = std::max(payload, values.size() * sizeof(T));
capacity = std::max(capacity, values.capacity() * sizeof(T));
}
};
template <typename Run> void observe(Peaks& peak, const Run& run) {
if constexpr (requires { run.members; }) {
peak.observe(run.members);
} else {
peak.observe(run.classes);
}
}
template <typename Run>
void timed(Run run, std::string_view kind, std::uint64_t width, unsigned sample) {
Peaks peak;
observe(peak, run);
const auto start = std::chrono::steady_clock::now();
for (std::uint64_t tick = 0; tick < ticks; ++tick) {
run.step();
observe(peak, run);
}
const auto ns = std::chrono::duration_cast<std::chrono::nanoseconds>(
std::chrono::steady_clock::now() - start)
.count();
const auto result = run.census();
// Sample zero warms up the same workload but is not reported.
if (sample == 0) {
return;
}
std::cout << "{\"kind\":\"timing\",\"tier\":\"" << kind
<< "\",\"supply\":" << run.rule.environment_energy << ",\"width\":" << width
<< ",\"sample\":" << sample << ",\"elapsed_ns\":" << ns
<< ",\"peak_rows\":" << peak.rows << ",\"peak_payload_bytes\":" << peak.payload
<< ",\"peak_capacity_bytes\":" << peak.capacity << ",\"living\":" << result.living
<< ",\"energy\":" << result.energy << ",\"matter_per_channel\":" << result.matter
<< "}\n";
}
bool sensitivity(Rule rule, std::uint64_t width) {
auto individual = founding_individuals(100, rule);
auto aggregate = founding_classes(individual, width);
const auto energy = individual.total_energy();
const auto matter = individual.total_matter();
std::uint64_t max_population_gap = 0;
std::uint64_t max_energy_gap = 0;
std::uint64_t max_matter_gap = 0;
bool false_extinction = false;
for (std::uint64_t tick = 0; tick < ticks; ++tick) {
individual.step();
aggregate.step();
insist(individual.total_energy() == energy && aggregate.total_energy() == energy,
"energy conservation failed");
insist(individual.total_matter() == matter && aggregate.total_matter() == matter,
"matter conservation failed");
insist(representable(aggregate), "unrepresentable aggregate");
const auto a = individual.census();
const auto b = aggregate.census();
max_population_gap = std::max(max_population_gap, gap_per_thousand(a.living, b.living));
max_energy_gap = std::max(max_energy_gap, gap_per_thousand(a.energy, b.energy));
max_matter_gap = std::max(max_matter_gap, gap_per_thousand(a.matter, b.matter));
false_extinction = false_extinction || (a.living > 0 && b.living == 0);
}
const auto a = individual.census();
const auto b = aggregate.census();
const auto population_gap = gap_per_thousand(a.living, b.living);
const auto energy_gap = gap_per_thousand(a.energy, b.energy);
const auto matter_gap = gap_per_thousand(a.matter, b.matter);
const bool passes =
!false_extinction && population_gap <= 100 && energy_gap <= 100 && matter_gap <= 100;
std::cout << "{\"kind\":\"sensitivity\",\"supply\":" << rule.environment_energy
<< ",\"width\":" << width << ",\"individual_living\":" << a.living
<< ",\"aggregate_living\":" << b.living
<< ",\"population_gap_ppt\":" << population_gap
<< ",\"energy_gap_ppt\":" << energy_gap << ",\"matter_gap_ppt\":" << matter_gap
<< ",\"max_population_gap_ppt\":" << max_population_gap
<< ",\"max_energy_gap_ppt\":" << max_energy_gap
<< ",\"max_matter_gap_ppt\":" << max_matter_gap
<< ",\"false_extinction\":" << (false_extinction ? "true" : "false")
<< ",\"research_gate_pass\":" << (passes ? "true" : "false") << "}\n";
return passes;
}
} // namespace
int main(int argc, char** argv) try {
if (argc > 2 || (argc == 2 && std::string_view{argv[1]} != "--verify")) {
std::cerr << "usage: evo-resolution-benchmark [--verify]\n";
return 2;
}
const bool verify = argc == 2;
std::cout
<< "{\"kind\":\"metadata\",\"model\":\"resolution-fixture-2\",\"founders\":100,"
"\"ticks\":10000,\"randomness\":\"none\",\"warmups\":1,\"samples\":3,\"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 width : {10U, 25U, 50U, 100U, 200U}) {
bool all_pass = true;
for (const auto supply : {240U, 300U, 360U}) {
Rule rule;
rule.environment_energy = supply;
all_pass = sensitivity(rule, width) && all_pass;
if (!verify) {
for (unsigned sample = 0; sample <= 3; ++sample) {
auto individual = founding_individuals(100, rule);
auto aggregate = founding_classes(individual, width);
// Alternate order to reduce systematic first-run bias.
if (sample % 2 == 0) {
timed(individual, "individual", width, sample);
timed(aggregate, "aggregate", width, sample);
} else {
timed(aggregate, "aggregate", width, sample);
timed(individual, "individual", width, sample);
}
}
}
}
std::cout << "{\"kind\":\"policy\",\"width\":" << width
<< ",\"passes_all_scenarios\":" << (all_pass ? "true" : "false") << "}\n";
}
return 0;
} catch (const ModelViolation& error) {
std::fprintf(stderr, "%s\n", error.what);
return 1;
} catch (const std::exception& error) {
std::fprintf(stderr, "%s\n", error.what());
return 1;
} catch (...) {
std::fputs("unknown benchmark failure\n", stderr);
return 1;
}