Skip to content

Commit

Permalink
[rwa_wdm] io,sim: init profiling via timeit.default_timer (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
cassiotbatista committed Nov 4, 2020
1 parent d17b1dd commit bd1d13b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 8 deletions.
25 changes: 21 additions & 4 deletions rwa_wdm/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
"""
Expand All @@ -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
"""
Expand Down
16 changes: 12 additions & 4 deletions rwa_wdm/sim.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)

0 comments on commit bd1d13b

Please sign in to comment.