Merge "vswitches: Affinitize vswitch threads for OVS-DPDK"
[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 csv
25 import logging
26
27 from collections import OrderedDict
28 from core.results.results_constants import ResultsConstants
29 from conf import settings
30 from tools import systeminfo
31
32 _TEMPLATE_FILE = 'report.jinja'
33 _ROOT_DIR = os.path.normpath(os.path.dirname(os.path.realpath(__file__)))
34
35
36 def _get_env():
37     """
38     Get system configuration.
39
40     :returns: Return a dictionary of the test environment.
41               The following is an example return value:
42                {'kernel': '3.10.0-229.4.2.el7.x86_64',
43                 'os': 'OS Version',
44                 'cpu': ' CPU 2.30GHz',
45                 'platform': '[2 sockets]',
46                 'nic': 'NIC'}
47
48     """
49
50     env = {
51         'os': systeminfo.get_os(),
52         'kernel': systeminfo.get_kernel(),
53         'nic': systeminfo.get_nic(),
54         'cpu': systeminfo.get_cpu(),
55         'cpu_cores': systeminfo.get_cpu_cores(),
56         'memory' : systeminfo.get_memory(),
57         'platform': systeminfo.get_platform(),
58     }
59
60     return env
61
62
63 def _get_results(results_file):
64     """Get results from tests.
65
66     Get test results from a CSV file and return it as a list
67     of dictionaries for each row of data.
68
69     :param results_file: Path of the CSV results file
70
71     :returns: List of test results
72     """
73     with open(results_file, 'r') as csvfile:
74         reader = csv.reader(csvfile, delimiter=',')
75         result = []
76         res_head = next(reader)
77         for res_row in reader:
78             result.append(OrderedDict(zip(list(res_head), list(res_row))))
79
80     return result
81
82
83 def generate(testcases, input_file):
84     """Generate actual report.
85
86     Generate a Markdown-formatted file using results of tests and some
87     parsed system info.
88
89     :param input_file: Path to CSV results file
90
91     :returns: Path to generated report
92     """
93     output_file = '.'.join([os.path.splitext(input_file)[0], 'md'])
94
95     template_loader = jinja2.FileSystemLoader(searchpath=_ROOT_DIR)
96     template_env = jinja2.Environment(loader=template_loader)
97     template = template_env.get_template(_TEMPLATE_FILE)
98
99     tests = []
100     try:
101         for result in _get_results(input_file):
102             test_config = {}
103             for tc_conf in testcases:
104                 if tc_conf['Name'] == result[ResultsConstants.ID]:
105                     test_config = tc_conf
106                     break
107
108             # remove id and deployment from results but store their values
109             tc_id = result[ResultsConstants.ID]
110             tc_deployment = result[ResultsConstants.DEPLOYMENT]
111             del result[ResultsConstants.ID]
112             del result[ResultsConstants.DEPLOYMENT]
113
114             # pass test results, env details and configuration to template
115             tests.append({
116                 'ID': tc_id.upper(),
117                 'id': tc_id,
118                 'deployment': tc_deployment,
119                 'conf': test_config,
120                 'result': result,
121                 'env': _get_env(),
122             })
123
124         template_vars = {
125             'tests': tests,
126         }
127
128         output_text = template.render(template_vars)
129         with open(output_file, 'w') as file_:
130             file_.write(output_text)
131             logging.info('Test report written to "%s"', output_file)
132
133     except KeyError:
134         logging.info("Report: Ignoring file (Wrongly defined columns): %s", (input_file))
135         raise
136     return output_file
137
138
139 if __name__ == '__main__':
140     settings.load_from_dir('conf')
141     OUT = generate(sys.argv[1])
142     print('Test report written to "%s"...' % OUT)