From 47e70efc0ff7ca58e5de99186d1f1598178bbab4 Mon Sep 17 00:00:00 2001 From: Jarrod Pas Date: Wed, 16 Aug 2017 16:41:04 -0600 Subject: [PATCH 1/2] Add more random traffic patterns. --- pydtn/__init__.py | 83 +++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 73 insertions(+), 10 deletions(-) diff --git a/pydtn/__init__.py b/pydtn/__init__.py index 457830b..4f22ed2 100644 --- a/pydtn/__init__.py +++ b/pydtn/__init__.py @@ -22,6 +22,9 @@ __all__ = [ 'random_trace', 'Traffic', + 'random_many_to_many_traffic', + 'random_many_to_single_traffic', + 'random_single_to_many_traffic', 'random_traffic', ] __version__ = '0.2' @@ -556,16 +559,76 @@ Traffic = namedtuple('Traffic', [ ]) -def random_traffic(nodes, start=0, speed=1, seed=None, **options): - """Generate traffic from random source to random destination every step.""" - random = Random(seed) +def random_many_to_many_traffic(sources, destinations, **options): + """ + Generate random traffic from nodes in group a to nodes in group b. + + Option Arguments: + seed -- seed to use for the random number generator (default None) + start -- which time step the first packet is create (default 0) + step -- time between traffic generation (default 1) + traffic_per_step -- traffic to generate during each step (default 1) + time_to_live -- packet time to live (default infinite) + payload -- packet payload (default 0) + """ + options = ChainMap(options, { + 'seed': None, + 'start': 0, + 'step': 1, + 'traffic_per_step': 1, + 'time_to_live': float('inf'), + 'payload': 0, + }) + + if isinstance(sources, dict): + sources = list(sources) + + if isinstance(destinations, dict): + destinations = list(destinations) + + random = Random(options['seed']) + + def random_pair(): + """Return a pair of nodes that are different.""" + source = random.choice(sources) + while True: + destination = random.choice(destinations) + if source != destination: + break + return source, destination + + for create_time in count(start=options['start'], step=options['step']): + for _ in range(options['traffic_per_step']): + source, destination = random_pair() + yield Traffic(source=source, + destination=destination, + created=create_time, + time_to_live=options['time_to_live'], + payload=options['payload']) + + +def random_many_to_single_traffic(sources, destination, **options): + """ + Generate random traffic between any node in sources to destination. - if isinstance(nodes, dict): - nodes = list(nodes) + See random_many_to_many_traffic for options. + """ + return random_many_to_many_traffic(sources, [destination], **options) - time_to_live = options.get('time_to_live', float('inf')) - payload = options.get('payload', 0) - for created in count(start=start, step=speed): - source, destination = random.sample(nodes, 2) - yield Traffic(source, destination, created, time_to_live, payload) +def random_single_to_many_traffic(source, destinations, **options): + """ + Generate random traffic between source and any node in destinations. + + See random_many_to_many_traffic for options. + """ + return random_many_to_many_traffic([source], destinations, **options) + + +def random_traffic(nodes, **options): + """ + Generate random traffic between any two node in nodes. + + See random_many_to_many_traffic for options. + """ + return random_many_to_many_traffic(nodes, nodes, **options) -- GitLab From 24e2cfd7c367741b01fb7a556493247a315e3853 Mon Sep 17 00:00:00 2001 From: Jarrod Pas Date: Wed, 16 Aug 2017 16:41:20 -0600 Subject: [PATCH 2/2] Update examples to reflect random traffic changes. --- examples/random_node.py | 2 +- examples/shed.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/random_node.py b/examples/random_node.py index b49c9bf..0067bfa 100644 --- a/examples/random_node.py +++ b/examples/random_node.py @@ -46,7 +46,7 @@ def main(): traffic_options = { 'seed': seed, - 'speed': 1, + 'step': 1, } traffic = random_traffic(nodes, **traffic_options) diff --git a/examples/shed.py b/examples/shed.py index 8fe4668..a0932c1 100644 --- a/examples/shed.py +++ b/examples/shed.py @@ -35,7 +35,7 @@ def run_simulation(simulation): traffic_options = { 'seed': seed, 'start': epoch, - 'speed': 30 * 60, # 1 packet every 30 mins + 'step': 30 * 60, # 1 packet every 30 mins } traffic = random_traffic(nodes, **traffic_options) -- GitLab