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