-
Notifications
You must be signed in to change notification settings - Fork 0
/
scenario3.py
142 lines (107 loc) · 3.96 KB
/
scenario3.py
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# * This program is free software; you can redistribute it and/or modify
# * it under the terms of the GNU General Public License version 2 as
# * published by the Free Software Foundation;
# *
# * This program is distributed in the hope that it will be useful,
# * but WITHOUT ANY WARRANTY; without even the implied warranty of
# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# * GNU General Public License for more details.
# *
# * You should have received a copy of the GNU General Public License
# * along with this program; if not, write to the Free Software
# * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
# */
import ns.applications
import ns.core
import ns.internet
import ns.network
import ns.point_to_point
import ns.netanim as netanim
import matplotlib.pyplot as plt
########
# Create a trace file
asciiTraceHelper = ns.network.AsciiTraceHelper()
traceFileStream = asciiTraceHelper.CreateFileStream("trace.tr")
# // Default Network Topology
# //
# // 10.1.1.0
# // n0 -------------- n1
# // point-to-point
# //
ns.core.LogComponentEnable("UdpEchoClientApplication", ns.core.LOG_LEVEL_INFO)
ns.core.LogComponentEnable("UdpEchoServerApplication", ns.core.LOG_LEVEL_INFO)
nodes = ns.network.NodeContainer()
nodes.Create(2)
pointToPoint = ns.point_to_point.PointToPointHelper()
pointToPoint.SetDeviceAttribute("DataRate", ns.core.StringValue("5Mbps"))
pointToPoint.SetChannelAttribute("Delay", ns.core.StringValue("2ms"))
devices = pointToPoint.Install(nodes)
stack = ns.internet.InternetStackHelper()
stack.Install(nodes)
address = ns.internet.Ipv4AddressHelper()
address.SetBase(ns.network.Ipv4Address("10.1.1.0"),
ns.network.Ipv4Mask("255.255.255.0"))
interfaces = address.Assign(devices)
echoServer = ns.applications.UdpEchoServerHelper(9)
serverApps = echoServer.Install(nodes.Get(1))
serverApps.Start(ns.core.Seconds(1.0))
serverApps.Stop(ns.core.Seconds(10.0))
packet_sizes = [64, 128, 256, 512, 1024]
for size in packet_sizes:
print(size)
echoClient = ns.applications.UdpEchoClientHelper(interfaces.GetAddress(1), 9)
echoClient.SetAttribute("MaxPackets", ns.core.UintegerValue(1))
echoClient.SetAttribute("Interval", ns.core.TimeValue(ns.core.Seconds(1.0)))
echoClient.SetAttribute("PacketSize", ns.core.UintegerValue(size))
clientApps = echoClient.Install(nodes.Get(0))
#clientApps.Start(ns.core.Seconds(2.0))
#clientApps.Stop(ns.core.Seconds(10.0))
########
# Connect the trace sources for each network interface to the trace file
pointToPoint.EnableAsciiAll(traceFileStream)
stack.EnableAsciiIpv4All(traceFileStream)
#anim = ns.netanim.AnimationInterface("animation.xml")
ns.core.Simulator.Run()
ns.core.Simulator.Destroy()
traceFile = open("trace.tr", "r")
# Initialize variables to calculate throughput
totalBytes = 0
startTime = 0.0
endTime = 0.0
for line in traceFile:
# Split the line into columns
columns = line.split()
# Get the current time
currentTime = float(columns[1])
# Get the packet size
#packetSize = int(columns[5])
packetSize = size
# If this is the first packet, record the start time
if startTime == 0.0:
startTime = currentTime
# Add the packet size to the total bytes
totalBytes += packetSize
# Record the end time
endTime = currentTime
# Calculate the throughput
duration= endTime- startTime #temps latence
throughput= (totalBytes* 8) / (duration* 1000000) #debit
# Print the results
print("Total Bytes: ", totalBytes)
print("Start Time: ", startTime)
print("End Time: ", endTime)
print("Duration: ", duration)
print("Throughput: ", throughput, "Mbps")
traceFile.close()
# Open the trace file and read the data
# Create some data
x = [1, 2, 3, 4, 5]
y = [throughput, throughput, throughput, throughput, throughput]
# Create a line plot
plt.plot(x, y)
# Add labels and title
plt.xlabel('X axis label')
plt.ylabel('Y axis label')
plt.title('My line plot')
plt.show()
# Close the trace file