Best Python code snippet using locust
frontend.py
Source:frontend.py
...52 max_wait = 150053"""54*on_quitting* is fired when the locust process is exiting55"""56def on_quitting(**kw):57 """ Upload data to GCS bucket on quit """58 storage_client = storage.Client()59 bucket = storage_client.get_bucket("artifacts.{}.appspot.com".format(os.environ['GCP_PROJECT']))60 rdir = os.path.dirname(os.path.realpath(__file__))61 rfiles = glob.glob(rdir + '/*.csv')62 for rfile in rfiles:63 time_postfix = str(datetime.now().replace(microsecond=0))64 blobname = Path(rfile).stem + '_' + time_postfix + '.csv'65 blob = bucket.blob('stress/' + blobname)66 blob.upload_from_filename(rfile)67 print("Test result is available via {}".format(blob.public_url))68 return69"""70Register locust.events.quitting hook if executing under --no-web mode in a kubernetes container...
hooks_sample.py
Source:hooks_sample.py
...9def on_locust_error(locust_instance, exception, tb):10 print "%r, %s, %s" % (locust_instance, exception, "".join(traceback.format_tb(tb)))11def on_hatch_complete(user_count):12 print "Haha, Locust have generate %d users" % user_count13def on_quitting():14 print "Locust is quiting"15events.request_success += on_request_success16events.request_failure += on_request_failure17events.locust_error += on_locust_error18events.hatch_complete += on_hatch_complete19events.quitting += on_quitting20class UserTask(TaskSet):21 @task(5)22 def job1(self):23 with self.client.get('/', catch_response = True) as r:24 if random.choice([0, 1]):25 r.success()26 else:27 r.failure('0')...
locustfile.py
Source:locustfile.py
...10 max_wait = 1500011 host = "http://www.noppanit.com"12def on_request_success(request_type, name, response_time, response_length):13 print 'on success'14def on_quitting():15 aggregated_stats = global_stats.aggregated_stats("Total").get_stripped_report()16 f = open('stats.json','w')17 f.write(str(aggregated_stats))18 f.close()...
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!!