Belle II Software development
module_statistics.py
1#!/usr/bin/env python3
2
3
10
11
15
16from basf2 import Path, process, statistics
17
18# Create main path
19main = Path()
20
21# EventInfoSetter - generate event meta data
22eventinfosetter = 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
27eventinfo = main.add_module('EventInfoPrinter')
28
29# Process all events
30process(main)
31
32# Print basic event statistics to stdout
33print('Event Statistics:')
34print(statistics)
35
36# Print basic event statistics for specific modules
37print('Event Statistics for selected modules:')
38print(statistics(modules=[eventinfosetter]))
39
40# Print statistics adding all counters
41print('Total processing times:')
42print(statistics(statistics.TOTAL))
43
44# Make a list of all available statistic counters
45statistic_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
55for 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
64print('Memory statistics')
65for 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
72stats = statistics.get(eventinfosetter)
73eventinfo_total = stats.time_sum(statistics.TOTAL)
74print(f'EventInfoSetter needed {eventinfo_total / 1e6:.3f} ms in total')
75
76# Print total processing time
77framework_total = statistics.get_global().time_sum(statistics.TOTAL)
78print(f'Total processing time: {framework_total / 1e6:.3f} ms')
79
80# Calculate estimate for framework overhead
81modules_total = sum(e.time_sum(statistics.TOTAL) for e in statistics.modules)
82overhead = framework_total - modules_total
83print(f'Framework overhead: {overhead / 1e6:.3f} ms ({100 * overhead / framework_total:.2f} %)')
84print()
85
86# Clear statistics
87statistics.clear()
88print('Empty statistics')
89print(statistics)