Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- import copy
- from Event import Event
- from matplotlib import pyplot as plt
- def generate(time):
- return random.uniform(20,30) + time
- def main():
- allTimes = []
- bufferValues = []
- bandwidthValues = []
- results = open("results.txt", "w")
- high = 10
- low = 1
- packSize = 4.5
- currentTime = 0
- totalTime = 100
- startTime = 0
- buffer = 0
- bandwidth = high
- events = []
- changeBandwidth = Event(generate(currentTime), "Change bandwidth")
- events.append(changeBandwidth)
- changeBuffer = Event(currentTime + (packSize/bandwidth), "Change buffer size")
- events.append(changeBuffer)
- results.write(str(int(currentTime * 100)) + " " + str(bandwidth) + " " + str(int(buffer*100)) + "\n")
- allTimes.append(int(currentTime * 100))
- bandwidthValues.append(bandwidth)
- bufferValues.append(buffer)
- while currentTime < totalTime:
- events.sort(key=lambda x: x.time, reverse=False)
- event = Event(events[0].time, events[0].type)
- if event.type == "Change buffer size":
- startTime = currentTime
- currentTime = event.time
- if event.type == "Change bandwidth":
- if bandwidth == high:
- bandwidth = low
- else:
- bandwidth = high
- changeBandwidth = Event(generate(currentTime), "Change bandwidth")
- events.append(changeBandwidth)
- elif event.type == "Change buffer size":
- changeBuffer = Event(currentTime + (packSize/bandwidth), "Change buffer size")
- events.append(changeBuffer)
- buffer += 1
- buffer = buffer - (currentTime - startTime)
- if buffer > 30: buffer = 30
- if buffer < 0: buffer = 0
- results.write(str(int(currentTime * 100)) + " " + str(bandwidth) + " " + str(int(buffer*100)) + "\n")
- allTimes.append(int(currentTime * 100))
- bandwidthValues.append(bandwidth)
- bufferValues.append(buffer)
- events.pop(0)
- plt.plot(allTimes, bandwidthValues, "-b", label="Bandwidth")
- plt.plot(allTimes, bufferValues, "-r", label="Buffer")
- plt.xlabel('Time [s]')
- plt.ylabel('Buffer size [s]')
- plt
- plt.legend(loc="upper left")
- plt.show()
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement