Best Python code snippet using locust
cv19-webapp.py
Source: cv19-webapp.py
...26 result = {27 'fails': environment.stats.total.fail_ratio * 100,28 'resp.time': {29 'avg': int(environment.stats.total.avg_response_time),30 '99.9%': int(environment.stats.total.get_response_time_percentile(0.999)),31 '99%': int(environment.stats.total.get_response_time_percentile(0.99)),32 '95%': int(environment.stats.total.get_response_time_percentile(0.95)),33 '90%': int(environment.stats.total.get_response_time_percentile(0.85))34 }35 }36 print(f'Test complete statistics: {result}')37 if environment.stats.total.fail_ratio * 100 > config.threshold_fails:38 print(f'â Test failed due to failure ratio > {config.threshold_fails}%')39 environment.process_exit_code = 140 # elif environment.stats.total.avg_response_time > config.threshold_avg:41 # print(f'â Test failed due to average response time ratio > {config.threshold_avg} ms')42 # environment.process_exit_code = 143 elif environment.stats.total.get_response_time_percentile(0.95) > config.threshold_95:44 print(f'â Test failed due to 95th percentile response time > {config.threshold_95} ms')45 environment.process_exit_code = 146 else:47 print(f'â
Test passed')48 environment.process_exit_code = 049class WebsiteUser(HttpUser):50 wait_time = between(5, 15)51 52 def _validate_response(self, response, status_code, text=None):53 content = response.content or ''54 self.debug(f'Response [{response.status_code}]: {content[0:100]}')55 if response.status_code == status_code:56 if text or text in content:57 return True...
loadtest.py
Source: loadtest.py
...41 'avg_response_time': value.avg_response_time,42 'max_response_time': value.max_response_time,43 'response_times': value.response_times,44 'response_time_percentiles': {45 55: value.get_response_time_percentile(0.55),46 65: value.get_response_time_percentile(0.65),47 75: value.get_response_time_percentile(0.75),48 85: value.get_response_time_percentile(0.85),49 95: value.get_response_time_percentile(0.95)50 },51 'total_rps': value.total_rps,52 'total_rpm': value.total_rps * 6053 }54 for id, error in runners.locust_runner.errors.items():55 error_dict = error.to_dict()56 locust_task_name = '{0}_{1}'.format(error_dict['method'], error_dict['name'])57 statistics['failures'][locust_task_name] = error_dict58 return statistics59 def sig_term_handler(self, signum, frame):60 logger.info("Received sigterm, exiting")61 logger.info(json.dumps(self.stats()))62 sys.exit(0)63 def sig_alarm_handler(self, signum, frame):...
__init__.py
Source: __init__.py
...14def do_checks(environment, **kwargs):15 if isinstance(environment.runner, WorkerRunner):16 return17 stats = environment.runner.stats.total18 percentile_90 = stats.get_response_time_percentile(0.90)19 percentile_95 = stats.get_response_time_percentile(0.95)20 percentile_99 = stats.get_response_time_percentile(0.99)21 RPS = stats.total_rps22 fail_ratio = stats.fail_ratio23 kpis_config = readKPIsConfig(os.environ.get("LOCUST_LOCUSTFILE"))24 check_rps = kpis_config.get('requests_per_second') 25 check_fail_ratio = kpis_config.get('fail_ratio_allowed') 26 check_response_time_90_percentile = kpis_config.get('response_time_percentile_90_milliseconds')27 check_response_time_95_percentile = kpis_config.get('response_time_percentile_95_milliseconds')28 check_response_time_99_percentile = kpis_config.get('response_time_percentile_99_milliseconds')29 validate_metric_goal(environment, percentile_90, check_response_time_90_percentile, "90% percentile (ms)")30 validate_metric_goal(environment, percentile_95, check_response_time_95_percentile, "95% percentile (ms)")31 validate_metric_goal(environment, percentile_99, check_response_time_99_percentile, "99% percentile (ms)")32 validate_rps(environment, RPS, check_rps, "Requests per second")33 validate_metric_goal(environment, fail_ratio, check_fail_ratio, "Fail ratio")34 KPIsresult = validateTestRunResult(environment.process_exit_code)
Check out the latest blogs from LambdaTest on this topic:
Joseph, who has been working as a Quality Engineer, was assigned to perform web automation for the company’s website.
Lack of training is something that creates a major roadblock for a tester. Often, testers working in an organization are all of a sudden forced to learn a new framework or an automation tool whenever a new project demands it. You may be overwhelmed on how to learn test automation, where to start from and how to master test automation for web applications, and mobile applications on a new technology so soon.
There are times when developers get stuck with a problem that has to do with version changes. Trying to run the code or test without upgrading the package can result in unexpected errors.
Websites and web apps are growing in number day by day, and so are the expectations of people for a pleasant web experience. Even though the World Wide Web (WWW) was invented only in 1989 (32 years back), this technology has revolutionized the world we know back then. The best part is that it has made life easier for us. You no longer have to stand in long queues to pay your bills. You can get that done within a few minutes by visiting their website, web app, or mobile app.
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!!