Study of TCP/UDP Performance Using NS-2

 

Study of TCP/UDP Performance Using NS-2

 

Aim: To study and compare the performance of TCP and UDP using the NS-2 simulation tool. (Packet transmission, Packet loss, Throughput and Packet Delivery Ratio (PDR))

 

Simulation setup

Parameter

TCP

UDP

Number of application packets

1000

1000

Packet size

1000 bytes

1000 bytes

Total application data

1 MB

1 MB

Bottleneck bandwidth

1 Mbps

1 Mbps

 

Steps to execute

1.    Install NS-2 in Google Colab

a.   Open Google Colab and create a new notebook

b.   Check the Linux environment

o   !uname -a

c.   Update the package list

o   !apt-get update -qq

d.   Install NS-2

o   !apt-get install -y ns2

e.   Check the NS-2 installation and verify

o   !which ns

2.   TCP UDP Performance analysis.

a.   Create the TCL file

b.   Run the TCL program

c.   Check the generated files

d.   Analyse the tracefile and plot the result

 


 

TCP UDP Performance analysis

 

Create the tcl file

%%writefile tcp_udp.tcl

 

# Create simulator

set ns [new Simulator]

 

# Create trace file

set tracefile [open tcp_udp.tr w]

$ns trace-all $tracefile

 

# Create nodes

set n0 [$ns node]       ;# TCP source

set n1 [$ns node]       ;# UDP source

set n2 [$ns node]       ;# Router

set n3 [$ns node]       ;# Router

set n4 [$ns node]       ;# Destination

 

# Create links

$ns duplex-link $n0 $n2 10Mb 10ms DropTail

$ns duplex-link $n1 $n2 10Mb 10ms DropTail

 

# Bottleneck link

$ns duplex-link $n2 $n3 1Mb 20ms DropTail

$ns duplex-link $n3 $n4 10Mb 10ms DropTail

 

# Queue limit

$ns queue-limit $n2 $n3 10

 

# TCP CONNECTION

# TCP Reno agent

set tcp [new Agent/TCP/Reno]

$tcp set packetSize_ 1000

 

# Attach TCP to n0

$ns attach-agent $n0 $tcp

 

# TCP receiver

set tcpsink [new Agent/TCPSink]

$ns attach-agent $n4 $tcpsink

$ns connect $tcp $tcpsink

 

# TCP traffic

set tcp_app [new Application/Traffic/CBR]

$tcp_app set packetSize_ 1000

$tcp_app set interval_ 0.01

$tcp_app set random_ false

 

$tcp_app attach-agent $tcp

 

# UDP CONNECTION

# UDP agent

set udp [new Agent/UDP]

$udp set packetSize_ 1000

$ns attach-agent $n1 $udp

 

# UDP receiver

set null [new Agent/Null]

$ns attach-agent $n4 $null

$ns connect $udp $null

 

# UDP traffic

set udp_app [new Application/Traffic/CBR]

$udp_app set packetSize_ 1000

$udp_app set interval_ 0.01

$udp_app set random_ false

 

$udp_app attach-agent $udp

 

# START TRANSMISSION

$ns at 0.5 "$tcp_app start"

$ns at 0.5 "$udp_app start"

 

# STOP TRANSMISSION

$ns at 10.5 "$tcp_app stop"

$ns at 10.5 "$udp_app stop"

 

# END SIMULATION

$ns at 11.0 "finish"

 

# Finish procedure

proc finish {} {

 

    global ns tracefile

 

    $ns flush-trace

 

    close $tracefile

 

    exit 0

}

 

# Run simulation

 

$ns run

 

Run the TCL program

!ns tcp_udp.tcl

 

Check the generated files

!ls -lh tcp_udp*

 

Analyze the trace file and plot the result

# TCP vs UDP Performance Analysis

 

TCP_GENERATED = 1000

UDP_GENERATED = 1000

 

tcp_received = 0

udp_received = 0

 

tcp_bytes = 0

udp_bytes = 0

 

tcp_dropped = 0

udp_dropped = 0

 

with open("tcp_udp.tr", "r") as f:

 

    for line in f:

 

        fields = line.split()

 

        if len(fields) < 6:

            continue

 

        event = fields[0]

        from_node = fields[2]

        to_node = fields[3]

        packet_type = fields[4]

 

        try:

            packet_size = int(fields[5])

        except:

            continue

 

        # TCP received at destination n4

        if (event == "r" and

            to_node == "4" and

            packet_type == "tcp"):

 

            tcp_received += 1

            tcp_bytes += packet_size

 

        # UDP received at destination n4

        if (event == "r" and

            to_node == "4" and

            packet_type == "cbr"):

 

            udp_received += 1

            udp_bytes += packet_size

 

        # Dropped packets

        if event == "d":

 

            if packet_type == "tcp":

                tcp_dropped += 1

 

            elif packet_type == "cbr":

                udp_dropped += 1

 

# Packet Loss

tcp_loss = TCP_GENERATED - tcp_received

udp_loss = UDP_GENERATED - udp_received

 

# PDR

tcp_pdr = (tcp_received / TCP_GENERATED) * 100

udp_pdr = (udp_received / UDP_GENERATED) * 100

 

# Throughput

duration = 10.0

 

tcp_throughput = (tcp_bytes * 8/(duration * 1000000))

udp_throughput = (udp_bytes * 8/(duration * 1000000))

 

# Display Results

print("             TCP vs UDP PERFORMANCE")

print(f"{'Metric':<25}{'TCP':<15}{'UDP':<15}")

 

print(f"{'Packets Generated':<25}"

      f"{TCP_GENERATED:<15}"

      f"{UDP_GENERATED:<15}")

 

 

print(f"{'Packets Received':<25}"

      f"{tcp_received:<15}"

      f"{udp_received:<15}")

 

print(f"{'Packet Loss':<25}"

      f"{tcp_loss:<15}"

      f"{udp_loss:<15}")

 

print(f"{'Packets Dropped':<25}"

      f"{tcp_dropped:<15}"

      f"{udp_dropped:<15}")

 

print(f"{'PDR (%)':<25}"

      f"{tcp_pdr:<15.2f}"

      f"{udp_pdr:<15.2f}")

 

print(f"{'Throughput (Mbps)':<25}"

      f"{tcp_throughput:<15.4f}"

      f"{udp_throughput:<15.4f}")

 

 

Packet Loss

import matplotlib.pyplot as plt

 

protocols = ["TCP", "UDP"]

loss = [tcp_loss, udp_loss]

 

plt.figure(figsize=(7,5))

plt.bar(protocols, loss)

 

plt.xlabel("Protocol")

plt.ylabel("Number of Packets")

plt.title("TCP vs UDP Packet Loss")

 

plt.show()

 

Throughput

throughput = [tcp_throughput, udp_throughput]

 

plt.figure(figsize=(7,5))

plt.bar(protocols, throughput)

 

plt.xlabel("Protocol")

plt.ylabel("Throughput (Mbps)")

plt.title("TCP vs UDP Throughput")

 

plt.show()

 

 

Packet Delivery Ratio

pdr = [tcp_pdr, udp_pdr]

 

plt.figure(figsize=(7,5))

plt.bar(protocols, pdr)

 

plt.xlabel("Protocol")

plt.ylabel("Packet Delivery Ratio (%)")

plt.title("TCP vs UDP Packet Delivery Ratio")

 

plt.ylim(0, 100)

plt.show()

 

 

Result: The performance of TCP and UDP was successfully studied using NS-2 simulation. TCP and UDP traffic were generated through a common bottleneck link, and their performance was analyzed using packet delivery, packet loss and throughput measurements.

 

 

No comments:

Post a Comment

Don't be a silent reader...
Leave your comments...

Anu