OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
benchmark_timer.py
Go to the documentation of this file.
1 """
2 Module which allows timing to be performed.
3 """
4 import time
5 
6 HOURS_PER_DAY = 24
7 MINS_PER_HOUR = 60
8 SECS_PER_DAY = 86400
9 SECS_PER_HOUR = 3600
10 SECS_PER_MIN = 60
11 
12 #2345678901234567890123456789012345678901234567890123456789012345678901234567890
13 
14 class BenchmarkTimer(object):
15  """ A class for simple benchmark timing. """
16  def __init__(self):
17  self.start_time = None
18  self.end_time = None
19  self.total_time = None
20 
21  def end(self):
22  """ Sets the end time of the timer. """
23  if self.start_time:
24  if not self.end_time:
25  self.end_time = time.time()
26  self.total_time = self.end_time - self.start_time
27  else:
28  raise BenchmarkTimerError("Timer already ended at {0}.".format(self.get_end_time_str()))
29  else:
30  raise BenchmarkTimerError("Timer must started before it can be ended.")
31 
32  def get_end_time_str(self):
33  if self.end_time:
34  return time.strftime('%Y-%m-%d, %H:%M:%S', time.localtime(self.end_time))
35  else:
36  raise BenchmarkTimerError("The End time has not been set.")
37 
38  def get_start_time_str(self):
39  if self.start_time:
40  return time.strftime('%Y-%m-%d, %H:%M:%S', time.localtime(self.start_time))
41  else:
42  raise BenchmarkTimerError("Timer not started yet.")
43 
44  def start(self):
45  """ Sets the start time of the timer. """
46  if not self.start_time:
47  self.start_time = time.time()
48  else:
49  raise BenchmarkTimerError("Timer already started at {0}.".format(self.get_start_time_str()))
50 
51  def get_total_time(self):
52  """ Returns the elapsed time. """
53  if self.start_time:
54  if self.end_time:
55  return self.start_time - self.end_time
56  else:
57  raise BenchmarkTimerError("Total time not available, timer still running.")
58  else:
59  raise BenchmarkTimerError("Total time not available, timer not yet started.")
60 
61  def get_total_time_str(self):
62  """ Returns the elapsed time. """
63  if self.total_time:
64  return self.__str__()
65  else:
66  if self.start_time:
67  raise BenchmarkTimerError('Total time not available! Timer still running.')
68  else:
69  raise BenchmarkTimerError('Total time not available! Timer not started.')
70 
71  def __repr__(self):
72  if self.end_time:
73  return self.start_time, self.end_time, self.total_time
74  elif self.start_time:
75  return self.start_time
76  else:
77  raise BenchmarkTimerError('Timer is not started yet.')
78 
79  def __str__(self):
80  if self.total_time:
81  (days, secs) = divmod(self.total_time, SECS_PER_DAY)
82  (hours, secs) = divmod(secs, SECS_PER_HOUR)
83  (mins, secs) = divmod(secs, SECS_PER_MIN)
84  if days > 0:
85  return '{0} {1:02d}:{2:02d}:{3:06.3f}'.format(
86  int(days), int(hours), int(mins), secs)
87  else:
88  return '{0:02d}:{1:02d}:{2:06.3f}'.format(int(hours),
89  int(mins), secs)
90  elif self.start_time:
91  return 'Timer started at {0} is still running.'.format(
92  time.strftime('%Y/%m/%d, %H:%M:%S',
93  time.localtime(self.start_time)))
94  else:
95  raise BenchmarkTimerError('Timer is not started yet.')
96 
97 class BenchmarkTimerError(Exception):
98  """ Exception class for the BenchmarkTimer. """
99  def __init__(self, m):
100  self.msg = m
101  def __str__(self):
102  return repr(self.msg)