feat(bench): benchmark targets, baselines and regression gates (G1, G8)
VoidBuildSystem CI / 3A · Linux test (relwithdebinfo) (push) Failing after 6m43s
VoidBuildSystem CI / 3A · Linux test (debug) (push) Failing after 6m49s
VoidBuildSystem CI / 4A2 · VBS installers (linux target) (push) Skipped
VoidBuildSystem CI / 4A2 · VBS installers (windows target) (push) Skipped
VoidBuildSystem CI / 5 · Release (push) Skipped
VoidBuildSystem CI / 1 · Requirements (push) Successful in 7s
VoidBuildSystem CI / 2A · Linux build (debug) (push) Successful in 2m16s
VoidBuildSystem CI / 2A · Linux build (coverage) (push) Successful in 2m22s
VoidBuildSystem CI / 2A · Linux build (sanitizers) (push) Successful in 3m49s
VoidBuildSystem CI / 2B · Windows Wine build (debug) (push) Successful in 2m1s
VoidBuildSystem CI / 2A · Linux build (relwithdebinfo) (push) Successful in 4m46s
VoidBuildSystem CI / 2B · Windows Wine build (relwithdebinfo) (push) Successful in 3m16s
VoidBuildSystem CI / 3B · Windows Wine test (debug) (push) Successful in 18s
VoidBuildSystem CI / 3A · Linux test (coverage) (push) Failing after 1m4s
VoidBuildSystem CI / 3A · Linux test (sanitizers) (push) Successful in 1m4s
VoidBuildSystem CI / 3B · Windows Wine test (relwithdebinfo) (push) Successful in 17s

- p.benchmark(name) adds the `benchmark` target kind, built and linked
  like an executable (graph lowering, compdb, IDE runnables and the
  installer default destination all handle it).
- vbs bench builds only the benchmark targets (or the --target subset),
  runs each binary --samples times (default 3) with VBS_BENCH=1, keeps the
  best wall-clock time, and compares against --baseline
  (default <build>/.vbs/bench_baseline.json) with --max-regression PCT
  (default 5). The first run records the baseline; --update-baseline
  rewrites it while merging entries for benchmarks not selected.
- Baselines carry os/architecture/compiler/profile; comparison across
  machines warns and skips the gate instead of failing noisy CI.
- --json FILE writes a schema-versioned results document (per-benchmark
  status, samples, baseline, regression percent).
- vbs metadata bumps to schema v2 (new `benchmark` kind, corrected
  `header_only` enum); metadata-v1.schema.json is retained per the
  schema versioning rule, and the schema test fixture now declares a
  benchmark target.

Verified: bench_records_baseline_and_enforces_regression_gate covers
recording, passing, crafted-slow/fast baselines, JSON results,
--update-baseline, machine mismatch, no-benchmark and invalid options;
the schema test passes against v2; vbs_core_tests 8/8; CLI contracts
pass; clang-format clean.
This commit is contained in:
faumaray committed 2026-12-06 20:37:33 +03:00
1 parent f1cf277847
commit 7365279cf3
16 files changed
+921 -25

No files matched your search

+5
View File
@@ -76,6 +76,11 @@ check_process(2 "" "at least one --source is required" fuzz)
check_process(2 "" "invalid --runs value" fuzz --source f.cpp --runs 0)
check_process(2 "" "--coverage-min and --coverage-diff require --coverage" test --coverage-min 80)
check_process(2 "" "invalid --shard value" test --shard 0/2)
check_process(0 "Usage: vbs bench " "" bench --help)
check_process(0 "--max-regression" "" bench --help)
check_process(0 "--update-baseline" "" bench --help)
check_process(2 "" "invalid --max-regression" bench --max-regression abc)
check_process(2 "" "invalid --samples" bench --samples 0)
check_process(2 "" "test-runner options require" build --rerun-failed)
check_process(2 "" "invalid value 'bogus' for '--sandbox'" build --sandbox bogus)
check_process(0 "Usage: vbs new " "" new --help)
+190 -2
View File
@@ -5852,7 +5852,7 @@ VOID_TEST(FullV1Tests, json_schemas_validate_metadata_describe_and_explain) {
Void::Testing::require(parsed.has_value());
return *parsed;
};
const auto metadata_schema = load_schema("metadata-v1.schema.json");
const auto metadata_schema = load_schema("metadata-v2.schema.json");
const auto describe_schema = load_schema("describe-v1.schema.json");
const auto explain_schema = load_schema("explain-v1.schema.json");
@@ -5871,11 +5871,15 @@ void build(Void::Build::Project &p) {
p.test("check")
.cxx_standard(Void::Build::CxxStandard::Cxx26)
.sources({"tests/check.cpp"});
p.benchmark("bench")
.cxx_standard(Void::Build::CxxStandard::Cxx26)
.sources({"benches/bench.cpp"});
}
)cpp");
write_file(proj / "src" / "demo.cpp", "int demo() { return 1; }\n");
write_file(proj / "src" / "main.cpp", "int demo();\nint main() { return demo() == 1 ? 0 : 1; }\n");
write_file(proj / "tests" / "check.cpp", "int main() { return 0; }\n");
write_file(proj / "benches" / "bench.cpp", "int main() { return 0; }\n");
auto run_vbs = [&](std::vector<std::string> args) {
Platform::ProcessRequest req;
@@ -5911,7 +5915,7 @@ void build(Void::Build::Project &p) {
}
Void::Testing::expect(valid);
}
Void::Testing::expect_eq(schema_version(metadata_document), 1LL);
Void::Testing::expect_eq(schema_version(metadata_document), 2LL);
auto describe = run_vbs({"describe", "--project", proj.string(), "--build-dir",
(tmp / "build").string()});
@@ -5986,3 +5990,187 @@ void build(Void::Build::Project &p) {
}
}
// === G1/G8: benchmark targets, baselines and the regression gate ===
VOID_TEST(FullV1Tests, bench_records_baseline_and_enforces_regression_gate) {
const auto vbs_bin = locate_vbs_binary();
Void::Testing::expect(!vbs_bin.empty() && std::filesystem::exists(vbs_bin));
if (vbs_bin.empty() || !std::filesystem::exists(vbs_bin)) {
return;
}
Void::FileSystem::TempDir tmp{"vbs_bench_"};
const auto proj = tmp / "proj";
write_file(proj / "build.cpp", R"cpp(#include <Void/Build.hpp>
void build(Void::Build::Project &p) {
p.benchmark("bench_sleep")
.cxx_standard(Void::Build::CxxStandard::Cxx26)
.sources({"benches/sleep.cpp"});
}
)cpp");
write_file(proj / "benches" / "sleep.cpp", R"cpp(#include <chrono>
#include <thread>
int main() {
std::this_thread::sleep_for(std::chrono::milliseconds(1));
return 0;
}
)cpp");
auto run_vbs = [&](std::vector<std::string> args) {
Platform::ProcessRequest req;
req.executable = vbs_bin;
req.arguments = std::move(args);
req.timeout = std::chrono::seconds{600};
return Platform::run_supervised(req);
};
const auto build_dir = tmp / "build";
auto bench = [&](std::vector<std::string> extra) {
std::vector<std::string> args{"bench", "--project", proj.string(), "--build-dir",
build_dir.string(), "-p", "debug", "--samples", "1"};
args.insert(args.end(), extra.begin(), extra.end());
return run_vbs(std::move(args));
};
const auto baseline_path = build_dir / ".vbs" / "bench_baseline.json";
const auto parse_object = [](const std::string &text) {
auto parsed = Void::Json::Strict::parse(text);
Void::Testing::require(parsed.has_value());
const auto *object = std::get_if<JsonValue::Object>(&parsed->value);
Void::Testing::require(object != nullptr);
return *object;
};
const auto string_field = [](const JsonValue::Object &object, std::string_view key) {
const auto *value = std::get_if<std::string>(&object.at(std::string{key}).value);
Void::Testing::require(value != nullptr);
return *value;
};
// First run: no baseline yet, so it is recorded and the run succeeds.
auto recorded = bench({});
Void::Testing::expect(recorded.has_value() && recorded->succeeded());
auto baseline_text = Void::FileSystem::read_file(baseline_path);
Void::Testing::require(baseline_text.has_value());
const auto recorded_document = parse_object(*baseline_text);
const auto *version = std::get_if<std::int64_t>(
&recorded_document.at("schema_version").value);
Void::Testing::require(version != nullptr);
Void::Testing::expect_eq(*version, 1LL);
const auto *recorded_benchmarks =
std::get_if<JsonValue::Object>(&recorded_document.at("benchmarks").value);
Void::Testing::require(recorded_benchmarks != nullptr);
const auto *recorded_best =
std::get_if<double>(&recorded_benchmarks->at("bench_sleep").value);
Void::Testing::require(recorded_best != nullptr);
Void::Testing::expect(*recorded_best > 0.0);
const std::string recorded_os = string_field(recorded_document, "os");
const std::string recorded_arch = string_field(recorded_document, "architecture");
const std::string recorded_compiler = string_field(recorded_document, "compiler");
const std::string recorded_profile = string_field(recorded_document, "profile");
// A high threshold passes; a crafted low baseline fails the gate.
auto relaxed = bench({"--max-regression", "1000"});
Void::Testing::expect(relaxed.has_value() && relaxed->succeeded());
const auto write_baseline = [&](double best, std::string os) {
JsonValue::Object benchmarks;
benchmarks["bench_sleep"] = JsonValue{best};
JsonValue::Object root;
root["schema_version"] = JsonValue{std::int64_t{1}};
root["os"] = JsonValue{std::move(os)};
root["architecture"] = JsonValue{recorded_arch};
root["compiler"] = JsonValue{recorded_compiler};
root["profile"] = JsonValue{recorded_profile};
root["benchmarks"] = JsonValue{std::move(benchmarks)};
JsonValue document;
document.value = std::move(root);
auto serialized = Void::Json::Strict::serialize(document);
Void::Testing::require(serialized.has_value());
write_file(baseline_path, *serialized + "\n");
};
write_baseline(1e-9, recorded_os);
auto regressed = bench({"--max-regression", "5"});
Void::Testing::expect(regressed.has_value() && regressed->exit_code == 1);
if (regressed) {
const std::string output = regressed->stdout_data + regressed->stderr_data;
Void::Testing::expect(output.find("REGRESSED") != std::string::npos);
Void::Testing::expect(output.find("exceed --max-regression") != std::string::npos);
}
// A generous baseline passes and --json writes the machine-readable result.
write_baseline(1e9, recorded_os);
const auto results_path = tmp / "bench-results.json";
auto passing = bench({"--max-regression", "5", "--json", results_path.string()});
Void::Testing::expect(passing.has_value() && passing->succeeded());
auto results_text = Void::FileSystem::read_file(results_path);
Void::Testing::require(results_text.has_value());
const auto results_document = parse_object(*results_text);
const auto *results_version =
std::get_if<std::int64_t>(&results_document.at("schema_version").value);
Void::Testing::require(results_version != nullptr);
Void::Testing::expect_eq(*results_version, 1LL);
const auto *results_entries =
std::get_if<JsonValue::Array>(&results_document.at("benchmarks").value);
Void::Testing::require(results_entries != nullptr);
Void::Testing::expect_eq(results_entries->size(), std::size_t{1});
const auto *entry = std::get_if<JsonValue::Object>(&results_entries->front().value);
Void::Testing::require(entry != nullptr);
Void::Testing::expect_eq(string_field(*entry, "name"), std::string{"bench_sleep"});
Void::Testing::expect_eq(string_field(*entry, "status"), std::string{"pass"});
const auto *result_regressions =
std::get_if<std::int64_t>(&results_document.at("regressions").value);
Void::Testing::require(result_regressions != nullptr);
Void::Testing::expect_eq(*result_regressions, 0LL);
// --update-baseline rewrites a failing baseline with the measured timing.
write_baseline(1e-9, recorded_os);
auto updated = bench({"--update-baseline"});
Void::Testing::expect(updated.has_value() && updated->succeeded());
auto updated_text = Void::FileSystem::read_file(baseline_path);
Void::Testing::require(updated_text.has_value());
const auto updated_document = parse_object(*updated_text);
const auto *updated_benchmarks =
std::get_if<JsonValue::Object>(&updated_document.at("benchmarks").value);
Void::Testing::require(updated_benchmarks != nullptr);
const auto *updated_best =
std::get_if<double>(&updated_benchmarks->at("bench_sleep").value);
Void::Testing::require(updated_best != nullptr);
Void::Testing::expect(*updated_best > 0.5);
// Baselines from another machine are not comparable: warn but do not fail.
write_baseline(1e-9, "Windows");
auto mismatched = bench({"--max-regression", "5"});
Void::Testing::expect(mismatched.has_value() && mismatched->succeeded());
if (mismatched) {
Void::Testing::expect((mismatched->stdout_data + mismatched->stderr_data)
.find("skipping the regression gate") != std::string::npos);
}
// Without benchmark targets the command is a usage error.
const auto plain = tmp / "plain";
write_file(plain / "build.cpp", R"cpp(#include <Void/Build.hpp>
void build(Void::Build::Project &p) { p.executable("app").sources({"src/main.cpp"}); }
)cpp");
write_file(plain / "src" / "main.cpp", "int main() { return 0; }\n");
auto none = run_vbs({"bench", "--project", plain.string(), "--build-dir",
(tmp / "build-plain").string(), "-p", "debug"});
Void::Testing::expect(none.has_value() && none->exit_code == 2);
if (none) {
Void::Testing::expect(none->stderr_data.find("no benchmark targets") != std::string::npos);
}
// Invalid thresholds are rejected before any build work.
auto bad_threshold =
run_vbs({"bench", "--project", proj.string(), "--build-dir", build_dir.string(),
"--max-regression", "abc"});
Void::Testing::expect(bad_threshold.has_value() && bad_threshold->exit_code == 2);
if (bad_threshold) {
Void::Testing::expect(bad_threshold->stderr_data.find("invalid --max-regression") !=
std::string::npos);
}
auto bad_samples =
run_vbs({"bench", "--project", proj.string(), "--build-dir", build_dir.string(),
"--samples", "0"});
Void::Testing::expect(bad_samples.has_value() && bad_samples->exit_code == 2);
if (bad_samples) {
Void::Testing::expect(bad_samples->stderr_data.find("invalid --samples") !=
std::string::npos);
}
}