From 9d2100a165bcdbadf23e56791e462c5531c68639 Mon Sep 17 00:00:00 2001 From: Kamil Monicz Date: Thu, 18 Dec 2025 03:34:32 +0000 Subject: [PATCH] libstore-tests: benchmark registerValidPaths on derivations - Add a focused nix-store-benchmarks benchmark that registers many derivation paths into a temporary local store root --- src/libstore-tests/meson.build | 1 + .../register-valid-paths-bench.cc | 79 +++++++++++++++++++ 2 files changed, 80 insertions(+) create mode 100644 src/libstore-tests/register-valid-paths-bench.cc diff --git a/src/libstore-tests/meson.build b/src/libstore-tests/meson.build index 123bccb90..2d12b14d3 100644 --- a/src/libstore-tests/meson.build +++ b/src/libstore-tests/meson.build @@ -124,6 +124,7 @@ if get_option('benchmarks') 'bench-main.cc', 'derivation-parser-bench.cc', 'ref-scan-bench.cc', + 'register-valid-paths-bench.cc', ) benchmark_exe = executable( diff --git a/src/libstore-tests/register-valid-paths-bench.cc b/src/libstore-tests/register-valid-paths-bench.cc new file mode 100644 index 000000000..bd8cb113b --- /dev/null +++ b/src/libstore-tests/register-valid-paths-bench.cc @@ -0,0 +1,79 @@ +#include + +#include "nix/store/derivations.hh" +#include "nix/store/local-store.hh" +#include "nix/store/store-open.hh" +#include "nix/util/file-system.hh" +#include "nix/util/hash.hh" +#include "nix/util/tests/test-data.hh" + +#ifndef _WIN32 + +# include +# include + +using namespace nix; + +static void BM_RegisterValidPathsDerivations(benchmark::State & state) +{ + const int derivationCount = state.range(0); + + for (auto _ : state) { + state.PauseTiming(); + + auto tmpRoot = createTempDir(); + auto realStoreDir = tmpRoot / "nix/store"; + std::filesystem::create_directories(realStoreDir); + + std::shared_ptr store = openStore(fmt("local?root=%s", tmpRoot.string())); + auto localStore = std::dynamic_pointer_cast(store); + if (!localStore) + throw Error("expected local store"); + + ValidPathInfos infos; + for (int i = 0; i < derivationCount; ++i) { + std::string drvName = fmt("register-valid-paths-bench-%d", i); + auto drvPath = StorePath::random(drvName + ".drv"); + + Derivation drv; + drv.name = drvName; + drv.outputs.emplace("out", DerivationOutput{DerivationOutput::Deferred{}}); + drv.platform = "x86_64-linux"; + drv.builder = "foo"; + drv.env["out"] = ""; + drv.fillInOutputPaths(*localStore); + + auto drvContents = drv.unparse(*localStore, /*maskOutputs=*/false); + + /* Create an on-disk store object without registering it + in the SQLite DB. LocalFSStore::getFSAccessor(path, false) + allows reading store objects based on their filesystem + presence alone. */ + std::ofstream out(realStoreDir / std::string(drvPath.to_string()), std::ios::binary); + out.write(drvContents.data(), drvContents.size()); + if (!out) + throw SysError("writing derivation to store"); + + ValidPathInfo info{drvPath, UnkeyedValidPathInfo(*localStore, Hash::dummy)}; + info.narSize = drvContents.size(); + + infos.emplace(drvPath, std::move(info)); + } + + state.ResumeTiming(); + + localStore->registerValidPaths(infos); + + state.PauseTiming(); + localStore.reset(); + store.reset(); + std::filesystem::remove_all(tmpRoot); + state.ResumeTiming(); + } + + state.SetItemsProcessed(state.iterations() * derivationCount); +} + +BENCHMARK(BM_RegisterValidPathsDerivations)->Arg(10)->Arg(50)->Arg(200); + +#endif