Advertisement
Guest User

Untitled

a guest
Dec 9th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.41 KB | None | 0 0
  1. from Event import Event
  2. import matplotlib.pyplot as plt
  3. import matplotlib.ticker as ticker
  4. import random
  5.  
  6.  
  7. def generate_time(current_time1):
  8. generator = random.gauss(90, 35) + current_time1
  9. return generator
  10.  
  11.  
  12. high = 8
  13. low = 1
  14. start_time = 0.0
  15. total_time = 400.0
  16. current_time = 0.0
  17. max_buffer = 30.0
  18. buffer = 0.0
  19. package_size = 4
  20. bandwidth = low
  21. plot_buffer_values = []
  22. plot_bandwidth_values = []
  23. plot_buffer_time = []
  24. plot_bandwidth_time = []
  25. events = []
  26. events.append(Event(current_time, "BANDWIDTH_CHANGE"))
  27. events.append(Event((current_time + (package_size/bandwidth)), "BUFFER_CHANGE"))
  28.  
  29. while current_time < total_time:
  30. events.sort(key=lambda Event: Event.time, reverse=False)
  31. event = events[0]
  32. if event.getType() == 'BUFFER_CHANGE':
  33. start_time = current_time
  34. current_time = event.getTime()
  35. if event.getType() == 'BANDWIDTH_CHANGE':
  36. if bandwidth == high:
  37. bandwidth = low
  38. else:
  39. bandwidth = high
  40. events.append(Event(generate_time(current_time), "BANDWIDTH_CHANGE"))
  41. elif event.getType() == 'BUFFER_CHANGE':
  42. events.append(Event((current_time + (package_size/bandwidth)), "BUFFER_CHANGE"))
  43. buffer += 1
  44. buffer -= (current_time - start_time)
  45. if buffer > max_buffer:
  46. buffer = max_buffer
  47. elif buffer < 0:
  48. buffer = 0
  49. plot_buffer_time.append(current_time)
  50. plot_buffer_values.append(buffer)
  51. plot_bandwidth_time.append(current_time)
  52. plot_bandwidth_values.append(bandwidth)
  53. events.pop(0)
  54.  
  55.  
  56. plt.switch_backend('TkAgg')
  57. fig = plt.figure(dpi=200)
  58. plt.subplots_adjust(hspace=0.8)
  59. ax1 = plt.subplot(211)
  60. color = 'tab:red'
  61. ax1.set_xlim(0, total_time)
  62. ax1.grid(axis='both', which='both')
  63. ax1.yaxis.set_major_locator(ticker.AutoLocator())
  64. ax1.yaxis.set_minor_locator(ticker.AutoMinorLocator())
  65. ax1.xaxis.set_major_locator(ticker.AutoLocator())
  66. ax1.xaxis.set_minor_locator(ticker.AutoMinorLocator())
  67. ax1.set_xlabel("Czas symulacji [s]")
  68. ax1.set_ylabel("Bufor [s]", color=color)
  69. ax1.tick_params(axis='y', labelcolor=color)
  70. ax1.plot(plot_buffer_time, plot_buffer_values, color=color, lw=0.5)
  71.  
  72. ax2 = ax1.twinx()
  73. color = 'tab:green'
  74. ax2.set_ylim(top=10)
  75. ax2.set_ylabel("Pasmo [Mbps]", color=color)
  76. ax2.tick_params(axis='y', labelcolor=color)
  77. ax2.plot(plot_bandwidth_time, plot_bandwidth_values, color=color, drawstyle='steps-post')
  78.  
  79. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement