Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from Event import Event
- import matplotlib.pyplot as plt
- import matplotlib.ticker as ticker
- import random
- def generate_time(current_time1):
- generator = random.gauss(90, 35) + current_time1
- return generator
- high = 8
- low = 1
- start_time = 0.0
- total_time = 400.0
- current_time = 0.0
- max_buffer = 30.0
- buffer = 0.0
- package_size = 4
- bandwidth = low
- plot_buffer_values = []
- plot_bandwidth_values = []
- plot_buffer_time = []
- plot_bandwidth_time = []
- events = []
- events.append(Event(current_time, "BANDWIDTH_CHANGE"))
- events.append(Event((current_time + (package_size/bandwidth)), "BUFFER_CHANGE"))
- while current_time < total_time:
- events.sort(key=lambda Event: Event.time, reverse=False)
- event = events[0]
- if event.getType() == 'BUFFER_CHANGE':
- start_time = current_time
- current_time = event.getTime()
- if event.getType() == 'BANDWIDTH_CHANGE':
- if bandwidth == high:
- bandwidth = low
- else:
- bandwidth = high
- events.append(Event(generate_time(current_time), "BANDWIDTH_CHANGE"))
- elif event.getType() == 'BUFFER_CHANGE':
- events.append(Event((current_time + (package_size/bandwidth)), "BUFFER_CHANGE"))
- buffer += 1
- buffer -= (current_time - start_time)
- if buffer > max_buffer:
- buffer = max_buffer
- elif buffer < 0:
- buffer = 0
- plot_buffer_time.append(current_time)
- plot_buffer_values.append(buffer)
- plot_bandwidth_time.append(current_time)
- plot_bandwidth_values.append(bandwidth)
- events.pop(0)
- plt.switch_backend('TkAgg')
- fig = plt.figure(dpi=200)
- plt.subplots_adjust(hspace=0.8)
- ax1 = plt.subplot(211)
- color = 'tab:red'
- ax1.set_xlim(0, total_time)
- ax1.grid(axis='both', which='both')
- ax1.yaxis.set_major_locator(ticker.AutoLocator())
- ax1.yaxis.set_minor_locator(ticker.AutoMinorLocator())
- ax1.xaxis.set_major_locator(ticker.AutoLocator())
- ax1.xaxis.set_minor_locator(ticker.AutoMinorLocator())
- ax1.set_xlabel("Czas symulacji [s]")
- ax1.set_ylabel("Bufor [s]", color=color)
- ax1.tick_params(axis='y', labelcolor=color)
- ax1.plot(plot_buffer_time, plot_buffer_values, color=color, lw=0.5)
- ax2 = ax1.twinx()
- color = 'tab:green'
- ax2.set_ylim(top=10)
- ax2.set_ylabel("Pasmo [Mbps]", color=color)
- ax2.tick_params(axis='y', labelcolor=color)
- ax2.plot(plot_bandwidth_time, plot_bandwidth_values, color=color, drawstyle='steps-post')
- plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement