1 ##############################################################################
2 # Copyright (c) 2016 ZTE Corporation and others.
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
11 PoC of performance profiler for OPNFV doctor project
15 Export environment variables to set timestamp at each checkpoint in millisecond.
16 Valid check points are: DOCTOR_PROFILER_T{00-09}
18 See also: https://goo.gl/98Osig
24 OUTPUT = 'doctor_profiling_output'
25 PREFIX = 'DOCTOR_PROFILER'
26 TOTAL_CHECK_POINTS = 10
27 MODULE_CHECK_POINTS = ['T00', 'T01', 'T04', 'T05', 'T06', 'T09']
29 # Inspired by https://github.com/reorx/httpstat
31 Total time cost: {total}(ms)
32 ==============================================================================>
33 |Monitor|Inspector |Controller|Notifier|Evaluator |
34 |{M00} |{M01} |{M02} |{M03} |{M04} |
36 link down:{T00}| | | | | | | | |
37 raw failure:{T01}| | | | | | | |
38 found affected:{T02}| | | | | | |
39 set VM error:{T03}| | | | | |
40 marked host down:{T04}| | | | |
41 notified VM error:{T05} | | | |
42 transformed event:{T06}| | |
43 evaluated event:{T07}| |
50 check_points = ["T{:02d}".format(i) for i in range(TOTAL_CHECK_POINTS)]
51 module_map = {"M{:02d}".format(i):
52 (MODULE_CHECK_POINTS[i], MODULE_CHECK_POINTS[i + 1])
53 for i in range(len(MODULE_CHECK_POINTS) - 1)}
56 elapsed_ms = {cp: os.getenv("{}_{}".format(PREFIX, cp))
57 for cp in check_points}
60 return TAG_FORMAT.format(tag or '?')
62 tags = {cp: format_tag(ms) for cp, ms in elapsed_ms.iteritems()}
65 if elapsed_ms[cp[0]] and elapsed_ms[cp[1]]:
66 return int(elapsed_ms[cp[1]]) - int(elapsed_ms[cp[0]])
70 # module time cost tags
71 modules_cost_ms = {module: time_cost(cp)
72 for module, cp in module_map.iteritems()}
74 tags.update({module: format_tag(cost)
75 for module, cost in modules_cost_ms.iteritems()})
77 tags.update({'total': time_cost((check_points[0], check_points[-1]))})
79 profile = TEMPLATE.format(**tags)
81 logfile = open('{}.json'.format(OUTPUT), 'w')
82 logfile.write(json.dumps(tags))
86 if __name__ == '__main__':