Skip to content

Commit

Permalink
Support sampling histograms
Browse files Browse the repository at this point in the history
  • Loading branch information
kmaragon committed Aug 18, 2022
1 parent c99d8cb commit 0844844
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 17 deletions.
2 changes: 1 addition & 1 deletion conanfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

class CxxmetricsConan(ConanFile):
name = "cxxmetrics"
version = "0.0.7"
version = "0.0.8"
license = "Apache 2.0"
settings = "os"
url = "https://github.com/kmaragon/cxxmetrics"
Expand Down
2 changes: 1 addition & 1 deletion cxxmetrics/ewma.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ TValue ewma<TClockGet, TWindow, TInterval, TValue>::tick(const clock_point &at)
if (missed_intervals > 0)
{
// we missed some intervals - we'll average in zeros
if ((at - last) > period(TWindow) && TWindow > TInterval)
if ((TWindow > TInterval) && (at - last) > period(TWindow))
{
constexpr auto intervals_per_window = TWindow / TInterval;
period::value missed_windows = missed_intervals / intervals_per_window;
Expand Down
9 changes: 0 additions & 9 deletions cxxmetrics/exponential_decay.hpp

This file was deleted.

4 changes: 2 additions & 2 deletions cxxmetrics/metric_value.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -863,9 +863,9 @@ class metric_value
}

template<typename TRep, typename TPer>
operator std::chrono::nanoseconds() const
operator std::chrono::duration<TRep, TPer>() const
{
return value_.to_nanos();
return std::chrono::duration_cast<std::chrono::duration<TRep, TPer>>(value_.to_nanos());
}
};

Expand Down
44 changes: 44 additions & 0 deletions cxxmetrics/snapshots.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,50 @@ class reservoir_snapshot
return values_[index - 1] + metric_value((pos - index) * static_cast<long double>(values_[index] - values_[index - 1]));
}

/**
* \brief Sample n values from the snapshot, calling a function for each
* sampled value
*
* \tparam Handler The handler type
*
* The handler will receive a const metric_value and quantile
*
* \param max_to_visit The maximum number of values
* \param hnd The handler to call for each metric_value in the snapshot
*/
template <typename Handler>
void sample(std::size_t max_to_visit, Handler hnd) const
{
long double q_proportion = 1.0L / std::min(
static_cast<long double>(max_to_visit + 1),
static_cast<long double>(values_.size() - 1));

for (std::size_t i = 0; i < max_to_visit; i++) {
long double q = q_proportion * (i + 1);

auto pos = q * (values_.size() + 1);
auto index = static_cast<std::size_t>(pos);
if (index >= values_.size())
return;

if (index < 1) {
hnd(min(), pos * 100 / static_cast<long double>(values_.size() + 1));
continue;
}

if (index >= values_.size()) {
hnd(max(), pos * 100 / static_cast<long double>(values_.size() + 1));
continue;
}

hnd(values_[index - 1] +
metric_value(
(pos - static_cast<double>(index)) *
static_cast<long double>(values_[index] - values_[index - 1])),
pos * 100 / static_cast<long double>(values_.size() + 1));
}
}

/**
* \brief Get the mean value in the snapshot
*
Expand Down
2 changes: 1 addition & 1 deletion test/conanfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class CxxmetricsTestConan(ConanFile):
settings = "os", "compiler", "build_type", "arch"
generators = "cmake"
exports = "CMakeLists.txt", "../test*"
build_requires = "catch2/2.3.0@bincrafters/stable"
build_requires = "catch2/2.13.9"

def source(self):
tools.replace_in_file("CMakeLists.txt", "project(\"cxxmetrics_test\" CXX)", '''project("cxxmetrics_test" CXX)
Expand Down
8 changes: 6 additions & 2 deletions test/publisher_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,10 @@ TEST_CASE("Publisher can get histogram options", "[publisher]")
{
values.emplace(q, round(v));
});

ss.sample(9, [&](const metric_value& v, quantile q) {
values.emplace(q, round(v));
});
});
};

Expand All @@ -337,7 +341,7 @@ TEST_CASE("Publisher can get histogram options", "[publisher]")

subject.visit_registry(visitor);
REQUIRE(value == metric_value(50));
REQUIRE(values.size() == 3); // defaults
REQUIRE(values.size() == 12); // defaults
REQUIRE(count == 2);

print_percentiles(values);
Expand All @@ -348,6 +352,6 @@ TEST_CASE("Publisher can get histogram options", "[publisher]")

print_percentiles(values);
REQUIRE(value == metric_value(0));
REQUIRE(values.size() == 1);
REQUIRE(values.size() == 10);
REQUIRE(count == 4);
}
2 changes: 1 addition & 1 deletion test/reservoir_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ TEST_CASE("Simple Reservoir threaded updates with snapshots", "[reservoir]")
for (int i = 0; i < 1000; i++)
{
auto s = r.snapshot();
CAPTURE(i)
CAPTURE(i);
REQUIRE(s.size() >= minsize);
}

Expand Down

0 comments on commit 0844844

Please sign in to comment.