ATLAS Offline Software
IoTestsLib.py
Go to the documentation of this file.
1 # Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
2 
3 
5 
6 
7 __author__ = "Sebastien Binet <binet@cern.ch>"
8 __version__ = "$Revision: 1.1 $"
9 __doc__ = """
10 a set of simple minded functions to test ROOT I/O (from python)
11 """
12 
13 from array import array as carray
14 
15 import random
16 # set some dummy seed, for reproducibility
17 random.seed(20080910) # first LHC startup :)
18 
19 from os import sysconf
20 _pagesz = sysconf('SC_PAGE_SIZE') // 1024 # in kb
21 
22 _py_dtype_to_root = {
23  'i' : 'I',
24  'f' : 'F',
25  }
26 """translates the usual python 'dtype' codes to the ROOT/CINT ones
27 """
28 
29 from PyUtils.Decorators import forking
30 
31 def pymon():
32  """returns (cpu[ms], vmem[kb], rss[kb])
33  """
34  from resource import getrusage, RUSAGE_SELF
35  from string import split as ssplit
36  cpu = getrusage(RUSAGE_SELF)
37  mem = open('/proc/self/statm','r')
38  cpu = (cpu.ru_utime+cpu.ru_stime) * 1e3 # in milliseconds
39  mem = ssplit(mem.readlines()[0])
40  vmem = int(mem[0])*_pagesz
41  rss = int(mem[1])*_pagesz
42  return cpu,vmem,rss
43 
44 def comp_delta(d, verbose=False):
45  assert 'start' in d
46  assert 'stop' in d
47  assert len(d['start']) == 3
48  assert len(d['stop']) == 3
49  if verbose:
50  print (repr(d))
51  delta = { 'cpu' : d['stop'][0] - d['start'][0],
52  'vmem': d['stop'][1] - d['start'][1],
53  'rss' : d['stop'][2] - d['start'][2],
54  'nbytes': -1
55  }
56  if 'nbytes' in d:
57  delta['nbytes'] = d['nbytes']
58  print ("==> cpu: %(cpu)8.3f ms vmem: %(vmem)i kB rss: %(rss)i kB nbytes: %(nbytes)i kB" % delta)
59  return delta
60 
62  import sys
63  # for ROOT...
64  if '-b' not in sys.argv:
65  sys.argv.insert(1, '-b')
66  import ROOT
67  return ROOT
68 
69 ROOT = import_ROOT()
70 
71 @forking
72 def io_test1_write(fname, nevts=1000, sz=1000, dtype='i'):
73  """testing writing 1000 evts with arrays of 1000- integers
74  """
75  f = ROOT.TFile.Open(fname, 'RECREATE')
76  t = ROOT.TTree('t', 't')
77 
78  nevts= nevts
79  imax = sz
80  data = carray(dtype, imax*[ 0 ] )
81  #t.Branch( 'mynum', n, 'mynum/I' )
82  t.Branch( 'i', data, 'data[%d]/%s'%(imax, _py_dtype_to_root[dtype]) )
83 
84  from random import randint
85 
86  fill = t.Fill
87  for i in range(nevts):
88  for j in range(sz):
89  data[j] = randint(0, sz)
90  fill()
91 
92  f.Write()
93  f.Close()
94  return
95 
96 @forking
97 def io_test1_read(fname, verbose=False):
98  f = ROOT.TFile.Open(fname, 'READ')
99  t = f.Get('t')
100  assert t, "could not find tree 't'"
101  nevts = t.GetEntries()
102  if verbose:
103  print ("::: reading [%s] (%i events) [sz=%s kB]" % (fname, nevts,
104  f.GetSize()//1024))
105  tot_bytes = 0
106  get_entry = t.GetEntry
107  start = pymon()
108  for ievt in range(nevts):
109  # copy next entry into memory and verify
110  nb = get_entry(ievt)
111  if nb <= 0:
112  continue
113  tot_bytes += nb
114  # use the values directly from the tree
115  assert len(t.data) > 0
116  stop = pymon()
117 
118  del t
119  f.Close()
120 
121  return {'start' : start,
122  'stop' : stop,
123  'nbytes': tot_bytes//1024}
124 
125 @forking
126 def io_test2_write(fname, nevts=1000, sz=1000, dtype='i'):
127  """testing writing 1000 evts with arrays of (variable length) 1000- ints
128  """
129  f = ROOT.TFile.Open(fname, 'RECREATE')
130  t = ROOT.TTree('t', 't')
131 
132  nevts= nevts
133  imax = sz
134 
135  n = carray( 'i', [ 0 ] )
136  data = carray( dtype, imax*[ 0 ] )
137  t.Branch( 'sz', n, 'sz/I' )
138  t.Branch( 'data', data, 'data[sz]/%s'%_py_dtype_to_root[dtype])
139 
140  from random import randint
141 
142  fill = t.Fill
143  for i in range(nevts):
144  jmax = randint(1, sz)
145  n[0] = jmax
146  for j in range(jmax):
147  data[j] = randint(0, sz)
148  fill()
149 
150  f.Write()
151  f.Close()
152  return
153 
154 @forking
155 def io_test2_read(fname, verbose=False):
156  f = ROOT.TFile.Open(fname, 'READ')
157  t = f.Get('t')
158  assert t, "could not find tree 't'"
159  nevts = t.GetEntries()
160  if verbose:
161  print ("::: reading [%s] (%i events) [sz=%s kB]" % (fname, nevts,
162  f.GetSize()//1024))
163  tot_bytes = 0
164  get_entry = t.GetEntry
165  start = pymon()
166  for ievt in range(nevts):
167  # copy next entry into memory and verify
168  nb = get_entry(ievt)
169  if nb <= 0:
170  continue
171  tot_bytes += nb
172  # use the values directly from the tree
173  assert len(t.data) > 0
174  stop = pymon()
175 
176  del t
177  f.Close()
178 
179  return {'start' : start,
180  'stop' : stop,
181  'nbytes': tot_bytes//1024}
182 
183 
184 
185 if __name__ == "__main__":
186  # FIXME: use 'nose' instead... for automatical test discovery
187  print ("::: running all tests...")
188 
189  nreads = 10 # nbr of times to repeat each 'read' test
190  mon_data = {}
191 
192  # -----
193  # io_test1
194  # -----
195 
196  # io_test1 - ints
197  fname = '/tmp/out_test1_ints.root'
198  w = io_test1_write(fname=fname,
199  nevts=100000, sz=1000,
200  dtype='i')
201  mon_data['io_test1-ints'] = []
202  for _ in range(nreads):
203  mon_data['io_test1-ints'].append(comp_delta(io_test1_read(fname=fname)))
204 
205  # io_test1 - floats
206  fname = '/tmp/out_test1_flts.root'
207  w = io_test1_write(fname=fname,
208  nevts=100000, sz=1000,
209  dtype='f')
210  mon_data['io_test1-flts'] = []
211  for _ in range(nreads):
212  mon_data['io_test1-flts'].append(comp_delta(io_test1_read(fname=fname)))
213 
214  # -----
215  # io_test2
216  # -----
217 
218  # io_test2 - ints
219  fname = '/tmp/out_test2_ints.root'
220  w = io_test2_write(fname=fname,
221  nevts=100000, sz=1000,
222  dtype='i')
223  mon_data['io_test2-ints'] = []
224  for _ in range(nreads):
225  mon_data['io_test2-ints'].append(comp_delta(io_test2_read(fname=fname)))
226 
227  # io_test2 - floats
228  fname = '/tmp/out_test2_floats.root'
229  w = io_test2_write(fname=fname,
230  nevts=100000, sz=1000,
231  dtype='f')
232  mon_data['io_test2-flts'] = []
233  for _ in range(nreads):
234  mon_data['io_test2-flts'].append(comp_delta(io_test2_read(fname=fname)))
235 
236 
237  print (mon_data)
python.IoTestsLib.pymon
def pymon()
Definition: IoTestsLib.py:31
python.IoTestsLib.io_test2_write
def io_test2_write(fname, nevts=1000, sz=1000, dtype='i')
Definition: IoTestsLib.py:126
python.IoTestsLib.io_test1_read
def io_test1_read(fname, verbose=False)
Definition: IoTestsLib.py:97
dumpHVPathFromNtuple.append
bool append
Definition: dumpHVPathFromNtuple.py:91
python.IoTestsLib.import_ROOT
def import_ROOT()
Definition: IoTestsLib.py:61
plotBeamSpotVxVal.range
range
Definition: plotBeamSpotVxVal.py:194
PyAthena::repr
std::string repr(PyObject *o)
returns the string representation of a python object equivalent of calling repr(o) in python
Definition: PyAthenaUtils.cxx:106
python.IoTestsLib.comp_delta
def comp_delta(d, verbose=False)
Definition: IoTestsLib.py:44
Trk::open
@ open
Definition: BinningType.h:40
python.CaloAddPedShiftConfig.int
int
Definition: CaloAddPedShiftConfig.py:45
lumiFormat.fill
fill
Definition: lumiFormat.py:104
python.IoTestsLib.io_test1_write
def io_test1_write(fname, nevts=1000, sz=1000, dtype='i')
Definition: IoTestsLib.py:72
python.IoTestsLib.io_test2_read
def io_test2_read(fname, verbose=False)
Definition: IoTestsLib.py:155