def __init__(self, arrival_mean, service_mean): # Parameters: self.arrival_mean = arrival_mean self.service_mean = service_mean # System state: self.queue_size = 0 self.server_busy = False # Statistics: self.system_size_trace = Trace() self.server_busy_trace = Trace() self.service_intervals = Statistic() self.arrivals = Intervals() self.departures = Intervals()
def __init__( self, sim, address=None, sifs=None, phy_header_size=None, ack_size=None, ): super().__init__(sim) # Properties: self.__address = address self.__sifs = sifs if sifs is not None else sim.params.sifs self.__phy_header_size = (phy_header_size if phy_header_size is not None else sim.params.phy_header_size) self.__ack_size = (ack_size if ack_size is not None else sim.params.ack_size) # State variables: self.__state = Receiver.State.IDLE self.__rxbuf = set() self.__cur_tx_pdu = None # Statistics: self.__num_collisions = 0 self.__num_received = 0 self.__busy_trace = Trace() self.__busy_trace.record(sim.stime, 0)
def __init__(self, sim, capacity): super().__init__(sim) self.__capacity = capacity self.__size = 0 # Statistics: self.size_trace = Trace() self.size_trace.record(self.sim.stime, 0)
def __init__(self, sim, service_mean): super().__init__(sim) self.__service_mean = service_mean self.__busy = False # Statistics: self.delays = Statistic() self.busy_trace = Trace() self.busy_trace.record(self.sim.stime, 0)
def test_getting_pmf(data, probs): trace = Trace(data) pmf = trace.pmf() assert set(pmf.keys()) == set(probs.keys()) estimated, expected = [], [] for v, p in probs.items(): estimated.append(pmf[v]) expected.append(p) np.testing.assert_almost_equal(estimated, expected)
def test_getting_timeavg(data): def estimate(): interval = data[-1][0] - data[0][0] acc = 0 for i in range(1, len(data)): acc += data[i - 1][1] * (data[i][0] - data[i - 1][0]) / interval return acc trace = Trace(data) estimated_avg = estimate() np.testing.assert_almost_equal(trace.timeavg(), estimated_avg)
def test_record_adds_data(): trace = Trace() assert trace.as_tuple() == () trace.record(t=1, v=10) assert trace.as_tuple() == ((1, 10), ) trace.record(t=2, v=13) assert trace.as_tuple() == ( (1, 10), (2, 13), )
def __init__(self, sim, capacity, index=0): super().__init__(sim) self.__capacity = capacity self.packets = deque() self.index = index # Statistics: self.size_trace = Trace() self.size_trace.record(self.sim.stime, 0) self.num_dropped = 0 self.arrival_intervals = Intervals() self.arrival_intervals.record(sim.stime) self.num_arrived = 0 self.wait_intervals = Statistic()
def __init__(self, sim, service_time, index=0): super().__init__(sim) self.service_time = service_time self.packet = None self.index = index # Statistics: self.service_intervals = Statistic() self.busy_trace = Trace() self.busy_trace.record(self.sim.stime, 0) self.departure_intervals = Intervals() self.departure_intervals.record(sim.stime) self.num_served = 0
def __init__(self, sim): super().__init__(sim) # Topology comprises: # - `nodes`: each having position, properties # - `static_connections`: optional dictionary with static connections # We use it as a template for network creation. self.network = Network(sim.params.topology) self.repair_started = False self.failed_nodes = [] self.routing_mode = sim.params.routing_mode sim.logger.level = sim.params.log_level # Statistics: self.num_failed = Trace() self.num_offline = Trace() self.operable = Trace() self.num_failed_sampled = Trace() self.num_offline_sampled = Trace() # Record initial trace data: t = sim.stime num_offline = len(self.network.get_offline_nodes()) self.num_failed.record(t, 0) self.num_failed_sampled.record(t, 0) self.num_offline.record(t, num_offline) self.num_offline_sampled.record(t, num_offline) self.operable.record(t, 1) # Build network routes: self.network.build_routing_table(self.routing_mode) for sensor in self.network.sensors(): self.schedule_failure(sensor) # Check whether we record samples: if sim.params.record_samples: sim.schedule(sim.params.sample_interval, self.handle_sample_timeout)
def __init__(self, sim, bitrate=inf, header_size=0, preamble=0, ifs=0): super().__init__(sim) self.bitrate = bitrate self.header_size = header_size self.preamble = preamble self.ifs = ifs # State variables: self.__started = False self.__tx_frame = None self.__wait_ifs = False self.__rx_frame = None # Statistics: self.__num_received_frames = 0 self.__num_received_bits = 0 self.__rx_busy_trace = Trace() self.__rx_busy_trace.record(0, 0) self.__num_transmitted_packets = 0 self.__num_transmitted_bits = 0 self.__tx_busy_trace = Trace() self.__tx_busy_trace.record(0, 0) self.__service_time = Statistic() self.__service_started_at = None # Initialization: self.sim.schedule(self.sim.stime, self.start)
def __init__(self, sim): super().__init__(sim) self.children['queue'] = Queue(sim, sim.params.capacity) self.children['source'] = Source(sim, sim.params.arrival_mean) self.children['server'] = Server(sim, sim.params.service_mean) self.children['sink'] = Sink(sim) # Building connections: self.source.connections['queue'] = self.queue self.queue.connections['server'] = self.server self.server.connections['queue'] = self.queue self.server.connections['sink'] = self.sink # Statistics: self.system_size_trace = Trace() self.system_size_trace.record(self.sim.stime, 0)
def __init__( self, sim, address=None, phy_header_size=None, mac_header_size=None, ack_size=None, bitrate=None, preamble=None, max_propagation=0, ): super().__init__(sim) # Properties: self.__address = address self.__phy_header_size = (phy_header_size if phy_header_size is not None else sim.params.phy_header_size) self.__mac_header_size = (mac_header_size if mac_header_size is not None else sim.params.mac_header_size) self.__bitrate = bitrate if bitrate is not None else sim.params.bitrate self.__preamble = (preamble if preamble is not None else sim.params.preamble) self.__max_propagation = max_propagation self.__ack_size = (ack_size if ack_size is not None else sim.params.ack_size) # State variables: self.timeout = None self.cw = 65536 self.backoff = -1 self.num_retries = None self.pdu = None self.__state = Transmitter.State.IDLE self.__seqn = 0 # Statistics: self.backoff_vector = Statistic() self.__start_service_time = None self.service_time = Statistic() self.num_sent = 0 self.num_retries_vector = Statistic() self.__busy_trace = Trace() self.__busy_trace.record(sim.stime, 0) # Initialize: sim.schedule(0, self.start)
def __init__(self, sim): super().__init__(sim) arrivals = sim.params.arrivals services = sim.params.services queue_capacity = sim.params.queue_capacity n = sim.params.num_stations active_sources = {i for i, ar in enumerate(arrivals) if ar is not None} if n < 1: raise ValueError('num_stations must be >= 1') self.queues, self.sources, self.servers = [], [], [] for i in range(n): self.queues.append(Queue(sim, queue_capacity, i)) self.servers.append(Server(sim, services[i], i)) if i in active_sources: self.sources.append(Source(sim, arrivals[i], i)) else: self.sources.append(None) self.sink = Sink(sim) self.children['queue'] = self.queues self.children['server'] = self.servers self.children['sink'] = self.sink self.children['sources'] = [src for src in self.sources if src] # Connecting modules: for i in range(n): self.queues[i].connections['server'] = self.servers[i] self.servers[i].connections['queue'] = self.queues[i] if self.sources[i]: self.sources[i].connections['queue'] = self.queues[i] if i < n - 1: self.servers[i].connections['next'] = self.queues[i + 1] else: self.servers[i].connections['next'] = self.sink # Statistics: self.system_size_trace = [Trace() for _ in range(n)] for i in range(n): self.system_size_trace[i].record(sim.stime, 0) self.system_wait_intervals = [Statistic() for _ in range(n)]
def __init__(self, sim): super().__init__(sim) arrival = sim.params.arrival service = sim.params.service queue_capacity = sim.params.queue_capacity self.children['queue'] = Queue(sim, queue_capacity) self.children['source'] = Source(sim, arrival, index=0) self.children['server'] = Server(sim, service) self.children['sink'] = Sink(sim) # Building connections: self.source.connections['queue'] = self.queue self.queue.connections['server'] = self.server self.server.connections['queue'] = self.queue self.server.connections['next'] = self.sink # Statistics: self.system_size_trace = Trace() self.system_size_trace.record(self.sim.stime, 0) self.system_wait_intervals = Statistic()
def test_trace_creation_with_unordered_timestamps_raises_error(data): with pytest.raises(ValueError) as excinfo: Trace(data) assert 'data must be ordered by time' in str(excinfo.value).lower()
def test_trace_creation_with_wrong_mode_raises_error(): with pytest.raises(ValueError) as excinfo: Trace([(1, ), (2, )], mode='wrong') assert 'invalid mode' in str(excinfo.value).lower()
def test_trace_creation_with_2x2_data_without_mode_is_the_same_as_samples(): data = ((1, 2), (10, 20)) # t=1, v=10; t=2, v=20 trace_default = Trace(data) trace_samples = Trace(data, mode='samples') assert trace_default.as_tuple() == trace_samples.as_tuple()
def test_trace_creation_with_2x2_data_using_mode_argument(mode, expected): trace = Trace(((1, 2), (10, 20)), mode=mode) assert trace.as_tuple() == expected
def test_trace_creation_raises_error_when_passed_data_with_wrong_shape(data): with pytest.raises(ValueError) as excinfo: Trace(data) assert 'wrong data shape' in str(excinfo.value).lower()
def test_trace_creation_with_single_item(data): trace = Trace(data) assert not trace.empty assert len(trace) == 1 assert trace.as_tuple() == ((1, 10), )
def test_trace_is_initially_empty(): trace = Trace() assert trace.empty assert len(trace) == 0 assert trace.as_tuple() == ()
def test_asarray_with_illegal_mode_raises_error(): trace = Trace([[0, 0], [1, 5]]) with pytest.raises(ValueError) as excinfo: trace.asarray('wrong mode') assert 'invalid mode' in str(excinfo.value).lower()
def test_asarray(data, mode, expected): trace = Trace(data) actual = trace.asarray(mode) assert isinstance(actual, np.ndarray) np.testing.assert_almost_equal(actual, expected)
def test_trace_creation_with_valid_values_and_timestamps(): trace = Trace([[1, 2, 3], [10, 11, 9]]) assert not trace.empty assert len(trace) == 3 assert trace.as_tuple() == ((1, 10), (2, 11), (3, 9))
def test_getting_pmf_for_empty_trace_raises_error(): trace = Trace() with pytest.raises(ValueError) as excinfo: trace.pmf() assert 'expected non-empty values' in str(excinfo.value).lower()
def test_trace_creation_with_valid_data_given_in_pairs(): data = [(1, 5), (2, 8), (3, 40), (9, 34)] trace = Trace(data) assert not trace.empty assert len(trace) == 4 assert trace.as_tuple() == tuple(data)
def test_as_tuple(data, mode, expected): trace = Trace(data) if expected is None: expected = data assert trace.as_tuple(mode=mode) == expected
def test_record_with_past_time_causes_error(): trace = Trace([(10, 13)]) with pytest.raises(ValueError) as excinfo: trace.record(5, 10) assert 'adding data in past prohibited' in str(excinfo.value).lower()