Run raft sim with random seeds over time, but allow a seed to be easily replayed using the RunWithSeed function

This commit is contained in:
Tyler Neely 2022-11-18 10:34:21 +00:00
parent 7115a7e75b
commit 3ad8489735

View File

@ -14,6 +14,7 @@
#include <iostream> #include <iostream>
#include <map> #include <map>
#include <optional> #include <optional>
#include <random>
#include <set> #include <set>
#include <thread> #include <thread>
#include <vector> #include <vector>
@ -242,39 +243,48 @@ std::pair<SimulatorStats, LatencyHistogramSummaries> RunSimulation(SimulatorConf
return std::make_pair(simulator.Stats(), cli_io.ResponseLatencies()); return std::make_pair(simulator.Stats(), cli_io.ResponseLatencies());
} }
int main() { void RunWithSeed(uint64_t seed) {
spdlog::cfg::load_env_levels();
int n_tests = 50;
SimulatorConfig config{ SimulatorConfig config{
.drop_percent = 5, .drop_percent = 5,
.perform_timeouts = true, .perform_timeouts = true,
.scramble_messages = true, .scramble_messages = true,
.rng_seed = 0, .rng_seed = seed,
.start_time = Time::min() + std::chrono::microseconds{256 * 1024}, .start_time = Time::min() + std::chrono::microseconds{256 * 1024},
.abort_time = Time::max(), .abort_time = Time::max(),
}; };
spdlog::info("========================== NEW SIMULATION, replay with RunWithSeed({}) ==========================",
seed);
spdlog::info("\tTime\t\tTerm\tPort\tRole\t\tMessage\n");
auto [sim_stats_1, latency_stats_1] = RunSimulation(config);
spdlog::info("========================== NEW SIMULATION, replay with RunWithSeed({}) ==========================",
seed);
spdlog::info("\tTime\t\tTerm\tPort\tRole\t\tMessage\n");
auto [sim_stats_2, latency_stats_2] = RunSimulation(config);
if (sim_stats_1 != sim_stats_2 || latency_stats_1 != latency_stats_2) {
spdlog::error("simulator stats diverged across runs for test rng_seed: {}", seed);
spdlog::error("run 1 simulator stats: {}", sim_stats_1);
spdlog::error("run 2 simulator stats: {}", sim_stats_2);
spdlog::error("run 1 latency:\n{}", latency_stats_1.SummaryTable());
spdlog::error("run 2 latency:\n{}", latency_stats_2.SummaryTable());
std::terminate();
}
}
int main() {
spdlog::cfg::load_env_levels();
std::random_device random_device;
std::mt19937 generator(random_device());
std::uniform_int_distribution<> distribution;
int n_tests = 50;
for (int i = 0; i < n_tests; i++) { for (int i = 0; i < n_tests; i++) {
spdlog::info("\tTime\t\tTerm\tPort\tRole\t\tMessage\n"); uint64_t seed = distribution(generator);
RunWithSeed(seed);
// this is vital to cause tests to behave differently across runs
config.rng_seed = i;
spdlog::info("========================== NEW SIMULATION SEED {} ==========================", i);
auto [sim_stats_1, latency_stats_1] = RunSimulation(config);
spdlog::info("========================== NEW SIMULATION SEED {} ==========================", i);
auto [sim_stats_2, latency_stats_2] = RunSimulation(config);
if (sim_stats_1 != sim_stats_2 || latency_stats_1 != latency_stats_2) {
spdlog::error("simulator stats diverged across runs for test rng_seed {}", i);
spdlog::error("run 1 simulator stats: {}", sim_stats_1);
spdlog::error("run 2 simulator stats: {}", sim_stats_2);
spdlog::error("run 1 latency:\n{}", latency_stats_1.SummaryTable());
spdlog::error("run 2 latency:\n{}", latency_stats_2.SummaryTable());
std::terminate();
}
} }
spdlog::info("passed {} tests!", n_tests); spdlog::info("passed {} tests!", n_tests);