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
16 checkpoint in millisecond.
17 Valid check points are: DOCTOR_PROFILER_T{00-09}
19 See also: https://goo.gl/98Osig
25 from oslo_config import cfg
29 cfg.StrOpt('profiler_type',
30 default=os.environ.get('PROFILER_TYPE', 'poc'),
31 help='the type of installer'),
35 OUTPUT = 'doctor_profiling_output'
36 PREFIX = 'DOCTOR_PROFILER'
37 TOTAL_CHECK_POINTS = 10
38 MODULE_CHECK_POINTS = ['T00', 'T01', 'T04', 'T05', 'T06', 'T09']
40 # Inspired by https://github.com/reorx/httpstat
42 Total time cost: {total}(ms)
43 ==============================================================================>
44 |Monitor|Inspector |Controller|Notifier|Evaluator |
45 |{M00} |{M01} |{M02} |{M03} |{M04} |
47 link down:{T00}| | | | | | | | |
48 raw failure:{T01}| | | | | | | |
49 found affected:{T02}| | | | | | |
50 set VM error:{T03}| | | | | |
51 marked host down:{T04}| | | | |
52 notified VM error:{T05} | | | |
53 transformed event:{T06}| | |
54 evaluated event:{T07}| |
61 check_points = ["T{:02d}".format(i) for i in range(TOTAL_CHECK_POINTS)]
62 module_map = {"M{:02d}".format(i):
63 (MODULE_CHECK_POINTS[i], MODULE_CHECK_POINTS[i + 1])
64 for i in range(len(MODULE_CHECK_POINTS) - 1)}
67 elapsed_ms = {cp: os.getenv("{}_{}".format(PREFIX, cp))
68 for cp in check_points}
71 return TAG_FORMAT.format(tag or '?')
73 tags = {cp: format_tag(ms) for cp, ms in elapsed_ms.items()}
76 if elapsed_ms[cp[0]] and elapsed_ms[cp[1]]:
77 return int(elapsed_ms[cp[1]]) - int(elapsed_ms[cp[0]])
81 # module time cost tags
82 modules_cost_ms = {module: time_cost(cp)
83 for module, cp in module_map.items()}
85 tags.update({module: format_tag(cost)
86 for module, cost in modules_cost_ms.items()})
88 tags.update({'total': time_cost((check_points[0], check_points[-1]))})
90 profile = TEMPLATE.format(**tags)
92 logfile = open('{}.json'.format(OUTPUT), 'w')
93 logfile.write(json.dumps(tags))
97 log.info('%s' % profile)
100 if __name__ == '__main__':