Best Python code snippet using locust
test_web.py
Source:test_web.py
...354 STATS_HISTORY_FILENAME = "{}_stats_history.csv".format(STATS_BASE_NAME)355 STATS_FAILURES_FILENAME = "{}_failures.csv".format(STATS_BASE_NAME)356 def setUp(self):357 super().setUp()358 self.remove_files_if_exists()359 parser = get_parser(default_config_files=[])360 self.environment.parsed_options = parser.parse_args(["--csv", self.STATS_BASE_NAME, "--csv-full-history"])361 self.stats = self.environment.stats362 self.stats.CSV_STATS_INTERVAL_SEC = 0.02363 locust.stats.CSV_STATS_INTERVAL_SEC = 0.1364 self.stats_csv_writer = StatsCSVFileWriter(365 self.environment, stats.PERCENTILES_TO_REPORT, self.STATS_BASE_NAME, full_history=True366 )367 self.web_ui = self.environment.create_web_ui("127.0.0.1", 0, stats_csv_writer=self.stats_csv_writer)368 self.web_ui.app.view_functions["request_stats"].clear_cache()369 gevent.sleep(0.01)370 self.web_port = self.web_ui.server.server_port371 def tearDown(self):372 super().tearDown()373 self.web_ui.stop()374 self.runner.quit()375 self.remove_files_if_exists()376 def remove_file_if_exists(self, filename):377 if os.path.exists(filename):378 os.remove(filename)379 def remove_files_if_exists(self):380 self.remove_file_if_exists(self.STATS_FILENAME)381 self.remove_file_if_exists(self.STATS_HISTORY_FILENAME)382 self.remove_file_if_exists(self.STATS_FAILURES_FILENAME)383 def test_request_stats_full_history_csv(self):384 self.stats.log_request("GET", "/test", 1.39764125, 2)385 self.stats.log_request("GET", "/test", 999.9764125, 1000)386 self.stats.log_request("GET", "/test2", 120, 5612)387 greenlet = gevent.spawn(self.stats_csv_writer.stats_writer)388 gevent.sleep(0.01)389 self.stats_csv_writer.stats_history_flush()390 gevent.kill(greenlet)391 response = requests.get("http://127.0.0.1:%i/stats/requests_full_history/csv" % self.web_port)392 self.assertEqual(200, response.status_code)393 self._check_csv_headers(response.headers, "requests_full_history")...
pyfile.py
Source:pyfile.py
...45 # 妿åå¨ååæä»¶ï¼ä½æ¯ä¸å
许è¦çéåï¼å°±æ¥é46 elif os.path.isfile(path) and not overwrite:47 raise Exception('该ç®å½ä¸å·²ç»åå¨ååçæä»¶ï¼ä¸å¯åå»ºä¸æä»¶ååçæä»¶å¤¹ï¼'48 'overwrite=Trueå¯ä»¥å
å é¤è¯¥æä»¶åå建ï¼' + path)49 def remove_files_if_exists(self, path, file_prefix=None, file_suffix=None):50 """51 å é¤pathä¸çæå®æ ¼å¼æä»¶ã52 :param path: ç®å½æè
æä»¶å53 :param file_prefix: 妿æä»¶å满足è¿ä¸ªåç¼ï¼åå é¤54 :param file_suffix: 妿æä»¶å满足è¿ä¸ªåç¼ï¼åå é¤55 å¦ææ²¡ææå®åç¼ååç¼ï¼è¯´æpathçæä»¶åï¼åªéè¦å é¤è¿ä¸ªæä»¶56 """57 # file_prefix='__init'58 # file_suffix='.py'59 # 夿æ¯å¦åå¨60 if not os.path.exists(path):61 print('æå®çç®å½æè
æä»¶ä¸åå¨ï¼ä¸éè¦æ§è¡å 餿ä½ï¼' + path)62 return63 # å¦ææ¯æä»¶ï¼åå é¤64 if os.path.isfile(path):65 os.remove(path)66 return67 # å¦åå°±æ¯å 餿å®åç¼æè
åç¼çæä»¶68 # å
è·åå
¨é¨æä»¶69 all_files = os.listdir(path)70 files2 = []71 # æ¾å°åç¼æ ¼å¼æä»¶72 if file_prefix:73 files = [file for file in all_files if file.startswith(file_prefix)]74 files2.extend(files)75 # æ¾å°åç¼æ ¼å¼æä»¶76 if file_suffix:77 files = [file for file in all_files if file.endswith(file_suffix)]78 files2.extend(files)79 # å¦ææ²¡ææå®åç¼æè
åç¼ï¼å°±æ¯å é¤å
¨é¨æä»¶80 if not file_prefix and not file_suffix:81 files2 = all_files82 # å¼å§å 餿件83 for file in set(files2):84 file = os.path.join(path, file)85 os.remove(file)86 def remove_dir_and_files(self, path):87 """å é¤ç®å½ä»¥ååç®å½åæä»¶ç"""88 # path=r'D:\a'89 if not os.path.exists(path):90 print('æå®çç®å½ä¸åå¨ï¼ä¸éè¦æ§è¡å 餿ä½ï¼' + path)91 # å é¤92 shutil.rmtree(path)93 def remove_old_files(self, path, file_prefix, keep_days=5, datefmt='%Y-%m-%d.%H'):94 """å 餿§çæ°æ®å¤ä»½æä»¶ï¼è¦ä¸ªè¦æ±æä»¶å½åæ ¼å¼ï¼ file_prefix_20150101 """95 # file_prefix = 'file_prefix_'96 # å¤æè·¯å¾æ¯å¦åå¨97 if not os.path.exists(path):98 raise Exception('æå®çè·¯å¾ä¸åå¨ï¼è¯·æ£æ¥ï¼%s' % path)99 # æ¾åºæå®åç¼çæä»¶100 files = [file for file in os.listdir(path) if file.startswith(file_prefix)]101 # æ¾å°n天åçæä»¶102 latest_day = (datetime.datetime.today() - datetime.timedelta(keep_days)).strftime(datefmt)103 old_file = file_prefix + latest_day104 remove_files = [file for file in files if file < old_file]105 # å¼å§å æä»¶106 for file in remove_files:107 file = os.path.join(path, file)108 os.remove(file)109 print('å 餿§çæä»¶ï¼%s' % file)110 return111 def back_file(self, file, suffix='_bak'):112 """å¤ä»½æä»¶"""113 file_bak = '%s%s' % (file, suffix)114 self.remove_files_if_exists(file_bak)115 shutil.copy(file, file_bak)116 def copy(self, source, target):117 """å¤å¶æä»¶ææä»¶å¤¹å°å¦å¤çå°æ¹"""118 if not os.path.exists(self.get_path_name(target)):119 os.makedirs(self.get_path_name(target))120 print('ç®æ ç®å½ä¸åå¨ï¼å°å建ï¼%s' % self.get_path_name(target))121 # å¤å¶122 shutil.copyfile(source, target)123 def copy_dir(self, source, target):124 """å¤å¶æä»¶å¤¹"""125 if not os.path.exists(self.get_path_name(target)):126 os.makedirs(self.get_path_name(target))127 print('ç®æ ç®å½ä¸åå¨ï¼å°å建ï¼%s' % self.get_path_name(target))128 # å¤å¶...
parallel_func.py
Source:parallel_func.py
...112 pypickle.write(result_file, all_result_data)113 print('ç»æåå°æä»¶ï¼%s'%result_file)114 # -------------------------------------------------------------------------------------------------------115 # å é¤ä¸´æ¶æä»¶116 pyos.remove_files_if_exists(path, file_prefix=split_prefix)117 pyos.remove_files_if_exists(path, file_prefix=result_prefix)118 print('å é¤ä¸´æ¶æ°æ®æä»¶')119 t2 = datetime.datetime.now()120 print('宿å
¨é¨å¹¶è¡è®¡ç®ï¼èæ¶ï¼%d ç§'%(t2-t1).seconds)121 # è¿å122 if result_file:123 return result_file124 else:125 return all_result_data126def _test():...
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!!