refactor failure inject
[doctor.git] / 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 OUTPUT = 'doctor_profiling_output'
25 PREFIX = 'DOCTOR_PROFILER'
26 TOTAL_CHECK_POINTS = 10
27 MODULE_CHECK_POINTS = ['T00', 'T01', 'T04', 'T05', 'T06', 'T09']
28 TAG_FORMAT = "{:<5}"
29 # Inspired by https://github.com/reorx/httpstat
30 TEMPLATE = """
31 Total time cost: {total}(ms)
32 ==============================================================================>
33        |Monitor|Inspector           |Controller|Notifier|Evaluator           |
34        |{M00}  |{M01}               |{M02}     |{M03}   |{M04}               |
35        |       |      |      |      |          |        |      |      |      |
36 link down:{T00}|      |      |      |          |        |      |      |      |
37      raw failure:{T01}|      |      |          |        |      |      |      |
38          found affected:{T02}|      |          |        |      |      |      |
39                   set VM error:{T03}|          |        |      |      |      |
40                          marked host down:{T04}|        |      |      |      |
41                                notified VM error:{T05}  |      |      |      |
42                                         transformed event:{T06}|      |      |
43                                                  evaluated event:{T07}|      |
44                                                             fired alarm:{T08}|
45                                                                 received alarm:{T09}
46 """
47
48
49 def main():
50     check_points = ["T{:02d}".format(i) for i in range(TOTAL_CHECK_POINTS)]
51     module_map = {"M{:02d}".format(i):
52                   (MODULE_CHECK_POINTS[i], MODULE_CHECK_POINTS[i + 1])
53                   for i in range(len(MODULE_CHECK_POINTS) - 1)}
54
55     # check point tags
56     elapsed_ms = {cp: os.getenv("{}_{}".format(PREFIX, cp))
57                   for cp in check_points}
58
59     def format_tag(tag):
60         return TAG_FORMAT.format(tag or '?')
61
62     tags = {cp: format_tag(ms) for cp, ms in elapsed_ms.iteritems()}
63
64     def time_cost(cp):
65         if elapsed_ms[cp[0]] and elapsed_ms[cp[1]]:
66             return int(elapsed_ms[cp[1]]) - int(elapsed_ms[cp[0]])
67         else:
68             return None
69
70     # module time cost tags
71     modules_cost_ms = {module: time_cost(cp)
72                        for module, cp in module_map.iteritems()}
73
74     tags.update({module: format_tag(cost)
75                  for module, cost in modules_cost_ms.iteritems()})
76
77     tags.update({'total': time_cost((check_points[0], check_points[-1]))})
78
79     profile = TEMPLATE.format(**tags)
80
81     logfile = open('{}.json'.format(OUTPUT), 'w')
82     logfile.write(json.dumps(tags))
83
84     print profile
85
86 if __name__ == '__main__':
87     main()