Skip to content

Commit

Permalink
Add merge and reserve function to MetricValue (#315)
Browse files Browse the repository at this point in the history
Summary:

Add merge and reserve function to MetricValue. Merge allow concatenation of other MetricValues.

Reviewed By: bigzachattack

Differential Revision: D64163319
  • Loading branch information
briancoutinho authored and facebook-github-bot committed Oct 11, 2024
1 parent d195bbc commit 5bb572b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
9 changes: 9 additions & 0 deletions dynolog/src/metric_frame/MetricValues.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ class MetricValues final {
return *(begin() + idx);
}

void reserve(const size_t size) {
data_.reserve(size);
}

void resize(const size_t size) {
data_.resize(size);
}
Expand Down Expand Up @@ -77,6 +81,11 @@ class MetricValues final {
return std::make_pair(*min_it, *max_it);
}

void merge(const MetricValues<T>& other) {
data_.reserve(data_.size() + other.data_.size());
data_.insert(data_.end(), other.data_.begin(), other.data_.end());
}

private:
auto begin() const {
return data_.begin();
Expand Down
19 changes: 19 additions & 0 deletions dynolog/tests/metric_frame/MetricValuesTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,22 @@ TEST(MetricValuesTest, aggTest) {
EXPECT_TRUE(p50Maybe);
EXPECT_EQ(p50Maybe.value(), 2.0);
}

TEST(MetricValuesTest, mergeTest) {
MetricValues<uint32_t> u, v;
for (int i = 0; i < 5; i++) {
u.push_back(i);
}
for (int i = 0; i < 5; i++) {
v.push_back(i);
}
EXPECT_EQ(u.size(), 5);

u.merge(v);
EXPECT_EQ(u.size(), 10);
EXPECT_EQ(u.sum(), 20);

auto avgMaybe = u.avg();
EXPECT_TRUE(avgMaybe);
EXPECT_EQ(avgMaybe.value(), 2.0);
}

0 comments on commit 5bb572b

Please sign in to comment.