Advertisement
Guest User

main

a guest
Dec 12th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.25 KB | None | 0 0
  1. import random
  2. import copy
  3. from Event import Event
  4. from matplotlib import pyplot as plt
  5.  
  6. def generate(time):
  7. return random.uniform(20,30) + time
  8.  
  9. def main():
  10. allTimes = []
  11. bufferValues = []
  12. bandwidthValues = []
  13.  
  14. results = open("przebieg.txt", "w")
  15. high = 10
  16. low = 1
  17. packSize = 4.5
  18.  
  19. currentTime = 0
  20. totalTime = 100
  21. startTime = 0
  22. buffer = 0
  23. bandwidth = high
  24.  
  25. events = []
  26. changeBandwidth = Event(generate(currentTime), "Zmiana strumienia")
  27. events.append(changeBandwidth)
  28.  
  29. changeBuffer = Event(currentTime + (packSize/bandwidth), "Zmiana bufora")
  30. events.append(changeBuffer)
  31.  
  32. results.write(str(int(currentTime * 100)) + " " + str(bandwidth) + " " + str(int(buffer*100)) + "\n")
  33.  
  34. allTimes.append(int(currentTime * 100))
  35. bandwidthValues.append(bandwidth)
  36. bufferValues.append(buffer)
  37.  
  38. while currentTime < totalTime:
  39. events.sort(key=lambda x: x.time, reverse=False)
  40.  
  41. event = Event(events[0].time, events[0].type)
  42.  
  43. if event.type == "Zmiana bufora":
  44. startTime = currentTime
  45.  
  46. currentTime = event.time
  47.  
  48. if event.type == "Zmiana strumienia":
  49.  
  50. if bandwidth == high:
  51. bandwidth = low
  52. else:
  53. bandwidth = high
  54.  
  55. changeBandwidth = Event(generate(currentTime), "Zmiana strumienia")
  56. events.append(changeBandwidth)
  57. elif event.type == "Zmiana bufora":
  58. changeBuffer = Event(currentTime + (packSize/bandwidth), "Zmiana bufora")
  59. events.append(changeBuffer)
  60. buffer += 1
  61. buffer = buffer - (currentTime - startTime)
  62. if buffer > 30: buffer = 30
  63. if buffer < 0: buffer = 0
  64.  
  65. results.write(str(int(currentTime * 100)) + " " + str(bandwidth) + " " + str(int(buffer*100)) + "\n")
  66.  
  67. allTimes.append(int(currentTime * 100))
  68. bandwidthValues.append(bandwidth)
  69. bufferValues.append(buffer)
  70.  
  71. events.pop(0)
  72. plt.plot(allTimes, bandwidthValues, "-b", label="Pasmo")
  73. plt.plot(allTimes, bufferValues, "-r", label="Bufor")
  74. plt.xlabel('Czas')
  75. plt.ylabel('Wartosc')
  76. plt.legend(loc="upper left")
  77. plt.show()
  78. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement