Unverified 提交 3cde2cb8 authored 作者: Vittorio Palmisano's avatar Vittorio Palmisano 提交者: GitHub

Optimize rate calculator (v2) (#547)

上级 6f8953a3
...@@ -14,15 +14,17 @@ namespace RTC ...@@ -14,15 +14,17 @@ namespace RTC
public: public:
static constexpr size_t DefaultWindowSize{ 1000u }; static constexpr size_t DefaultWindowSize{ 1000u };
static constexpr float DefaultBpsScale{ 8000.0f }; static constexpr float DefaultBpsScale{ 8000.0f };
static constexpr uint16_t DefaultWindowItems{ 1000u }; static constexpr uint16_t DefaultWindowItems{ 100u };
public: public:
RateCalculator( RateCalculator(
size_t windowSize = DefaultWindowSize, size_t windowSizeMs = DefaultWindowSize,
float scale = DefaultBpsScale, float scale = DefaultBpsScale,
uint16_t windowItems = DefaultWindowItems) uint16_t windowItems = DefaultWindowItems)
: windowSize(windowSize), scale(scale), windowItems(windowItems) : windowSizeMs(windowSizeMs), scale(scale), windowItems(windowItems)
{ {
this->itemSizeMs = std::max(windowSizeMs / windowItems, static_cast<size_t>(1));
Reset(); Reset();
} }
void Update(size_t size, uint64_t nowMs); void Update(size_t size, uint64_t nowMs);
...@@ -43,13 +45,13 @@ namespace RTC ...@@ -43,13 +45,13 @@ namespace RTC
void Reset(uint64_t nowMs) void Reset(uint64_t nowMs)
{ {
this->buffer.reset(new BufferItem[this->windowItems]); this->buffer.reset(new BufferItem[this->windowItems]);
this->newestTime = 0u; this->newestItemStartTime = 0u;
this->newestTimeIndex = -1; this->newestItemIndex = -1;
this->oldestTimeIndex = 0u; this->oldestItemStartTime = 0u;
this->oldestIndex = -1; this->oldestItemIndex = -1;
this->totalCount = 0u; this->totalCount = 0u;
this->lastRate = 0u; this->lastRate = 0u;
this->lastTime = 0u; this->lastTime = 0u;
} }
private: private:
...@@ -61,21 +63,23 @@ namespace RTC ...@@ -61,21 +63,23 @@ namespace RTC
private: private:
// Window Size (in milliseconds). // Window Size (in milliseconds).
size_t windowSize{ DefaultWindowSize }; size_t windowSizeMs{ DefaultWindowSize };
// Scale in which the rate is represented. // Scale in which the rate is represented.
float scale{ DefaultBpsScale }; float scale{ DefaultBpsScale };
// Window Size (number of items). // Window Size (number of items).
uint16_t windowItems{ DefaultWindowItems }; uint16_t windowItems{ DefaultWindowItems };
// Item Size (in milliseconds), calculated as: windowSizeMs / windowItems.
size_t itemSizeMs{ 0u };
// Buffer to keep data. // Buffer to keep data.
std::unique_ptr<BufferItem[]> buffer; std::unique_ptr<BufferItem[]> buffer;
// Time (in milliseconds) for last item in the time window. // Time (in milliseconds) for last item in the time window.
uint64_t newestTime{ 0u }; uint64_t newestItemStartTime{ 0u };
// Index for the last item in the time window. // Index for the last item in the time window.
int32_t newestTimeIndex{ -1 }; int32_t newestItemIndex{ -1 };
// Time (in milliseconds) for oldest item in the time window. // Time (in milliseconds) for oldest item in the time window.
uint64_t oldestTimeIndex{ 0u }; uint64_t oldestItemStartTime{ 0u };
// Index for the oldest item in the time window. // Index for the oldest item in the time window.
int32_t oldestIndex{ -1 }; int32_t oldestItemIndex{ -1 };
// Total count in the time window. // Total count in the time window.
size_t totalCount{ 0u }; size_t totalCount{ 0u };
// Total bytes transmitted. // Total bytes transmitted.
...@@ -89,7 +93,7 @@ namespace RTC ...@@ -89,7 +93,7 @@ namespace RTC
class RtpDataCounter class RtpDataCounter
{ {
public: public:
explicit RtpDataCounter(size_t windowSize = 2500) : rate(windowSize) explicit RtpDataCounter(size_t windowSizeMs = 2500) : rate(windowSizeMs)
{ {
} }
......
...@@ -12,7 +12,7 @@ namespace RTC ...@@ -12,7 +12,7 @@ namespace RTC
MS_TRACE(); MS_TRACE();
// Ignore too old data. Should never happen. // Ignore too old data. Should never happen.
if (nowMs < this->oldestTimeIndex) if (nowMs < this->oldestItemStartTime)
return; return;
// Increase bytes. // Increase bytes.
...@@ -20,38 +20,49 @@ namespace RTC ...@@ -20,38 +20,49 @@ namespace RTC
RemoveOldData(nowMs); RemoveOldData(nowMs);
// Increase the index. // If the elapsed time from the newest item start time is greater than the
if (++this->newestTimeIndex >= this->windowItems) // item size (in milliseconds), increase the item index.
this->newestTimeIndex = 0; if (this->newestItemIndex < 0 || nowMs - this->newestItemStartTime >= this->itemSizeMs)
// Newest index overlaps with the oldest one, remove it.
if (this->newestTimeIndex == this->oldestIndex && this->oldestIndex != -1)
{ {
MS_WARN_TAG( this->newestItemIndex++;
info, this->newestItemStartTime = nowMs;
"calculation buffer full, windowSize:%zu ms windowItems:%" PRIu16, if (this->newestItemIndex >= this->windowItems)
this->windowSize, this->newestItemIndex = 0;
this->windowItems);
// Newest index overlaps with the oldest one, remove it.
BufferItem& oldestItem = buffer[this->oldestIndex]; if (this->newestItemIndex == this->oldestItemIndex && this->oldestItemIndex != -1)
this->totalCount -= oldestItem.count; {
oldestItem.count = 0u; MS_WARN_TAG(
oldestItem.time = 0u; info,
if (++this->oldestIndex >= this->windowItems) "calculation buffer full, windowSizeMs:%zu ms windowItems:%" PRIu16,
this->oldestIndex = 0; this->windowSizeMs,
this->windowItems);
BufferItem& oldestItem = buffer[this->oldestItemIndex];
this->totalCount -= oldestItem.count;
oldestItem.count = 0u;
oldestItem.time = 0u;
if (++this->oldestItemIndex >= this->windowItems)
this->oldestItemIndex = 0;
}
// Set the newest item.
BufferItem& item = buffer[this->newestItemIndex];
item.count = size;
item.time = nowMs;
}
else
{
// Update the newest item.
BufferItem& item = buffer[this->newestItemIndex];
item.count += size;
} }
// Update the newest item.
BufferItem& item = buffer[this->newestTimeIndex];
item.count += size;
item.time = nowMs;
this->newestTime = nowMs;
// Set the oldest item index and time, if not set. // Set the oldest item index and time, if not set.
if (this->oldestIndex < 0) if (this->oldestItemIndex < 0)
{ {
this->oldestIndex = this->newestTimeIndex; this->oldestItemIndex = this->newestItemIndex;
this->oldestTimeIndex = nowMs; this->oldestItemStartTime = nowMs;
} }
this->totalCount += size; this->totalCount += size;
...@@ -71,7 +82,7 @@ namespace RTC ...@@ -71,7 +82,7 @@ namespace RTC
RemoveOldData(nowMs); RemoveOldData(nowMs);
float scale = this->scale / this->windowSize; float scale = this->scale / this->windowSizeMs;
this->lastTime = nowMs; this->lastTime = nowMs;
this->lastRate = static_cast<uint32_t>(std::trunc(this->totalCount * scale + 0.5f)); this->lastRate = static_cast<uint32_t>(std::trunc(this->totalCount * scale + 0.5f));
...@@ -83,36 +94,36 @@ namespace RTC ...@@ -83,36 +94,36 @@ namespace RTC
{ {
MS_TRACE(); MS_TRACE();
// No item set // No item set.
if (this->newestTimeIndex < 0 || this->oldestIndex < 0) if (this->newestItemIndex < 0 || this->oldestItemIndex < 0)
return; return;
uint64_t newoldestTimeIndex = nowMs - this->windowSize; uint64_t newoldestTime = nowMs - this->windowSizeMs;
// Oldest item already removed. // Oldest item already removed.
if (newoldestTimeIndex <= this->oldestTimeIndex) if (newoldestTime <= this->oldestItemStartTime)
return; return;
// A whole window size time has elapsed since last entry. Reset the buffer. // A whole window size time has elapsed since last entry. Reset the buffer.
if (newoldestTimeIndex > this->newestTime) if (newoldestTime > this->newestItemStartTime)
{ {
Reset(nowMs); Reset(nowMs);
return; return;
} }
while (this->oldestTimeIndex < newoldestTimeIndex) while (this->oldestItemStartTime < newoldestTime)
{ {
BufferItem& oldestItem = buffer[this->oldestIndex]; BufferItem& oldestItem = buffer[this->oldestItemIndex];
this->totalCount -= oldestItem.count; this->totalCount -= oldestItem.count;
oldestItem.count = 0u; oldestItem.count = 0u;
oldestItem.time = 0u; oldestItem.time = 0u;
if (++this->oldestIndex >= this->windowItems) if (++this->oldestItemIndex >= this->windowItems)
this->oldestIndex = 0; this->oldestItemIndex = 0;
const BufferItem& newOldestItem = buffer[this->oldestIndex]; const BufferItem& newOldestItem = buffer[this->oldestItemIndex];
this->oldestTimeIndex = newOldestItem.time; this->oldestItemStartTime = newOldestItem.time;
} }
} }
......
...@@ -61,7 +61,7 @@ SCENARIO("Bitrate calculator", "[rtp][bitrate]") ...@@ -61,7 +61,7 @@ SCENARIO("Bitrate calculator", "[rtp][bitrate]")
SECTION("slide") SECTION("slide")
{ {
RateCalculator rate; RateCalculator rate(1000, 8000, 1000);
// clang-format off // clang-format off
std::vector<data> input = std::vector<data> input =
...@@ -79,23 +79,46 @@ SCENARIO("Bitrate calculator", "[rtp][bitrate]") ...@@ -79,23 +79,46 @@ SCENARIO("Bitrate calculator", "[rtp][bitrate]")
REQUIRE(rate.GetRate(nowMs + 3001) == 0); REQUIRE(rate.GetRate(nowMs + 3001) == 0);
} }
SECTION("slide with 100 items")
{
RateCalculator rate(1000, 8000, 100);
// clang-format off
std::vector<data> input =
{
{ 0, 5, 40 },
{ 999, 2, 56 },
{ 1001, 1, 24 }, // merged inside 999
{ 1001, 1, 32 }, // merged inside 999
{ 2000, 1, 8 } // it will erase the item with timestamp=999,
// removing also the next two samples.
// The end estimation will include only the last sample.
};
// clang-format on
validate(rate, nowMs, input);
REQUIRE(rate.GetRate(nowMs + 3001) == 0);
}
SECTION("wrap") SECTION("wrap")
{ {
// window: 1000ms, items: 5 (granularity: 200ms)
RateCalculator rate(1000, 8000, 5); RateCalculator rate(1000, 8000, 5);
// clang-format off // clang-format off
std::vector<data> input = std::vector<data> input =
{ {
{ 1000, 1, 1*8 }, { 1000, 1, 1*8 },
{ 1001, 1, 1*8 + 1*8 }, { 1200, 1, 1*8 + 1*8 },
{ 1002, 1, 1*8 + 2*8 }, { 1400, 1, 1*8 + 2*8 },
{ 1003, 1, 1*8 + 3*8 }, { 1600, 1, 1*8 + 3*8 },
{ 1004, 1, 1*8 + 4*8 }, { 1800, 1, 1*8 + 4*8 },
{ 1005, 1, 1*8 + (5-1)*8 }, // starts wrap here { 2000, 1, 1*8 + (5-1)*8 }, // starts wrap here
{ 1006, 1, 1*8 + (6-2)*8 }, { 2200, 1, 1*8 + (6-2)*8 },
{ 1007, 1, 1*8 + (7-3)*8 }, { 2400, 1, 1*8 + (7-3)*8 },
{ 1008, 1, 1*8 + (8-4)*8 }, { 2600, 1, 1*8 + (8-4)*8 },
{ 1009, 1, 1*8 + (9-5)*8 }, { 2800, 1, 1*8 + (9-5)*8 },
}; };
// clang-format on // clang-format on
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论