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
23 PREFIX = 'DOCTOR_PROFILER'
24 TOTAL_CHECK_POINTS = 10
25 MODULE_CHECK_POINTS = ['T00', 'T01', 'T04', 'T05', 'T06', 'T09']
27 # Inspired by https://github.com/reorx/httpstat
29 Total time cost: {total}(ms)
30 ==============================================================================>
31 |Monitor|Inspector |Controller|Notifier|Evaluator |
32 |{M00} |{M01} |{M02} |{M03} |{M04} |
34 host down:{T00}| | | | | | | | |
35 raw failure:{T01}| | | | | | | |
36 found affected:{T02}| | | | | | |
37 marked host down:{T03}| | | | | |
38 set VM error:{T04} | | | | |
39 notified VM error:{T05} | | | |
40 transformed event:{T06}| | |
41 evaluated event:{T07}| |
48 check_points = ["T{:02d}".format(i) for i in range(TOTAL_CHECK_POINTS)]
49 module_map = {"M{:02d}".format(i):
50 (MODULE_CHECK_POINTS[i], MODULE_CHECK_POINTS[i + 1])
51 for i in range(len(MODULE_CHECK_POINTS) - 1)}
54 elapsed_ms = {cp: os.getenv("{}_{}".format(PREFIX, cp))
55 for cp in check_points}
58 return TAG_FORMAT.format(tag or '?')
60 tags = {cp: format_tag(ms) for cp, ms in elapsed_ms.iteritems()}
63 if elapsed_ms[cp[0]] and elapsed_ms[cp[1]]:
64 return int(elapsed_ms[cp[1]]) - int(elapsed_ms[cp[0]])
68 # module time cost tags
69 modules_cost_ms = {module: time_cost(cp)
70 for module, cp in module_map.iteritems()}
72 tags.update({module: format_tag(cost)
73 for module, cost in modules_cost_ms.iteritems()})
75 tags.update({'total': time_cost((check_points[0], check_points[-1]))})
77 profile = TEMPLATE.format(**tags)
81 if __name__ == '__main__':