Add Pylint to VSPERF commit gate
[vswitchperf.git] / tools / report / report.py
1 # Copyright 2015 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
28 from tools import systeminfo
29
30 _TEMPLATE_FILE = 'report.jinja'
31 _ROOT_DIR = os.path.normpath(os.path.dirname(os.path.realpath(__file__)))
32
33
34 def _get_env():
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         'nic': 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     }
57
58     return env
59
60
61 def generate(input_file, tc_results, tc_stats):
62     """Generate actual report.
63
64     Generate a Markdown-formatted file using results of tests and some
65     parsed system info.
66
67     :param input_file: Path to CSV results file
68
69     :returns: Path to generated report
70     """
71     output_file = '.'.join([os.path.splitext(input_file)[0], 'md'])
72
73     template_loader = jinja2.FileSystemLoader(searchpath=_ROOT_DIR)
74     template_env = jinja2.Environment(loader=template_loader)
75     template = template_env.get_template(_TEMPLATE_FILE)
76
77     tests = []
78     try:
79         for result in tc_results:
80             test_config = {}
81             for tc_conf in settings.getValue('PERFORMANCE_TESTS'):
82                 if tc_conf['Name'] == result[ResultsConstants.ID]:
83                     test_config = tc_conf
84                     break
85
86             # remove id and deployment from results but store their values
87             tc_id = result[ResultsConstants.ID]
88             tc_deployment = result[ResultsConstants.DEPLOYMENT]
89             del result[ResultsConstants.ID]
90             del result[ResultsConstants.DEPLOYMENT]
91
92             # pass test results, env details and configuration to template
93             tests.append({
94                 'ID': tc_id.upper(),
95                 'id': tc_id,
96                 'deployment': tc_deployment,
97                 'conf': test_config,
98                 'result': result,
99                 'env': _get_env(),
100                 'stats': tc_stats
101             })
102
103         template_vars = {
104             'tests': tests,
105         }
106
107         output_text = template.render(template_vars) #pylint: disable=no-member
108         with open(output_file, 'w') as file_:
109             file_.write(output_text)
110             logging.info('Test report written to "%s"', output_file)
111
112     except KeyError:
113         logging.info("Report: Ignoring file (Wrongly defined columns): %s",
114                      (input_file))
115         raise
116     return output_file
117
118
119 if __name__ == '__main__':
120     settings.load_from_dir('conf')
121     OUT = generate(sys.argv[1], '', '')
122     print('Test report written to "%s"...' % OUT)