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