Support Apex with services in containers
[doctor.git] / doctor_tests / profiler_poc.py
1 ##############################################################################
2 # Copyright (c) 2016 ZTE Corporation and others.
3 #
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 ##############################################################################
9
10 """
11 PoC of performance profiler for OPNFV doctor project
12
13 Usage:
14
15 Export environment variables to set timestamp at each
16 checkpoint in millisecond.
17 Valid check points are: DOCTOR_PROFILER_T{00-09}
18
19 See also: https://goo.gl/98Osig
20 """
21
22 import json
23 import os
24
25 from oslo_config import cfg
26
27
28 OPTS = [
29     cfg.StrOpt('profiler_type',
30                default=os.environ.get('PROFILER_TYPE', 'poc'),
31                help='the type of installer'),
32 ]
33
34
35 OUTPUT = 'doctor_profiling_output'
36 PREFIX = 'DOCTOR_PROFILER'
37 TOTAL_CHECK_POINTS = 10
38 MODULE_CHECK_POINTS = ['T00', 'T01', 'T04', 'T05', 'T06', 'T09']
39 TAG_FORMAT = "{:<5}"
40 # Inspired by https://github.com/reorx/httpstat
41 TEMPLATE = """
42 Total time cost: {total}(ms)
43 ==============================================================================>
44        |Monitor|Inspector           |Controller|Notifier|Evaluator           |
45        |{M00}  |{M01}               |{M02}     |{M03}   |{M04}               |
46        |       |      |      |      |          |        |      |      |      |
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}|      |
55                                                             fired alarm:{T08}|
56                                                                 received alarm:{T09}
57 """    # noqa
58
59
60 def main(log=None):
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)}
65
66     # check point tags
67     elapsed_ms = {cp: os.getenv("{}_{}".format(PREFIX, cp))
68                   for cp in check_points}
69
70     def format_tag(tag):
71         return TAG_FORMAT.format(tag or '?')
72
73     tags = {cp: format_tag(ms) for cp, ms in elapsed_ms.items()}
74
75     def time_cost(cp):
76         if elapsed_ms[cp[0]] and elapsed_ms[cp[1]]:
77             return int(elapsed_ms[cp[1]]) - int(elapsed_ms[cp[0]])
78         else:
79             return None
80
81     # module time cost tags
82     modules_cost_ms = {module: time_cost(cp)
83                        for module, cp in module_map.items()}
84
85     tags.update({module: format_tag(cost)
86                  for module, cost in modules_cost_ms.items()})
87
88     tags.update({'total': time_cost((check_points[0], check_points[-1]))})
89
90     profile = TEMPLATE.format(**tags)
91
92     logfile = open('{}.json'.format(OUTPUT), 'w')
93     logfile.write(json.dumps(tags))
94
95     print(profile)
96     if log:
97         log.info('%s' % profile)
98
99
100 if __name__ == '__main__':
101     main()