# [ns] Calculate Average queue delay and average queue size - Explained

```Dear ns-community,

I probably have found a way to answer my own question which was posted
previously concerning the calculation of average queue delay and size with
monitor objects.```
```
After thorough study, I eventually found out what one has to do in order
to calculate the average queue delay and average queue size (i.e. without
extra-tcl code
or awk scripts) through a monitor object. The code to do that has already
been implemented
in ns-2, in monitor objects.

However, please note that the methods below correspond to my observations
and effort and thereby
I do not guarantee that there are 100% error-free, since I might have missed
something, or understood
something wrong. On the other hand, after the experiments that I carried out
with some tcl scripts,
it seems that the monitor objects along with these methods work OK. The
experiments were carried out for wired links and with no
line-failures (the monitor objects in ns present problems in the calculation
of the queue statistics when a line
drops and comes up again - it seems that all counters get reset!).

The NS-2 version I use is ns-2.29 under Debian Sarge

For the examples below I am assuming a 3-node topology with links
in-between:

node1 -> node2 -> node3

The link characteristics (propagation delay, bandwidth, etc) and the queue
management (droptail, RED, etc) may be as one desires

*********** CALCULATION OF AVERAGE QUEUE SIZE **********************

After the creation of the links and the nodes, we are going to measure the
average queue
size of the connection from node2 to node3. Instead of using a tcl procedure
that is executed
every n seconds, we are going to use the Integrator objects that ns-2
already supports under the monitor objects.

At first we create the monitor object as usual, for the queue that we want
to monitor:

set monitorN2_N3 [\$ns monitor-queue \$node2 \$node3 stdout]

then we "get" the integrator object through the monitor object for the
queue. This method returns an
integrator object which later can be queried for the average queue size in
packets (Integrator objects calculate
integrals of samples over time):

set inetgratorObjectN2_N3 [\$monitorN2_N3 get-pkts-integrator] (or
[\$monitorN2_N3 get-bytes-integrator] for average size in bytes)

and finally before the end of our simulation we print out the result with:

puts stdout "Average queue size: [expr {[\$integratorObjectN2_N3 set
sum_]/\$finishTime}]"

where sum_ is a property of the integrator object that contains the integral
sum of all the sample points from the start of the
simulation until the time we "get" it (finishTime).

finishTime is a custom variable containing the finish time of our simulation
(100,200 seconds, etc).
We have to divide it in order to get the average over all the simulated
time.

***************************** CALCULATING AVERAGE QUEUE DELAY
***********************************************

In order to calculate the average queue delay of a queue with a monitor
object, we can use a Samples object that accompanies
the monitor object, which simply uses the timestamp field of a special
solely used for this purpose (i.e. calculate statistics).

Below I include the code for the average queue size as well

At first we create a samples object that will be passed to the monitor
object later:

set monitorN2_N3 [\$ns monitor-queue \$node2 \$node3 stdout]
set inetgratorObjectN2_N3 [\$monitorN2_N3 get-pkts-integrator]

set samples_object [new Samples]

Then we must "set it up" to be used with our monitor object:

\$monitorN2_N3 set-delay-samples \$samples_object

Then we "get" it through the get method of our monitor object (as we have
done for the monitor object)

set delaySamplesObject [\$monitorN2_N3 get-delay-samples]

And then at the end of the simulation we call the "mean" method of the
samples object (that has been assigned to our queue)
to get the mean delay of the queue in seconds:

puts stdout "Average Queue Delay: [\$delaySamplesObject mean]"

There are other methods to use with the samples object, such as variance,
methods one might check the "Mathematical Support" chapter 24 of the ns-2
manual.

Also, there are set-pkts-integrator and set-bytes-integrator methods as well
for setting up the integrator objects as well.
However, it is not necessary one to use them, since their corresponding
"Samples" objects are being created through
init-monitor during the initialization of the monitor object. Only the delay
samples is not being initialized and not calling
the method set-delay-samples with an existing samples object returns an
error.

Also, please note that the above might have alradey been documented as such
somewhere else beyond my knowledge,
but since I have seen this question many times in the forum I decided to
post an answer in the forum. My apologies
for any inconvenience this might have caused.

Comments concerning the above are welcome.

Kind Regards,

-Fk

--
Filippos N Kolovos

Software Systems Analyst & Engineer
M.Sc. (Eng.) in Data Communications

Automation & Networking Department
University of Macedonia Library
Egnatia 156, P.O.Box 1591
540 06 Thessaloniki, Greece

E-Mail: [EMAIL PROTECTED],
[EMAIL PROTECTED]
----------------------------------------------
```