Best Python code snippet using lisa_python
common.py
Source:common.py
...329 ntttcp.restore_system(udp_mode)330 for lagscope in [client_lagscope, server_lagscope]:331 lagscope.restore_busy_poll()332 return perf_ntttcp_message_list333def perf_iperf(334 test_result: TestResult,335 connections: List[int],336 buffer_length_list: List[int],337 udp_mode: bool = False,338) -> None:339 environment = test_result.environment340 assert environment, "fail to get environment from testresult"341 client = cast(RemoteNode, environment.nodes[0])342 server = cast(RemoteNode, environment.nodes[1])343 client_iperf3 = client.tools[Iperf3]344 server_iperf3 = server.tools[Iperf3]345 test_case_name = inspect.stack()[1][3]346 iperf3_messages_list: List[Any] = []347 if udp_mode:...
perf_test.py
Source:perf_test.py
1import time2import unittest3import select4import datetime5import paramiko6server_list = {7 "controller": {8 "ipaddr": "10.10.10.18",9 "user": "root",10 "password": "tester"11 },12 "node1": {13 "ipaddr": "10.10.10.14",14 "user": "root",15 "password": "tester",16 "vm_ip": "169.254.0.6"17 },18 "node2": {19 "ipaddr": "10.10.10.95",20 "user": "root",21 "password": "tester",22 "vm_ip": "169.254.0.13"23 }24 }25test_VMs = ["VM2","VM3"]26class TFVPPVTestCase(unittest.TestCase):27 """Test throughput in VM(virtual machine) <-> Physical NIC <-> Physical NIC <-> VM (VPPV) topology28 """29 def _get_VMs_list(self, controller_client):30 stdin, stdout, stderr = controller_client.exec_command("nova list", timeout=30)31 32 stdout_lines = stdout.readlines()33 stderr_lines = stderr.readlines()34 35 if len(stdout_lines) == 0 or len(stderr_lines) > 0:36 for line in stderr_lines: 37 print line38 raise BaseException("Execute 'nova list' error")39 40 vm_list = []41 42 for line in stdout_lines[3:-1]: 43 vm_fields = line.split("|")44 vm = {"name": vm_fields[2].strip(),45 "status": vm_fields[3].strip()}46 vm_list.append(vm)47 return vm_list48 def _start_VM(self, controller_client, vm_name, skip_err=True):49 stdin, stdout, stderr = controller_client.exec_command("nova start " + vm_name)50 stdout_lines = stdout.readlines()51 stderr_lines = stderr.readlines()52 53 if len(stdout_lines) == 0 or len(stderr_lines) > 0:54 for line in stderr_lines: 55 print line56 if skip_err == False:57 raise BaseException("Execute 'nova start' error, vm=" + vm_name)58 print stdout_lines59 def _stop_VM(self, controller_client, vm_name, skip_err=True):60 stdin, stdout, stderr = controller_client.exec_command("nova stop " + vm_name)61 stdout_lines = stdout.readlines()62 stderr_lines = stderr.readlines()63 64 if len(stdout_lines) == 0 or len(stderr_lines) > 0:65 for line in stderr_lines: 66 print line67 if skip_err == False:68 raise BaseException("Execute 'nova stop ' error, vm=" + vm_name)69 print stdout_lines70 def _update_vrouter(self, node_client, vrouter_path):71 sftp = node_client.open_sftp()72 sftp.put(vrouter_path, './contrail-vrouter-dpdk-ci')73 sftp.chmod('./contrail-vrouter-dpdk-ci', 755)74 print "Start to update vrouter binary" 75 # TODO: Replace hardcoded core numbers ("10-17" in the command below) with input from a config file or environment76 # in order to support hardware platforms with different CPU core topologies77 stdin, stdout, stderr = node_client.exec_command("./change_vrouter_cores.sh contrail-vrouter-dpdk-ci 10-17", timeout=120)78 for line in stdout.readlines():79 print line80 print "Check the contrail-vrouter status, wait unit it becomes active"81 for i in range(5):82 stdin, stdout, stderr = node_client.exec_command("contrail-status", timeout=30)83 for line in stdout.readlines():84 print line85 if "agent: active" in line:86 print "vRotuer launched"87 return True88 time.sleep(30)89 raise BaseException("Deploy vRouter binary fail")90 def setUp(self):91 # Make sure /etc/kolla/admin-openrc.sh is executed in bashrc92 # Then it could be support nova command line93 print "Create ssh connection to controller"94 self.__srv_controller = paramiko.SSHClient()95 self.__srv_controller.set_missing_host_key_policy(paramiko.AutoAddPolicy())96 self.__srv_controller.connect(server_list["controller"]["ipaddr"],97 username=server_list["controller"]["user"],98 password=server_list["controller"]["password"])99 print "Create ssh connection to node 1"100 self.__srv_node1 = paramiko.SSHClient()101 self.__srv_node1.set_missing_host_key_policy(paramiko.AutoAddPolicy())102 self.__srv_node1.connect(server_list["node1"]["ipaddr"],103 username=server_list["node1"]["user"],104 password=server_list["node1"]["password"])105 print "Create ssh connection to node 2"106 self.__srv_node2 = paramiko.SSHClient()107 self.__srv_node2.set_missing_host_key_policy(paramiko.AutoAddPolicy())108 self.__srv_node2.connect(server_list["node2"]["ipaddr"],109 username=server_list["node2"]["user"],110 password=server_list["node2"]["password"])111 # Update vRouter-dpdk binary to computer node112 self._update_vrouter(self.__srv_node1, "./contrail-vrouter-dpdk")113 self._update_vrouter(self.__srv_node2, "./contrail-vrouter-dpdk")114 time.sleep(60)115 116 print "Check the VMs status"117 VMs = self._get_VMs_list(self.__srv_controller)118 print VMs119 for vm in VMs:120 if vm["name"] in test_VMs:121 self._start_VM(self.__srv_controller, vm["name"])122 time.sleep(60) 123 print "Wait 120 secs for VMs boot up." 124 time.sleep(120)125 VMs = self._get_VMs_list(self.__srv_controller)126 for vm in VMs:127 if vm["name"] in test_VMs:128 if vm["status"] != "ACTIVE":129 print "Launch VM failure, vm=" + vm["name"]130 raise BaseException("Launch VM failure, vm=" + vm["name"])131 def testPMDThroughput(self):132 """Test DPDK PMD(polling mode driver) using TREX traffic generator https://trex-tgn.cisco.com/133 """134 print "PMD Throughput"135 try:136 chnn_trex = self.__srv_node1.invoke_shell()137 chnn_trex.send("ssh root@{0}\n".format(server_list["node1"]["vm_ip"]))138 time.sleep(10)139 chnn_trex.send("./run_test_trex.sh\n")140 time.sleep(20) 141 chnn_testpmd = self.__srv_node2.invoke_shell()142 chnn_testpmd.send("ssh root@{0}\n".format(server_list["node2"]["vm_ip"]))143 time.sleep(10)144 chnn_testpmd.send("./run_test_pmd.sh\n")145 time.sleep(20)146 chnn_tester = self.__srv_node1.invoke_shell()147 chnn_tester.send("ssh root@{0}\n".format(server_list["node1"]["vm_ip"]))148 chnn_tester.send("cd tra;python -u test.py\n")149 isDone = False150 result = ""151 152 start_time = datetime.datetime.now()153 timeout = 3600 154 while not isDone:155 end_time = datetime.datetime.now()156 if (end_time-start_time).seconds > timeout:157 print "PMD throuputh test timeout"158 raise BaseException("PMD throuputh test timeout")159 rl, wl, xl = select.select([chnn_trex, chnn_testpmd, chnn_tester], [], [], 0.0)160 if len(rl) > 0:161 for chnn in rl:162 out = chnn.recv(1024)163 if chnn is chnn_tester:164 result = result + out165 print str(out)166 if result.find("Done") != -1:167 isDone = True168 169 print result 170 txt_perf_result = result.split("\r\n")[-3].split("=>")[1].strip()171 perf_num = int(txt_perf_result.split(" ")[0])*4172 perf_unit = txt_perf_result.split(" ")[1]173 with open("perf_pmd.xml","w") as xml_result:174 xml_result.write('<report name="PMDThroughputTest" categ="performance">')175 xml_result.write('<test name="PMDThroughput" executed="yes">')176 xml_result.write('<result>')177 xml_result.write('<success passed="yes" state="100"/>')178 xml_result.write('<performance unit="' + perf_unit + 179 '" mesure="' + str(perf_num) + 180 '" isRelevant="true"/>')181 xml_result.write('</result>')182 xml_result.write('</test>')183 xml_result.write('</report>')184 except Exception, ex:185 with open("perf_pmd.xml","w") as xml_result:186 xml_result.write('<report name="PMDThroughputTest" categ="performance">')187 xml_result.write('<test name="PMDThroughput" executed="yes">')188 xml_result.write('<result>')189 xml_result.write('<success passed="no" state="0"/>')190 xml_result.write('</result>')191 xml_result.write('</test>')192 xml_result.write('</report>')193 finally:194 chnn_tester.close()195 chnn_testpmd.close()196 chnn_trex.close()197 def testiPerfTCPThroughput(self):198 """Test virtio throughput (kernel vRouter) using iperf traffic generator https://iperf.fr/199 """200 print "iPerf TCP Throughput"201 try:202 chnn_srv = self.__srv_node1.invoke_shell()203 chnn_srv.send("ssh root@{0}\n".format(server_list["node1"]["vm_ip"]))204 time.sleep(10)205 chnn_srv.send("iperf3 -s\n")206 time.sleep(5) 207 chnn_clt = self.__srv_node2.invoke_shell()208 chnn_clt.send("ssh root@{0}\n".format(server_list["node2"]["vm_ip"]))209 time.sleep(10)210 chnn_clt.send("iperf3 -c 1.1.1.3 -P 16 -i 5 -t 30\n")211 time.sleep(5)212 isDone = False213 result = ""214 start_time = datetime.datetime.now()215 timeout = 120216 while not isDone:217 if chnn_clt.exit_status_ready():218 break219 220 end_time = datetime.datetime.now()221 if (end_time-start_time).seconds > timeout:222 print "iPerf TCP throuputh test timeout"223 raise BaseException("iPerf TCP throuputh test timeout")224 rl, wl, xl = select.select([chnn_srv, chnn_clt], [], [], 0.0)225 if len(rl) > 0:226 for chnn in rl:227 out = chnn.recv(1024)228 if chnn is chnn_clt:229 result = result + out230 print str(out)231 if result.find("Done") != -1:232 isDone = True233 print result234 result_throughput = result.split("\r\n")[-4].split(" ")[4].strip()235 perf_num, perf_unit = result_throughput.split(" ")236 237 with open("perf_iperf.xml","w") as xml_result:238 xml_result.write('<report name="iPerfThroughputTest" categ="performance">')239 xml_result.write('<test name="iPerfThroughput" executed="yes">')240 xml_result.write('<result>')241 xml_result.write('<success passed="yes" state="100"/>')242 xml_result.write('<performance unit="' + perf_unit + 243 '" mesure="' + str(perf_num) + 244 '" isRelevant="true"/>')245 xml_result.write('</result>')246 xml_result.write('</test>')247 xml_result.write('</report>')248 249 except Exception, ex:250 with open("perf_iperf.xml","w") as xml_result:251 xml_result.write('<report name="iPerfThroughputTest" categ="performance">')252 xml_result.write('<test name="iPerfThroughput" executed="yes">')253 xml_result.write('<result>')254 xml_result.write('<success passed="no" state="0"/>')255 xml_result.write('</result>')256 xml_result.write('</test>')257 xml_result.write('</report>')258 259 finally:260 chnn_clt.close()261 chnn_srv.close()262 def tearDown(self):263 VMs = self._get_VMs_list(self.__srv_controller)264 for vm in VMs:265 if vm["name"] in test_VMs:266 self._stop_VM(self.__srv_controller, vm["name"])267 time.sleep(60)268 time.sleep(120)269 VMs = self._get_VMs_list(self.__srv_controller)270 for vm in VMs:271 if vm["name"] in test_VMs:272 if vm["status"] != "SHUTOFF":273 raise BaseException("Stop VM failure, vm=" + vm["name"])274 #self.__srv_controller.close()275 #self.__srv_node1.close()276 #self.__srv_node2.close()277if __name__ == "__main__":278# unittest.main()279 tests = [280 'testiPerfTCPThroughput', 281 'testPMDThroughput',282 ]283 suite = unittest.TestSuite(map(TFVPPVTestCase, tests))284 results = unittest.TestResult()...
networkperf.py
Source:networkperf.py
...191 network_interface=Synthetic(),192 ),193 )194 def perf_tcp_iperf_synthetic(self, result: TestResult) -> None:195 perf_iperf(196 result,197 connections=IPERF_TCP_CONCURRENCY,198 buffer_length_list=IPERF_TCP_BUFFER_LENGTHS,199 )200 @TestCaseMetadata(201 description="""202 This test case uses iperf3 to test sriov tcp network throughput.203 """,204 priority=3,205 timeout=TIMEOUT,206 requirement=simple_requirement(207 min_count=2,208 network_interface=Sriov(),209 ),210 )211 def perf_tcp_iperf_sriov(self, result: TestResult) -> None:212 perf_iperf(213 result,214 connections=IPERF_TCP_CONCURRENCY,215 buffer_length_list=IPERF_TCP_BUFFER_LENGTHS,216 )217 @TestCaseMetadata(218 description="""219 This test case uses iperf to test synthetic udp network throughput.220 """,221 priority=3,222 timeout=TIMEOUT,223 requirement=simple_requirement(224 min_count=2,225 network_interface=Synthetic(),226 ),227 )228 def perf_udp_iperf_synthetic(self, result: TestResult) -> None:229 perf_iperf(230 result,231 connections=IPERF_UDP_CONCURRENCY,232 buffer_length_list=IPERF_UDP_BUFFER_LENGTHS,233 udp_mode=True,234 )235 @TestCaseMetadata(236 description="""237 This test case uses iperf to test sriov udp network throughput.238 """,239 priority=3,240 timeout=TIMEOUT,241 requirement=simple_requirement(242 min_count=2,243 network_interface=Sriov(),244 ),245 )246 def perf_udp_iperf_sriov(self, result: TestResult) -> None:247 perf_iperf(248 result,249 connections=IPERF_UDP_CONCURRENCY,250 buffer_length_list=IPERF_UDP_BUFFER_LENGTHS,251 udp_mode=True,252 )253 def after_case(self, log: Logger, **kwargs: Any) -> None:254 environment: Environment = kwargs.pop("environment")255 for process in ["lagscope", "netperf", "netserver", "ntttcp", "iperf3"]:...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!