Best Python code snippet using lisa_python
common.py
Source:common.py  
...175        ports = range(30000, 30001)176    for port in ports:177        server_netperf.run_as_server(port)178    for port in ports:179        client_netperf.run_as_client_async(server.internal_address, core_count, port)180    client_sar = client.tools[Sar]181    server_sar = server.tools[Sar]182    server_sar.get_statistics_async()183    result = client_sar.get_statistics()184    pps_message = client_sar.create_pps_performance_messages(185        result, inspect.stack()[1][3], test_type, test_result186    )187    notifier.notify(pps_message)188def perf_ntttcp(189    test_result: TestResult,190    server: Optional[RemoteNode] = None,191    client: Optional[RemoteNode] = None,192    udp_mode: bool = False,193    connections: Optional[List[int]] = None,194    test_case_name: str = "",195    server_nic_name: Optional[str] = None,196    client_nic_name: Optional[str] = None,197) -> List[Union[NetworkTCPPerformanceMessage, NetworkUDPPerformanceMessage]]:198    # Either server and client are set explicitly or we use the first two nodes199    # from the environment. We never combine the two options. We need to specify200    # server and client explicitly for nested VM's which are not part of the201    # `environment` and are created during the test.202    if server is not None or client is not None:203        assert server is not None, "server need to be specified, if client is set"204        assert client is not None, "client need to be specified, if server is set"205    else:206        environment = test_result.environment207        assert environment, "fail to get environment from testresult"208        # set server and client from environment, if not set explicitly209        server = cast(RemoteNode, environment.nodes[1])210        client = cast(RemoteNode, environment.nodes[0])211    if not test_case_name:212        # if it's not filled, assume it's called by case directly.213        test_case_name = inspect.stack()[1][3]214    if connections is None:215        if udp_mode:216            connections = NTTTCP_UDP_CONCURRENCY217        else:218            connections = NTTTCP_TCP_CONCURRENCY219    client_ntttcp, server_ntttcp = run_in_parallel(220        [lambda: client.tools[Ntttcp], lambda: server.tools[Ntttcp]]  # type: ignore221    )222    try:223        client_lagscope, server_lagscope = run_in_parallel(224            [225                lambda: client.tools[Lagscope],  # type: ignore226                lambda: server.tools[Lagscope],  # type: ignore227            ]228        )229        for ntttcp in [client_ntttcp, server_ntttcp]:230            ntttcp.setup_system(udp_mode)231        for lagscope in [client_lagscope, server_lagscope]:232            lagscope.set_busy_poll()233        data_path = get_nic_datapath(client)234        if NetworkDataPath.Sriov.value == data_path:235            server_nic_name = (236                server_nic_name if server_nic_name else server.nics.get_lower_nics()[0]237            )238            client_nic_name = (239                client_nic_name if client_nic_name else client.nics.get_lower_nics()[0]240            )241            dev_differentiator = "mlx"242        else:243            server_nic_name = (244                server_nic_name if server_nic_name else server.nics.default_nic245            )246            client_nic_name = (247                client_nic_name if client_nic_name else client.nics.default_nic248            )249            dev_differentiator = "Hypervisor callback interrupts"250        server_lagscope.run_as_server(ip=server.internal_address)251        max_server_threads = 64252        perf_ntttcp_message_list: List[253            Union[NetworkTCPPerformanceMessage, NetworkUDPPerformanceMessage]254        ] = []255        for test_thread in connections:256            if test_thread < max_server_threads:257                num_threads_p = test_thread258                num_threads_n = 1259            else:260                num_threads_p = max_server_threads261                num_threads_n = int(test_thread / num_threads_p)262            if 1 == num_threads_n and 1 == num_threads_p:263                buffer_size = int(1048576 / 1024)264            else:265                buffer_size = int(65536 / 1024)266            if udp_mode:267                buffer_size = int(1024 / 1024)268            server_result = server_ntttcp.run_as_server_async(269                server_nic_name,270                ports_count=num_threads_p,271                buffer_size=buffer_size,272                dev_differentiator=dev_differentiator,273                udp_mode=udp_mode,274            )275            client_lagscope_process = client_lagscope.run_as_client_async(276                server_ip=server.internal_address,277                ping_count=0,278                run_time_seconds=10,279                print_histogram=False,280                print_percentile=False,281                histogram_1st_interval_start_value=0,282                length_of_histogram_intervals=0,283                count_of_histogram_intervals=0,284                dump_csv=False,285            )286            client_ntttcp_result = client_ntttcp.run_as_client(287                client_nic_name,288                server.internal_address,289                buffer_size=buffer_size,290                threads_count=num_threads_n,291                ports_count=num_threads_p,292                dev_differentiator=dev_differentiator,293                udp_mode=udp_mode,294            )295            server_ntttcp_result = server_result.wait_result()296            server_result_temp = server_ntttcp.create_ntttcp_result(297                server_ntttcp_result298            )299            client_result_temp = client_ntttcp.create_ntttcp_result(300                client_ntttcp_result, role="client"301            )302            client_sar_result = client_lagscope_process.wait_result()303            client_average_latency = client_lagscope.get_average(client_sar_result)304            if udp_mode:305                ntttcp_message: Union[306                    NetworkTCPPerformanceMessage, NetworkUDPPerformanceMessage307                ] = client_ntttcp.create_ntttcp_udp_performance_message(308                    server_result_temp,309                    client_result_temp,310                    str(test_thread),311                    buffer_size,312                    test_case_name,313                    test_result,314                )315            else:316                ntttcp_message = client_ntttcp.create_ntttcp_tcp_performance_message(317                    server_result_temp,318                    client_result_temp,319                    client_average_latency,320                    str(test_thread),321                    buffer_size,322                    test_case_name,323                    test_result,324                )325            notifier.notify(ntttcp_message)326            perf_ntttcp_message_list.append(ntttcp_message)327    finally:328        for ntttcp in [client_ntttcp, server_ntttcp]: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:348        for node in [client, server]:349            ssh = node.tools[Ssh]350            ssh.set_max_session()351            node.close()352    for buffer_length in buffer_length_list:353        for connection in connections:354            server_iperf3_process_list: List[Process] = []355            client_iperf3_process_list: List[Process] = []356            client_result_list: List[ExecutableResult] = []357            server_result_list: List[ExecutableResult] = []358            if connection < 64:359                num_threads_p = connection360                num_threads_n = 1361            else:362                num_threads_p = 64363                num_threads_n = int(connection / 64)364            server_start_port = 750365            current_server_port = server_start_port366            current_server_iperf_instances = 0367            while current_server_iperf_instances < num_threads_n:368                current_server_iperf_instances += 1369                server_iperf3_process_list.append(370                    server_iperf3.run_as_server_async(371                        current_server_port, "g", 10, True, True, False372                    )373                )374                current_server_port += 1375            client_start_port = 750376            current_client_port = client_start_port377            current_client_iperf_instances = 0378            while current_client_iperf_instances < num_threads_n:379                current_client_iperf_instances += 1380                client_iperf3_process_list.append(381                    client_iperf3.run_as_client_async(382                        server.internal_address,383                        output_json=True,384                        report_periodic=1,385                        report_unit="g",386                        port=current_client_port,387                        buffer_length=buffer_length,388                        run_time_seconds=10,389                        parallel_number=num_threads_p,390                        ip_version="4",391                        udp_mode=udp_mode,392                    )393                )394                current_client_port += 1395            for client_iperf3_process in client_iperf3_process_list:...lagscope.py
Source:lagscope.py  
...131            expected_exit_code=0,132            expected_exit_code_failure_message=f"fail to launch cmd {self.command}"133            f"{cmd}",134        )135    def run_as_client_async(136        self,137        server_ip: str,138        test_interval: int = 0,139        run_time_seconds: int = 10,140        ping_count: int = 0,141        print_histogram: bool = True,142        print_percentile: bool = True,143        histogram_1st_interval_start_value: int = 30,144        length_of_histogram_intervals: int = 15,145        count_of_histogram_intervals: int = 30,146        dump_csv: bool = True,147        daemon: bool = False,148    ) -> Process:149        # -s: run as a sender150        # -i: test interval151        # -n: ping iteration152        # -H: print histogram of per-iteration latency values153        # -P: prints 50th, 75th, 90th, 95th, 99th, 99.9th, 99.99th, 99.999th percentile154        #  of latencies155        # -a: histogram 1st interval start value156        # -l: length of histogram intervals157        # -c: count of histogram intervals158        # -R: dumps raw latencies into csv file159        # -D: run as daemon160        cmd = f"{self.command} -s{server_ip} "161        if run_time_seconds:162            cmd += f" -t{run_time_seconds} "163        if count_of_histogram_intervals:164            cmd += f" -c{count_of_histogram_intervals} "165        if length_of_histogram_intervals:166            cmd += f" -l{length_of_histogram_intervals} "167        if histogram_1st_interval_start_value:168            cmd += f" -a{histogram_1st_interval_start_value} "169        if ping_count:170            cmd += f" -n{ping_count} "171        if test_interval:172            cmd += f" -i{test_interval} "173        if daemon:174            cmd += " -D "175        if print_histogram:176            cmd += " -H "177        if print_percentile:178            cmd += " -P "179        if dump_csv:180            cmd += f" -RLatency-{get_datetime_path()}.csv "181        process = self.node.execute_async(cmd, shell=True)182        return process183    def run_as_client(184        self,185        server_ip: str,186        test_interval: int = 0,187        run_time_seconds: int = 10,188        ping_count: int = 0,189        print_histogram: bool = True,190        print_percentile: bool = True,191        histogram_1st_interval_start_value: int = 30,192        length_of_histogram_intervals: int = 15,193        count_of_histogram_intervals: int = 30,194        dump_csv: bool = True,195        daemon: bool = False,196    ) -> ExecutableResult:197        process = self.run_as_client_async(198            server_ip,199            test_interval,200            run_time_seconds,201            ping_count,202            print_histogram,203            print_percentile,204            histogram_1st_interval_start_value,205            length_of_histogram_intervals,206            count_of_histogram_intervals,207            dump_csv,208            daemon,209        )210        result = process.wait_result()211        errors = find_groups_in_lines(result.stdout, self._client_failure_pattern)...netperf.py
Source:netperf.py  
...31            sudo=True,32            expected_exit_code=0,33            expected_exit_code_failure_message=f"fail to run {cmd}",34        )35    def run_as_client_async(36        self,37        server_ip: str,38        core_count: int,39        port: int = 30000,40        test_name: str = "TCP_RR",41        seconds: int = 150,42        time_unit: int = 1,43        send_recv_offset: str = "THROUGHPUT, THROUGHPUT_UNITS, MIN_LATENCY, MAX_LATENCY, MEAN_LATENCY, REQUEST_SIZE, RESPONSE_SIZE, STDDEV_LATENCY",  # noqa: E50144    ) -> Process:45        # -H: Specify the target machine and/or local ip and family46        # -p: Specify netserver port number and/or local port47        # -t: Specify test to perform48        # -n: Set the number of processors for CPU util49        # -l: Specify test duration (>0 secs) (<0 bytes|trans)...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!!
