fix TrafficGraphData bandwidth calculation (#1618)

* recalculate average bandwidth for given range

* tests for bandwidth calculation
This commit is contained in:
krychlicki 2017-09-12 16:33:48 +02:00 committed by UdjinM6
parent 33e460f306
commit 6ff7b7aa54
5 changed files with 83 additions and 7 deletions

View File

@ -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);
}
}

View File

@ -14,6 +14,9 @@ private Q_SLOTS:
void getRangeTests();
void switchRangeTests();
void clearTests();
void averageBandwidthTest();
void averageBandwidthEvery2EmptyTest();
};

View File

@ -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;
@ -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();

View File

@ -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&);
};

View File

@ -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;
}