b3642085d76112c11200bf8f75b2f98f728c71d4
[functest-kubernetes.git] / functest_kubernetes / netperf / netperf.py
1 #!/usr/bin/env python
2
3 # Copyright (c) 2021 Orange and others.
4 #
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
9
10 """
11 Benchmarking Kubernetes Networking Performance
12 """
13
14 import glob
15 import logging
16 import os
17 from pathlib import Path
18 import shutil
19 import subprocess
20 import time
21
22 from xtesting.core import testcase
23
24
25 class Netperf(testcase.TestCase):
26     """Run Benchmarking Kubernetes Networking Performance"""
27
28     __logger = logging.getLogger(__name__)
29
30     def __init__(self, **kwargs):
31         super().__init__(**kwargs)
32         self.output_log_name = 'functest-kubernetes.log'
33         self.output_debug_log_name = 'functest-kubernetes.debug.log'
34
35     def check_requirements(self):
36         """Check if launch is in $PATH"""
37         self.is_skipped = not (
38             shutil.which("launch") and shutil.which("plotperf"))
39         if self.is_skipped:
40             self.__logger.warning("launch or plotperf is missing")
41
42     def run(self, **kwargs):
43         self.start_time = time.time()
44         try:
45             if not os.path.exists(self.res_dir):
46                 os.makedirs(self.res_dir)
47             os.chdir(self.res_dir)
48             cmd = ['launch', '-iterations', '1', '-kubeConfig',
49                    f'{Path.home()}/.kube/config', '-v', '3']
50             output = subprocess.check_output(
51                 cmd, stderr=subprocess.STDOUT, timeout=3600)
52             self.__logger.info("%s\n%s", " ".join(cmd), output.decode("utf-8"))
53             lfiles = glob.glob(os.path.join(
54                 'results_netperf-latest', 'netperf-latest*.csv'))
55             results = max(lfiles, key=os.path.getmtime)
56             cmd = ['plotperf', '-c', results,
57                    '-o', self.res_dir, '-s', 'netperf']
58             output = subprocess.check_output(
59                 cmd, stderr=subprocess.STDOUT, timeout=60)
60             self.__logger.info("%s\n%s", " ".join(cmd), output.decode("utf-8"))
61             self.result = 100
62             status = testcase.TestCase.EX_OK
63         except subprocess.SubprocessError as exc:
64             self.__logger.exception(
65                 "Cannot run Netperf:\n%s", ' '.join(exc.cmd),
66                 exc.output.decode("utf-8"))
67             self.result = 0
68             status = testcase.TestCase.EX_RUN_ERROR
69         self.stop_time = time.time()
70         return status