Skip to content

Commit e745016

Browse files
committed
#617: separate timer decorator into own file; clean up writer code
1 parent 966684c commit e745016

File tree

2 files changed

+22
-46
lines changed

2 files changed

+22
-46
lines changed

src/lbaf/IO/lbsVTDataWriter.py

Lines changed: 2 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,6 @@
4545
import os
4646
import sys
4747
import math
48-
import time
49-
import functools
5048

5149
from logging import Logger
5250
from typing import Optional
@@ -57,16 +55,7 @@
5755
from ..Model.lbsRank import Rank
5856
from ..Model.lbsObject import Object
5957

60-
def timer(method):
61-
@functools.wraps(method)
62-
def wrapper(self, *args, **kwargs):
63-
start = time.time()
64-
res = method(self, *args, **kwargs)
65-
end = time.time()
66-
dur = end - start
67-
self._VTDataWriter__logger.info(f"{method.__name__}: {dur:.4f} s")
68-
return res
69-
return wrapper
58+
from ..Utils.lbsTimerDecorator import timer
7059

7160
class VTDataWriter:
7261
"""A class to write load directives for VT as JSON files
@@ -404,26 +393,13 @@ def _json_writer(self, rank_phases_double) -> str:
404393
@timer
405394
def write(self, phases: dict):
406395
""" Write one JSON per rank for dictonary of phases with possibly iterations."""
407-
408-
## REGION A
409-
410-
# start = time.time()
411-
412396
# Ensure that provided phase has correct type
413397
if not isinstance(phases, dict):
414398
self.__logger.error(
415399
"JSON writer must be passed a dictionary")
416400
raise SystemExit(1)
417401
self.__phases = phases
418402

419-
# end = time.time()
420-
# dur = end - start
421-
# print(f"Region A: {dur} s")
422-
423-
## REGION B
424-
425-
# start = time.time()
426-
427403
# Assemble mapping from ranks to their phases
428404
self.__rank_phases = {}
429405
for phase in self.__phases.values():
@@ -435,27 +411,7 @@ def write(self, phases: dict):
435411
# Prevent recursion overruns
436412
sys.setrecursionlimit(25000)
437413

438-
# end = time.time()
439-
# dur = end - start
440-
# print(f"Region B: {dur} s")
441-
442-
## REGION C
443-
444-
# start = time.time()
445-
446-
# Write individual rank files using data parallelism
447-
# with mp.pool.Pool(context=mp.get_context("spawn")) as pool:
448-
# self.__logger.info(f"{pool._processes} threads for {len(self.__rank_phases)} ranks.")
449-
# results = pool.imap_unordered(
450-
# self._json_writer, self.__rank_phases.items())
451-
# for file_name in results:
452-
# self.__logger.info(f"Wrote {file_name}")
453-
454-
# Try in serial
414+
# Write individual rank files
455415
for rank_phases_double in self.__rank_phases.items():
456416
file_name = self._json_writer(rank_phases_double)
457417
self.__logger.info(f"Wrote {file_name}")
458-
459-
# end = time.time()
460-
# dur = end - start
461-
# print(f"Region C: {dur} s")
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import time
2+
import logging
3+
import functools
4+
5+
def timer(method):
6+
@functools.wraps(method)
7+
def wrapper(self, *args, **kwargs):
8+
"""Times the execution of the decorated function."""
9+
# Time the function duration
10+
start = time.time()
11+
res = method(self, *args, **kwargs)
12+
end = time.time()
13+
dur = end - start
14+
15+
# Get a logger instance (stacklevel=2 uses the logger from the function being decorated)
16+
logger = logging.getLogger(self.__class__.__module__)
17+
logger.debug(f"{method.__name__}: {dur:.4f} s", stacklevel=2)
18+
19+
return res
20+
return wrapper

0 commit comments

Comments
 (0)