Add simple VxLAN decapsulation performance test
[vswitchperf.git] / tools / report / report.py
1 # Copyright 2015-2016 Intel Corporation.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #   http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 """
16 vSwitch Characterization Report Generation.
17
18 Generate reports in format defined by X.
19 """
20
21 import sys
22 import os
23 import jinja2
24 import logging
25
26 from core.results.results_constants import ResultsConstants
27 from conf import settings as S
28 from tools import systeminfo
29
30 _TEMPLATE_FILES = ['report.jinja', 'report_rst.jinja']
31 _ROOT_DIR = os.path.normpath(os.path.dirname(os.path.realpath(__file__)))
32
33
34 def _get_env(result):
35     """
36     Get system configuration.
37
38     :returns: Return a dictionary of the test environment.
39               The following is an example return value:
40                {'kernel': '3.10.0-229.4.2.el7.x86_64',
41                 'os': 'OS Version',
42                 'cpu': ' CPU 2.30GHz',
43                 'platform': '[2 sockets]',
44                 'nic': 'NIC'}
45
46     """
47
48     env = {
49         'os': systeminfo.get_os(),
50         'kernel': systeminfo.get_kernel(),
51         'nics': systeminfo.get_nic(),
52         'cpu': systeminfo.get_cpu(),
53         'cpu_cores': systeminfo.get_cpu_cores(),
54         'memory' : systeminfo.get_memory(),
55         'platform': systeminfo.get_platform(),
56         'vsperf': systeminfo.get_version('vswitchperf'),
57         'traffic_gen': systeminfo.get_version(S.getValue('TRAFFICGEN')),
58         'vswitch': systeminfo.get_version(S.getValue('VSWITCH')),
59     }
60
61     if S.getValue('VSWITCH').lower().count('dpdk'):
62         env.update({'dpdk': systeminfo.get_version('dpdk')})
63
64     if result[ResultsConstants.DEPLOYMENT].count('v'):
65         env.update({'vnf': systeminfo.get_version(S.getValue('VNF')),
66                     'guest_image': S.getValue('GUEST_IMAGE'),
67                     'loopback_app': list(map(systeminfo.get_version, S.getValue('GUEST_LOOPBACK'))),
68                    })
69
70     return env
71
72
73 def generate(input_file, tc_results, tc_stats, performance_test=True):
74     """Generate actual report.
75
76     Generate a Markdown-formatted file using results of tests and some
77     parsed system info.
78
79     :param input_file: Path to CSV results file
80
81     :returns: Path to generated report
82     """
83     output_files = [('.'.join([os.path.splitext(input_file)[0], 'md'])),
84                     ('.'.join([os.path.splitext(input_file)[0], 'rst']))]
85     template_loader = jinja2.FileSystemLoader(searchpath=_ROOT_DIR)
86     template_env = jinja2.Environment(loader=template_loader)
87
88     tests = []
89     try:
90         for result in tc_results:
91             test_config = {}
92             if performance_test:
93                 for tc_conf in S.getValue('PERFORMANCE_TESTS'):
94                     if tc_conf['Name'] == result[ResultsConstants.ID]:
95                         test_config = tc_conf
96                         break
97             else:
98                 for tc_conf in S.getValue('INTEGRATION_TESTS'):
99                     if tc_conf['Name'] == result[ResultsConstants.ID]:
100                         test_config = tc_conf
101                         break
102
103             # pass test results, env details and configuration to template
104             tests.append({
105                 'ID': result[ResultsConstants.ID].upper(),
106                 'id': result[ResultsConstants.ID],
107                 'deployment': result[ResultsConstants.DEPLOYMENT],
108                 'conf': test_config,
109                 'result': result,
110                 'env': _get_env(result),
111                 'stats': tc_stats
112             })
113
114             # remove id and deployment from results before rendering
115             # but after _get_env() is called; tests dict has its shallow copy
116             del result[ResultsConstants.ID]
117             del result[ResultsConstants.DEPLOYMENT]
118
119         template_vars = {
120             'tests': tests,
121         }
122         i = 0
123         for output_file in output_files:
124             template = template_env.get_template(_TEMPLATE_FILES[i])
125             output_text = template.render(template_vars) #pylint: disable=no-member
126             with open(output_file, 'w') as file_:
127                 file_.write(output_text)
128                 logging.info('Test report written to "%s"', output_file)
129             i += 1
130
131     except KeyError:
132         logging.info("Report: Ignoring file (Wrongly defined columns): %s",
133                      (input_file))
134         raise
135     return output_files
136
137
138 if __name__ == '__main__':
139     S.load_from_dir('conf')
140     OUT = generate(sys.argv[1], '', '')
141     print('Test report written to "%s"...' % OUT)