Skip to content

Commit

Permalink
fixed MovingAverageStat when value < old_value (#865)
Browse files Browse the repository at this point in the history
  • Loading branch information
cxxrocks authored and lodoyun committed Apr 25, 2017
1 parent e483800 commit ee6f252
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
9 changes: 7 additions & 2 deletions erizo/src/erizo/stats/StatNode.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "stats/StatNode.h"

#include <cmath>
#include <sstream>
#include <string>
#include <memory>
Expand Down Expand Up @@ -249,7 +250,7 @@ StatNode& MovingAverageStat::operator+=(uint64_t value) {
}

uint64_t MovingAverageStat::value() {
return static_cast<uint64_t>(current_average_);
return static_cast<uint64_t>(std::round(current_average_));
}

uint64_t MovingAverageStat::value(uint32_t sample_number) {
Expand All @@ -266,7 +267,11 @@ void MovingAverageStat::add(uint64_t value) {
/ (next_sample_position_ + 1);
} else {
uint64_t old_value = (*sample_vector_.get())[next_sample_position_ % window_size_];
current_average_ = current_average_ + static_cast<double>(value - old_value) / window_size_;
if (value > old_value) {
current_average_ = current_average_ + static_cast<double>(value - old_value) / window_size_;
} else {
current_average_ = current_average_ - static_cast<double>(old_value - value) / window_size_;
}
}

(*sample_vector_.get())[next_sample_position_ % window_size_] = value;
Expand Down
30 changes: 28 additions & 2 deletions erizo/src/test/stats/MovingAverageStatTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ TEST_F(MovingAverageStatTest, shouldCalculateAverageForLessSamplesThanWindowSize
EXPECT_EQ(moving_average_stat.value(), calculated_average);
}

TEST_F(MovingAverageStatTest, shouldCalculateAverageForLessSamplesThanWindowSize2) {
moving_average_stat+=kArbitraryNumberToAdd;
moving_average_stat+=kArbitraryNumberToAdd - 1;
moving_average_stat+=kArbitraryNumberToAdd - 2;
uint32_t calculated_average = (3*kArbitraryNumberToAdd - 3)/3;
EXPECT_EQ(moving_average_stat.value(), calculated_average);
}

TEST_F(MovingAverageStatTest, shouldCalculateAverageForValuesInWindow) {
for (uint i = 0; i < kArbitraryWindowSize; i++) {
moving_average_stat+=kArbitraryNumberToAdd;
Expand All @@ -52,10 +60,28 @@ TEST_F(MovingAverageStatTest, shouldCalculateAverageForValuesInWindow) {
EXPECT_EQ(moving_average_stat.value(), kArbitraryNumberToAdd + 1);
}

TEST_F(MovingAverageStatTest, shouldCalculateAverageForValuesInWindow2) {
for (uint i = 0; i < kArbitraryWindowSize; i++) {
moving_average_stat+=kArbitraryNumberToAdd;
}
for (uint i = 0; i < kArbitraryWindowSize; i++) {
moving_average_stat+=kArbitraryNumberToAdd - 1;
}
EXPECT_EQ(moving_average_stat.value(), kArbitraryNumberToAdd - 1);
}

TEST_F(MovingAverageStatTest, shouldCalculateAverageForGivenNumberOfSamples) {
for (uint i = 0; i < kArbitraryWindowSize; i++) {
moving_average_stat+=i;
}
EXPECT_EQ(moving_average_stat.value(1), 4u);
EXPECT_EQ(moving_average_stat.value(2), uint((4 + 3) / 2));
EXPECT_EQ(moving_average_stat.value(1), kArbitraryWindowSize - 1);
EXPECT_EQ(moving_average_stat.value(2), uint((kArbitraryWindowSize * 2 - 3) / 2));
}

TEST_F(MovingAverageStatTest, shouldCalculateAverageForGivenNumberOfSamples2) {
for (uint i = kArbitraryWindowSize; i > 0; i--) {
moving_average_stat+=i;
}
EXPECT_EQ(moving_average_stat.value(1), 1u);
EXPECT_EQ(moving_average_stat.value(2), uint((1 + 2) / 2));
}

0 comments on commit ee6f252

Please sign in to comment.