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 from oslo_config import cfg
28 cfg.StrOpt('profiler_type',
29 default=os.environ.get('PROFILER_TYPE', 'poc'),
30 help='the type of installer'),
34 OUTPUT = 'doctor_profiling_output'
35 PREFIX = 'DOCTOR_PROFILER'
36 TOTAL_CHECK_POINTS = 10
37 MODULE_CHECK_POINTS = ['T00', 'T01', 'T04', 'T05', 'T06', 'T09']
39 # Inspired by https://github.com/reorx/httpstat
41 Total time cost: {total}(ms)
42 ==============================================================================>
43 |Monitor|Inspector |Controller|Notifier|Evaluator |
44 |{M00} |{M01} |{M02} |{M03} |{M04} |
46 link down:{T00}| | | | | | | | |
47 raw failure:{T01}| | | | | | | |
48 found affected:{T02}| | | | | | |
49 set VM error:{T03}| | | | | |
50 marked host down:{T04}| | | | |
51 notified VM error:{T05} | | | |
52 transformed event:{T06}| | |
53 evaluated event:{T07}| |
60 check_points = ["T{:02d}".format(i) for i in range(TOTAL_CHECK_POINTS)]
61 module_map = {"M{:02d}".format(i):
62 (MODULE_CHECK_POINTS[i], MODULE_CHECK_POINTS[i + 1])
63 for i in range(len(MODULE_CHECK_POINTS) - 1)}
66 elapsed_ms = {cp: os.getenv("{}_{}".format(PREFIX, cp))
67 for cp in check_points}
70 return TAG_FORMAT.format(tag or '?')
72 tags = {cp: format_tag(ms) for cp, ms in elapsed_ms.items()}
75 if elapsed_ms[cp[0]] and elapsed_ms[cp[1]]:
76 return int(elapsed_ms[cp[1]]) - int(elapsed_ms[cp[0]])
80 # module time cost tags
81 modules_cost_ms = {module: time_cost(cp)
82 for module, cp in module_map.items()}
84 tags.update({module: format_tag(cost)
85 for module, cost in modules_cost_ms.items()})
87 tags.update({'total': time_cost((check_points[0], check_points[-1]))})
89 profile = TEMPLATE.format(**tags)
91 logfile = open('{}.json'.format(OUTPUT), 'w')
92 logfile.write(json.dumps(tags))
96 log.info('%s' % profile)
99 if __name__ == '__main__':