yams/tests/unit/common/test_rusage_catch2_test.cpp
Trevon 777e35adae
feat(profiling): Tracy for every library, zones on p2p and memory-sync, per-test CPU/RSS (#294)
* feat(profiling): Tracy zones on p2p and memory-sync background paths

Zone the memory-sync cycle (publish, sync, reconcile, apply, backfill,
delete-outbox drain), the p2p reconnect cycle, accept loop, per-session
worker and frame read, and the delta send/apply/bootstrap stages. Plot the
retained session count and pending erase intents. No behavior change; the
macros are no-ops without Tracy.

Signed-off-by: trvon <me@trevon.dev>

* test(infra): per-test-case CPU and peak RSS accounting for Catch2 binaries

A Catch2 listener in the shared test main records getrusage (GetProcessTimes
on Windows) CPU per case and prints the five heaviest cases plus the run's
peak RSS to stderr at run end. YAMS_TEST_RUSAGE=1 adds one line per case.
The measurement helpers are header-only and unit tested.

Signed-off-by: trvon <me@trevon.dev>

* fix(vector): make vector_database.cpp compile with Tracy enabled

The Profiling lane did not build: eight YAMS_PLOT sites in
vector_database.cpp used -> on a Result<T>, and two passed an int that is
ambiguous between Tracy's PlotData overloads. Use .value() and int64_t
casts. No behavior change outside TRACY_ENABLE builds.

Signed-off-by: trvon <me@trevon.dev>

* build: enable Tracy for every yams library in the Profiling lane

Define yams_tracy_args/yams_tracy_deps once in the root meson.build
(Tracy found, optional rocprofiler hooks, link check) and apply them to
every library and executable under src/, replacing the per-library copies
in search, topology, vector, daemon and tests/benchmarks. Empty when
enable-profiling is off.

Make YAMS_PLOT accept any numeric type and fix a Result deref in
search_service so the newly instrumented TUs compile with Tracy.

Signed-off-by: trvon <me@trevon.dev>

---------

Signed-off-by: trvon <me@trevon.dev>
2026-10-14 08:32:56 -06:00

43 lines
1.5 KiB
C++

// Copyright (c) 2025 YAMS Contributors
// SPDX-License-Identifier: GPL-3.0-or-later
#include <catch2/catch_test_macros.hpp>
#include <support/test_rusage.hpp>
#include <cstdint>
#include <string>
#include <vector>
using namespace yams::test::rusage;
TEST_CASE("rusage: maxrss normalisation", "[unit][common][rusage]") {
CHECK(normalizeMaxRss(2048, true) == 2048);
CHECK(normalizeMaxRss(2048, false) == 2048ULL * 1024ULL);
}
TEST_CASE("rusage: CPU delta tracks burned work and is monotonic", "[unit][common][rusage]") {
const Sample before = sampleSelf();
volatile std::uint64_t sink = 0;
for (std::uint64_t i = 0; i < 50'000'000ULL; ++i) {
sink = sink + i;
}
const Sample after = sampleSelf();
CHECK(cpuDeltaMicros(before, after) > 0);
CHECK(cpuDeltaMicros(after, before) == 0);
CHECK(after.peakRssBytes > 0);
CHECK(after.peakRssBytes >= before.peakRssBytes);
}
TEST_CASE("rusage: top-N ordering and formatting", "[unit][common][rusage]") {
std::vector<CaseCost> cases{{"a", 1000}, {"b", 9000}, {"c", 5000}, {"d", 5000}, {"e", 2000}};
const auto top = topByCpu(cases, 3);
REQUIRE(top.size() == 3);
CHECK(top[0].name == "b");
CHECK(top[1].name == "c");
CHECK(top[2].name == "d"); // stable on ties
CHECK(formatTopCpu(top) == "[rusage] top cpu: b 9ms, c 5ms, d 5ms");
CHECK(formatTopCpu({}) == "[rusage] top cpu: (none)");
CHECK(formatPeakRss(84ULL * 1024ULL * 1024ULL) == "[rusage] peak rss: 84 MB");
CHECK(clampName(std::string(100, 'x')).size() == 60);
}