From bd1d13bbb98032f8f43422ebd7fb44a0d02ab79b Mon Sep 17 00:00:00 2001 From: Cassio Batista Date: Tue, 3 Nov 2020 21:47:19 -0300 Subject: [PATCH] [rwa_wdm] io,sim: init profiling via timeit.default_timer (#5) --- rwa_wdm/io.py | 25 +++++++++++++++++++++---- rwa_wdm/sim.py | 16 ++++++++++++---- 2 files changed, 33 insertions(+), 8 deletions(-) diff --git a/rwa_wdm/io.py b/rwa_wdm/io.py index e321a9c..92ac6ba 100644 --- a/rwa_wdm/io.py +++ b/rwa_wdm/io.py @@ -5,14 +5,16 @@ import os import glob import logging +from typing import List import numpy as np import matplotlib.pyplot as plt -logger = logging.getLogger(__name__) # noqa +logger = logging.getLogger(__name__) -def write_results_to_disk(result_dir, filename, bplist): +def write_bp_to_disk(result_dir: str, + filename: str, bplist: List[float]) -> None: """Write blocking probabilities to text file """ @@ -21,14 +23,29 @@ def write_results_to_disk(result_dir, filename, bplist): os.mkdir(result_dir) filepath = os.path.join(result_dir, filename) - logger.info('Writing results to file "%s"' % filepath) + logger.info('Writing blocking probability results to file "%s"' % filepath) with open(filepath, 'a') as f: for bp in bplist: f.write(' %7.3f' % bp) f.write('\n') +def write_it_to_disk(result_dir: str, + filename: str, itlist: List[float]) -> None: + """Write profiling time information to text file -def plot_blocking_probability(result_dir): + """ + if not os.path.isdir(result_dir): + logger.info('Creating result dir in %s' % result_dir) + os.mkdir(result_dir) + + filepath = os.path.join(result_dir, filename) + logger.info('Writing simulation profiling times to file "%s"' % filepath) + with open(filepath, 'a') as f: + for it in itlist: + f.write(' %7.7f' % it) + + +def plot_bp(result_dir: str) -> None: """Reads blocking probabilities from file and plot overlapping graph """ diff --git a/rwa_wdm/sim.py b/rwa_wdm/sim.py index 8775d1c..a290feb 100644 --- a/rwa_wdm/sim.py +++ b/rwa_wdm/sim.py @@ -13,11 +13,12 @@ # [1] https://la.mathworks.com/matlabcentral/fileexchange/4797-wdm-network-blocking-computation-toolbox import logging +from timeit import default_timer # https://stackoverflow.com/a/25823885/3798300 from typing import Callable import numpy as np -from .io import write_results_to_disk, plot_blocking_probability +from .io import write_bp_to_disk, write_it_to_disk, plot_bp from .net import Network logger = logging.getLogger(__name__) @@ -92,7 +93,9 @@ def simulator(args): print('%4d' % i, end=' ') print() + time_per_simulation = [] for simulation in range(args.num_sim): + _sim_time = default_timer() net = get_net_instance_from_args(args.topology, args.channels) rwa = get_rwa_algorithm_from_args(args.r, args.w, args.rwa, args.pop_size, args.num_gen, @@ -190,17 +193,22 @@ def simulator(args): blocklist.append(blocks) blocks_per_erlang.append(100.0 * blocks / args.calls) + _sim_time = default_timer() - _sim_time + time_per_simulation.append(_sim_time) + print('\rBlocks: ', end='', flush=True) for b in blocklist: print('%04d ' % b, end='', flush=True) print('\n%-7s ' % net.name, end='') print(' '.join(['%4.1f' % b for b in blocks_per_erlang])) - filename = '%s_%dch_%dreq_%s.bp' % ( + fbase = '%s_%dch_%dreq_%s' % ( args.rwa if args.rwa is not None else '%s_%s' % (args.r, args.w), args.channels, args.calls, net.name) - write_results_to_disk(args.result_dir, filename, blocks_per_erlang) + write_bp_to_disk(args.result_dir, fbase + '.bp', blocks_per_erlang) + + write_it_to_disk(args.result_dir, fbase + '.it', time_per_simulation) if args.plot: - plot_blocking_probability(args.result_dir) + plot_bp(args.result_dir)