diff --git a/src/qt/test/trafficgraphdatatests.cpp b/src/qt/test/trafficgraphdatatests.cpp index af4d4f99d..37e05acc5 100644 --- a/src/qt/test/trafficgraphdatatests.cpp +++ b/src/qt/test/trafficgraphdatatests.cpp @@ -172,3 +172,52 @@ void TrafficGraphDataTests::clearTests() } QCOMPARE(trafficGraphData.getCurrentRangeQueue().size(), TrafficGraphData::DESIRED_DATA_SAMPLES); } + +void TrafficGraphDataTests::averageBandwidthTest() +{ + TrafficGraphData trafficGraphData(TrafficGraphData::Range_5m); + int step = 384; + quint64 totalBytesRecv = 0; + quint64 totalBytesSent = 0; + for (int i = 1; i <= TrafficGraphData::DESIRED_DATA_SAMPLES; i++){ + totalBytesRecv += step; + totalBytesSent += step; + trafficGraphData.update(totalBytesRecv, totalBytesSent); + } + QCOMPARE(trafficGraphData.getCurrentRangeQueueWithAverageBandwidth().size(), TrafficGraphData::DESIRED_DATA_SAMPLES); + for(auto& sample : trafficGraphData.getCurrentRangeQueueWithAverageBandwidth()){ + QCOMPARE(sample.in, 1.0); + QCOMPARE(sample.out, 1.0); + } + + trafficGraphData.switchRange(TrafficGraphData::Range_10m); + + QCOMPARE(trafficGraphData.getCurrentRangeQueueWithAverageBandwidth().size(), TrafficGraphData::DESIRED_DATA_SAMPLES / 2); + for(auto& sample : trafficGraphData.getCurrentRangeQueueWithAverageBandwidth()){ + QCOMPARE(sample.in, 1.0); + QCOMPARE(sample.out, 1.0); + } +} + +void TrafficGraphDataTests::averageBandwidthEvery2EmptyTest() +{ + TrafficGraphData trafficGraphData(TrafficGraphData::Range_5m); + int step = 384; + quint64 totalBytesRecv = 0; + quint64 totalBytesSent = 0; + for (int i = 1; i <= TrafficGraphData::DESIRED_DATA_SAMPLES; i++){ + if (i % 2 == 0) { + totalBytesRecv += step; + totalBytesSent += step; + } + trafficGraphData.update(totalBytesRecv, totalBytesSent); + } + + trafficGraphData.switchRange(TrafficGraphData::Range_10m); + + QCOMPARE(trafficGraphData.getCurrentRangeQueueWithAverageBandwidth().size(), TrafficGraphData::DESIRED_DATA_SAMPLES / 2); + for(auto& sample : trafficGraphData.getCurrentRangeQueueWithAverageBandwidth()){ + QCOMPARE(sample.in, 0.5); + QCOMPARE(sample.out, 0.5); + } +} diff --git a/src/qt/test/trafficgraphdatatests.h b/src/qt/test/trafficgraphdatatests.h index 06a27d0c0..a9fc8ff5a 100644 --- a/src/qt/test/trafficgraphdatatests.h +++ b/src/qt/test/trafficgraphdatatests.h @@ -14,6 +14,9 @@ private Q_SLOTS: void getRangeTests(); void switchRangeTests(); void clearTests(); + void averageBandwidthTest(); + void averageBandwidthEvery2EmptyTest(); + }; diff --git a/src/qt/trafficgraphdata.cpp b/src/qt/trafficgraphdata.cpp index 21129a257..d14fee1c7 100644 --- a/src/qt/trafficgraphdata.cpp +++ b/src/qt/trafficgraphdata.cpp @@ -1,4 +1,4 @@ -#include +#include "trafficgraphdata.h" const int TrafficGraphData::RangeMinutes[] = {5,10,15,30,60,120,180,360,720,1440}; const int TrafficGraphData::DESIRED_DATA_SAMPLES = 800; @@ -26,7 +26,7 @@ TrafficGraphData::TrafficGraphData(GraphRange range) { } - void TrafficGraphData::tryAddingSampleToStash(GraphRange range) +void TrafficGraphData::tryAddingSampleToStash(GraphRange range) { SampleQueue& queue = sampleMap[range]; if (queue.size() > DesiredQueueSizes[range]){ @@ -58,8 +58,8 @@ void TrafficGraphData::setLastBytes(quint64 nLastBytesIn, quint64 nLastBytesOut) bool TrafficGraphData::update(quint64 totalBytesRecv, quint64 totalBytesSent) { - float inRate = (totalBytesRecv - nLastBytesIn) / 1024.0f * 1000 / SMALLEST_SAMPLE_PERIOD; - float outRate = (totalBytesSent - nLastBytesOut) / 1024.0f * 1000 / SMALLEST_SAMPLE_PERIOD; + float inRate = totalBytesRecv - nLastBytesIn ; + float outRate = totalBytesSent - nLastBytesOut; nLastBytesIn = totalBytesRecv; nLastBytesOut = totalBytesSent; return update(TrafficSample(inRate,outRate)); @@ -236,6 +236,29 @@ TrafficGraphData::SampleQueue TrafficGraphData::getCurrentRangeQueue() return newQueue; } + +float TrafficGraphData::converSampletoBandwith(float dataAmount){ + // to base range + float result = dataAmount / RangeMinutes[currentGraphRange] * RangeMinutes[TrafficGraphData::Range_5m]; + // to B/s + result = result * 1000 / SMALLEST_SAMPLE_PERIOD; + // to KB/s + result = result / 1024; + + return result; + +} + +TrafficGraphData::SampleQueue TrafficGraphData::getCurrentRangeQueueWithAverageBandwidth(){ + SampleQueue newQueue; + getRangeQueue(currentGraphRange).mid(0,DESIRED_DATA_SAMPLES).swap(newQueue); + for(auto& sample : newQueue){ + sample.in = converSampletoBandwith(sample.in); + sample.out = converSampletoBandwith(sample.out); + } + return newQueue; +} + void TrafficGraphData::clear() { sampleMap.clear(); diff --git a/src/qt/trafficgraphdata.h b/src/qt/trafficgraphdata.h index 6c9fbd078..db1f034ae 100644 --- a/src/qt/trafficgraphdata.h +++ b/src/qt/trafficgraphdata.h @@ -58,6 +58,7 @@ public: void switchRange(GraphRange newRange); SampleQueue getRangeQueue(GraphRange range); SampleQueue getCurrentRangeQueue(); + SampleQueue getCurrentRangeQueueWithAverageBandwidth(); void clear(); void setLastBytes(quint64 nLastBytesIn, quint64 nLastBytesOut); @@ -82,7 +83,7 @@ private: SampleQueue sumEach2Samples(const SampleQueue &rangeQueue); SampleQueue sumEach3Samples(const SampleQueue &rangeQueue, GraphRange range); - + float converSampletoBandwith(float dataAmount); TrafficGraphData(const TrafficGraphData& that); TrafficGraphData& operator=(TrafficGraphData const&); }; diff --git a/src/qt/trafficgraphwidget.cpp b/src/qt/trafficgraphwidget.cpp index 89b98356a..bf89b928a 100644 --- a/src/qt/trafficgraphwidget.cpp +++ b/src/qt/trafficgraphwidget.cpp @@ -115,7 +115,7 @@ void TrafficGraphWidget::paintEvent(QPaintEvent *) } } - const TrafficGraphData::SampleQueue& queue = trafficGraphData.getCurrentRangeQueue(); + const TrafficGraphData::SampleQueue& queue = trafficGraphData.getCurrentRangeQueueWithAverageBandwidth(); if(!queue.empty()) { QPainterPath pIn; @@ -140,7 +140,7 @@ void TrafficGraphWidget::updateRates() if (updated){ float tmax = DEFAULT_SAMPLE_HEIGHT; - Q_FOREACH(const TrafficSample& sample, trafficGraphData.getCurrentRangeQueue()) { + Q_FOREACH(const TrafficSample& sample, trafficGraphData.getCurrentRangeQueueWithAverageBandwidth()) { if(sample.in > tmax) tmax = sample.in; if(sample.out > tmax) tmax = sample.out; }