Skip to content

Commit

Permalink
fix TrafficGraphData bandwidth calculation (#1618)
Browse files Browse the repository at this point in the history
* recalculate average bandwidth for given range

* tests for bandwidth calculation
  • Loading branch information
krychlicki authored and UdjinM6 committed Sep 12, 2017
1 parent 33e460f commit 6ff7b7a
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 7 deletions.
49 changes: 49 additions & 0 deletions src/qt/test/trafficgraphdatatests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
3 changes: 3 additions & 0 deletions src/qt/test/trafficgraphdatatests.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ private Q_SLOTS:
void getRangeTests();
void switchRangeTests();
void clearTests();
void averageBandwidthTest();
void averageBandwidthEvery2EmptyTest();

};


Expand Down
31 changes: 27 additions & 4 deletions src/qt/trafficgraphdata.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include <trafficgraphdata.h>
#include "trafficgraphdata.h"

const int TrafficGraphData::RangeMinutes[] = {5,10,15,30,60,120,180,360,720,1440};
const int TrafficGraphData::DESIRED_DATA_SAMPLES = 800;
Expand Down Expand Up @@ -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]){
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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();
Expand Down
3 changes: 2 additions & 1 deletion src/qt/trafficgraphdata.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ class TrafficGraphData
void switchRange(GraphRange newRange);
SampleQueue getRangeQueue(GraphRange range);
SampleQueue getCurrentRangeQueue();
SampleQueue getCurrentRangeQueueWithAverageBandwidth();
void clear();
void setLastBytes(quint64 nLastBytesIn, quint64 nLastBytesOut);

Expand All @@ -82,7 +83,7 @@ class TrafficGraphData
SampleQueue sumEach2Samples(const SampleQueue &rangeQueue);
SampleQueue sumEach3Samples(const SampleQueue &rangeQueue, GraphRange range);


float converSampletoBandwith(float dataAmount);
TrafficGraphData(const TrafficGraphData& that);
TrafficGraphData& operator=(TrafficGraphData const&);
};
Expand Down
4 changes: 2 additions & 2 deletions src/qt/trafficgraphwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down

0 comments on commit 6ff7b7a

Please sign in to comment.