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