xboa
SystemTest.py
Go to the documentation of this file.
1 from xboa import *
2 from xboa.core import *
3 from xboa.Hit import Hit
4 from xboa.Bunch import Bunch
5 from xboa.Common import rg
6 
7 import tempfile
8 import time
9 import subprocess
10 import os
11 import sys
12 import time
13 import math
14 import string
15 if Common.has_numpy():
16  import numpy
17  from numpy import linalg
18 import operator
19 import bisect
20 
22 run_test = xboa.test.TestTools.run_test
23 run_test_group = xboa.test.TestTools.run_test_group
24 __float_tol = xboa.test.TestTools.__float_tol
25 parse_tests = xboa.test.TestTools.parse_tests
26 
27 #=======================================================================================================================#
28 #====================== SYSTEM ==============================================================#
29 #=======================================================================================================================#
30 #
31 # I don't test that the program dies gracefully if it runs out of memory
32 #
33 
35  pid = os.getpid()
36  mem_usage = float(os.popen('ps -p %d -o %s | tail -1' % (pid, "rss")).read())
37  print "Memory usage for process", os.getpid(), mem_usage
38  return mem_usage
39 
40 def system_mem_test(verbose=True):
41  # Test memory usage isn't too huge
42  # Test bunch cleans up after itself
43  # We allocate new memory in this test and then deallocate it. Python process
44  # will increase its buffer but the buffer should be empty by the end. Then
45  # run again - python process should not increase buffer size by much as on
46  # second iteration we
47  import gc
48  gc.collect() #force a memory cleanup
49  mem_usage_b4 = print_mem_usage()
50  bunch_list=[]
51  print "Allocating memory"
52  for i in range(3):
53  bunch_list.append(Bunch())
54  for i in range(10000):
55  bunch_list[-1].append(Hit())
56  bunch_list[-1][0].get('x')
57  bunch_list[-1][0].set('x', 1.)
58  bunch_list[-1][0].set('global_weight', 0.5)
59  bunch_list[-1][0].get('global_weight') # this function was leaking
60  bunch_list[-1].moment(['x'])
61  Hit.clear_global_weights()
62  mem_usage = print_mem_usage()
63  print "Cleaning memory"
64  bunch = bunch_list[0]
65  while len(bunch) > 0:
66  del bunch[0]
67 
68  bunch_list.remove(bunch_list[0])
69  del bunch_list
70  Hit.clear_global_weights()
71  gc.collect() #force a memory cleanup
72  mem_usage = print_mem_usage()
73  if verbose:
74  print "Memory usage after cleanup in Mb (target 0 Mb):",mem_usage,'(absolute)',mem_usage-mem_usage_b4,'(difference)'
75  if mem_usage-mem_usage_b4 > 1000.: return "warn" # looks like memory leak...
76  return "pass"
77 
79  data = os.path.join('/', sys.prefix, 'share' ,'xboa', 'data')
80  temp = tempfile.mkstemp()[1]
81  print 'Writing test xboa9f data to ', temp
82  proc = subprocess.Popen(['XBOA9f',
83  '-i='+data+'/for009.dat',
84  '-c='+data+'/ecalc9f.inp',
85  '-o='+temp])
86  proc.wait()
87  if proc.returncode == 0:
88  return 'pass'
89  else:
90  return 'fail'
91 
92 def test_example(example_name):
93  here = os.getcwd()
94  os.chdir(os.path.expandvars('$HOME'))
95  proc = subprocess.Popen(['python', '-m', 'xboa.examples.'+example_name],
96  stdin=subprocess.PIPE)
97  time.sleep(1)
98  proc.communicate(input='\n')
99  proc.wait()
100  os.chdir(here)
101  if proc.returncode != 0:
102  return 'fail'
103  return 'pass'
104 
105 def test_system(test_examples=False):
106  test_results = []
107  run_test([], system_mem_test, () ) # first time can make the python memory buffer expand - so don't put into test
108  run_test(test_results, system_mem_test, () ) # now should work okay - but so dodgy we only return a warn on failure
109  run_test(test_results, system_xboa9f_test_1, ())
110  run_test(test_results, test_example, ('Example_1',) )
111  run_test(test_results, test_example, ('Example_2',) )
112  run_test(test_results, test_example, ('Example_3',) )
113  run_test(test_results, test_example, ('Example_4',) )
114  (passes,fails,warns) = parse_tests(test_results)
115  print '\n==============\n|| SYSTEM ||\n=============='
116  print 'Passed ',passes,' tests\nFailed ',fails,' tests\n',warns,' warnings\n\n\n'
117  return (passes,fails,warns)
118 
119 def main():
120  test_system(True)
121 
122 
123 if __name__ == '__main__':
124  main()
125