Belle II Software  release-05-02-19
module_statistics.py
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3 
4 
10 
11 from basf2 import Path, process, statistics
12 
13 # Create main path
14 main = Path()
15 
16 # EventInfoSetter - generate event meta data
17 eventinfosetter = main.add_module('EventInfoSetter',
18  expList=[71, 71, 73, 73, 73],
19  runList=[3, 4, 10, 20, 30],
20  evtNumList=[40, 600, 20, 500, 301])
21 # eventinfo - show event meta info
22 eventinfo = main.add_module('EventInfoPrinter')
23 
24 # Process all events
25 process(main)
26 
27 # Print basic event statistics to stdout
28 print('Event Statistics:')
29 print(statistics)
30 
31 # Print basic event statistics for specific modules
32 print('Event Statistics for selected modules:')
33 print(statistics([eventinfosetter]))
34 
35 # Print statistics adding all counters
36 print('Total processing times:')
37 print(statistics(statistics.TOTAL))
38 
39 # Change name of modules
40 statistics.set_name(eventinfosetter, 'Foo')
41 
42 # Make a list of all available statistic counters
43 statistic_counters = [
44  (statistics.INIT, "initialize()"),
45  (statistics.BEGIN_RUN, "beginRun()"),
46  (statistics.EVENT, "event()"),
47  (statistics.END_RUN, "endRun()"),
48  (statistics.TERM, "terminate()"),
49  (statistics.TOTAL, "*total*"),
50 ]
51 
52 # Print detailed statistics for each module
53 for stats in statistics.modules:
54  print('Module %s:' % stats.name)
55  for stat_counter, stat_name in statistic_counters:
56  print(' -> %12s: %10.3f ms, %4d calls, %10.3f +-%10.3f ms/call' % (
57  stat_name,
58  # Time is in the default time unit which happens to be ns
59  stats.time_sum(stat_counter) / 1e6,
60  stats.calls(stat_counter),
61  stats.time_mean(stat_counter) / 1e6,
62  stats.time_stddev(stat_counter) / 1e6,
63  ))
64  print()
65 
66 print('Memory statistics')
67 for stats in statistics.modules:
68  print('Module %s:' % stats.name)
69  for stat_counter, stat_name in statistic_counters:
70  print(' -> %12s: %10d KB, %4d calls, %10d +-%10.3f KB/call' % (
71  stat_name,
72  stats.memory_sum(stat_counter),
73  stats.calls(stat_counter),
74  stats.memory_mean(stat_counter),
75  stats.memory_stddev(stat_counter),
76  ))
77 
78 # Get Statistics for single module
79 stats = statistics.get(eventinfosetter)
80 eventinfo_total = stats.time_sum(statistics.TOTAL)
81 print('EventInfoSetter needed %.3f ms in total' % (eventinfo_total / 1e6))
82 # Now we can also set the name since we have the correct object
83 stats.name = "FooBar"
84 
85 # Print total processing time
86 framework_total = statistics.get_global().time_sum(statistics.TOTAL)
87 print('Total processing time: %.3f ms' % (framework_total / 1e6))
88 
89 # Calculate estimate for framework overhead
90 modules_total = sum(e.time_sum(statistics.TOTAL) for e in statistics.modules)
91 overhead = framework_total - modules_total
92 print('Framework overhead: %.3f ms (%.2f %%)' % (
93  overhead / 1e6,
94  100 * overhead / framework_total,
95 ))
96 print()
97 
98 # Clear statistics
99 statistics.clear()
100 print('Empty statistics')
101 print(statistics)