/
ThruputMeter.cc
86 lines (65 loc) · 2.22 KB
/
ThruputMeter.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
//
// Copyright (C) 2005 OpenSim Ltd.
//
// SPDX-License-Identifier: LGPL-3.0-or-later
//
#include "inet/common/misc/ThruputMeter.h"
namespace inet {
Define_Module(ThruputMeter);
void ThruputMeter::initialize()
{
startTime = par("startTime");
long _batchSize = par("batchSize");
if ((_batchSize < 0) || (((long)(unsigned int)_batchSize) != _batchSize))
throw cRuntimeError("Invalid 'batchSize=%ld' parameter at '%s' module", _batchSize, getFullPath().c_str());
batchSize = (unsigned int)_batchSize;
maxInterval = par("maxInterval");
numPackets = numBits = 0;
intvlStartTime = intvlLastPkTime = 0;
intvlNumPackets = intvlNumBits = 0;
WATCH(numPackets);
WATCH(numBits);
WATCH(intvlStartTime);
WATCH(intvlNumPackets);
WATCH(intvlNumBits);
bitpersecVector.setName("thruput (bit/sec)");
pkpersecVector.setName("packet/sec");
}
void ThruputMeter::handleMessage(cMessage *msg)
{
updateStats(simTime(), PK(msg)->getBitLength());
send(msg, "out");
}
void ThruputMeter::updateStats(simtime_t now, unsigned long bits)
{
numPackets++;
numBits += bits;
// packet should be counted to new interval
if (intvlNumPackets >= batchSize || now - intvlStartTime >= maxInterval)
beginNewInterval(now);
intvlNumPackets++;
intvlNumBits += bits;
intvlLastPkTime = now;
}
void ThruputMeter::beginNewInterval(simtime_t now)
{
simtime_t duration = now - intvlStartTime;
// record measurements
double bitpersec = intvlNumBits / duration.dbl();
double pkpersec = intvlNumPackets / duration.dbl();
bitpersecVector.recordWithTimestamp(intvlStartTime, bitpersec);
pkpersecVector.recordWithTimestamp(intvlStartTime, pkpersec);
// restart counters
intvlStartTime = now; // FIXME this should be *beginning* of tx of this packet, not end!
intvlNumPackets = intvlNumBits = 0;
}
void ThruputMeter::finish()
{
simtime_t duration = simTime() - startTime;
recordScalar("duration", duration);
recordScalar("total packets", numPackets);
recordScalar("total bits", numBits);
recordScalar("avg throughput (bit/s)", numBits / duration.dbl());
recordScalar("avg packets/s", numPackets / duration.dbl());
}
} // namespace inet