ATLAS Offline Software
AtlRunQueryTimer.py
Go to the documentation of this file.
1 # Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
2 
3 
4 from __future__ import print_function
5 from contextlib import contextmanager
6 from time import time
7 
8 class TimeCount:
9  def __init__(self, name):
10  self.name = name
11  self.totaltime = 0
12  self.subcounts = []
13 
14  def __repr__(self):
15  return "%s: %f" % (self.name, self.totaltime)
16 
17  def __str__(self):
18  return "%s: %f" % (self.name, self.totaltime)
19 
20  def printRecursive(self, lvl):
21  from operator import attrgetter
22  # print myself
23  if self.subcounts:
24  # sum up sub counts
25  ts = sum([x.totaltime for x in self.subcounts])
26  print ("%s%-70s : %f (sub sum %f)" % (5*lvl*" ", self.name, self.totaltime, ts))
27  else:
28  print ("%s%-70s : %f" % (5*lvl*" ", self.name, self.totaltime))
29  # sort sub counters
30  sortedByTime = sorted(self.subcounts,key=attrgetter('totaltime'),reverse=True)
31  # call print for sub counters
32  for subtc in sortedByTime:
33  subtc.printRecursive(lvl+1)
34 
35  #def __eq__(self, r):
36  # return self.name == r
37 
38 
39 class TimerStats:
40  level=0
41  total = TimeCount('total')
42  totalFlat = {}
43  context = []
44 
45  @classmethod
46  def saveTimeFlat(cls, exectime):
47  n = cls.context[-1]
48  if n not in cls.totalFlat:
49  cls.totalFlat[n] = [0,0]
50  cls.totalFlat[n][0] += exectime
51  cls.totalFlat[n][1] += 1
52 
53 
54  @classmethod
55  def saveTime(cls, exectime):
56  cur = cls.total # a TimeCount instance
57  for n in cls.context:
58  if n=='total':
59  cur = cls.total
60  continue
61  try:
62  idx = cur.subcounts.index(n)
63  print ('index', idx)
64  cur = cur.subcounts[idx]
65  except ValueError:
66  cur.subcounts += [TimeCount(n)]
67  cur = cur.subcounts[-1]
68  cur.totaltime += exectime
69  cls.saveTimeFlat(exectime)
70 
71  @classmethod
72  def printTimeSummary(cls):
73  #import pickle
74  #f = open("timecount.pickle","w")
75  #pickle.dump(cls.total,f)
76  #f.close()
77  cls.total.printRecursive(0)
78 
79  @classmethod
80  def printTimeFlat(cls):
81  for name, [_time, callcount] in sorted(cls.totalFlat.items(),key=lambda x: x[1][0]):
82  print ("%-70s : %f (%i)" % (name, _time, callcount))
83 
84 
85 @contextmanager
86 def timer(name, disabled = False):
87  "A context manager which spits out how long the block took to execute"
88  if disabled: #not environ.has_key("ARQDEBUG"):
89  yield
90  return
91 
92  from CoolRunQuery.utils.AtlRunQueryTimer import TimerStats as TS
93 
94  start = time()
95  TS.level += 1
96  TS.context.append(name)
97  try:
98  yield
99  finally:
100  end = time()
101  execTime = end - start
102  TS.saveTime(execTime)
103  TS.level -= 1
104  TS.context.pop()
105  print ("%*s took %.2f sec to %s" % (5*TS.level, "...", execTime, name))
python.utils.AtlRunQueryTimer.TimeCount.subcounts
subcounts
Definition: AtlRunQueryTimer.py:12
python.utils.AtlRunQueryTimer.TimeCount.__init__
def __init__(self, name)
Definition: AtlRunQueryTimer.py:9
python.utils.AtlRunQueryTimer.timer
def timer(name, disabled=False)
Definition: AtlRunQueryTimer.py:86
python.utils.AtlRunQueryTimer.TimeCount
Definition: AtlRunQueryTimer.py:8
python.utils.AtlRunQueryTimer.TimeCount.totaltime
totaltime
Definition: AtlRunQueryTimer.py:11
convertTimingResiduals.sum
sum
Definition: convertTimingResiduals.py:55
python.utils.AtlRunQueryTimer.TimerStats.saveTime
def saveTime(cls, exectime)
Definition: AtlRunQueryTimer.py:55
python.utils.AtlRunQueryTimer.TimeCount.name
name
Definition: AtlRunQueryTimer.py:10
python.utils.AtlRunQueryTimer.TimerStats.context
list context
Definition: AtlRunQueryTimer.py:43
python.utils.AtlRunQueryTimer.TimerStats.printTimeSummary
def printTimeSummary(cls)
Definition: AtlRunQueryTimer.py:72
DerivationFramework::TriggerMatchingUtils::sorted
std::vector< typename T::value_type > sorted(T begin, T end)
Helper function to create a sorted vector from an unsorted one.
python.utils.AtlRunQueryTimer.TimeCount.__repr__
def __repr__(self)
Definition: AtlRunQueryTimer.py:14
python.utils.AtlRunQueryTimer.TimeCount.printRecursive
def printRecursive(self, lvl)
Definition: AtlRunQueryTimer.py:20
python.utils.AtlRunQueryTimer.TimerStats.printTimeFlat
def printTimeFlat(cls)
Definition: AtlRunQueryTimer.py:80
TrigJetMonitorAlgorithm.items
items
Definition: TrigJetMonitorAlgorithm.py:79
python.utils.AtlRunQueryTimer.TimerStats.saveTimeFlat
def saveTimeFlat(cls, exectime)
Definition: AtlRunQueryTimer.py:46
python.utils.AtlRunQueryTimer.TimerStats.totalFlat
dictionary totalFlat
Definition: AtlRunQueryTimer.py:42
python.utils.AtlRunQueryTimer.TimeCount.__str__
def __str__(self)
Definition: AtlRunQueryTimer.py:17
python.utils.AtlRunQueryTimer.TimerStats.total
total
Definition: AtlRunQueryTimer.py:41
python.utils.AtlRunQueryTimer.TimerStats
Definition: AtlRunQueryTimer.py:39