Best Python code snippet using locust
test_runners.py
Source:test_runners.py
...101 stats.log_request("GET", "/1", 800, 56743)102 stats2 = RequestStats()103 stats2.log_request("GET", "/2", 700, 2201)104 server.mocked_send(Message("stats", {105 "stats":stats.serialize_stats(), 106 "stats_total": stats.total.serialize(),107 "errors":stats.serialize_errors(),108 "user_count": 1,109 }, "fake_client"))110 server.mocked_send(Message("stats", {111 "stats":stats2.serialize_stats(), 112 "stats_total": stats2.total.serialize(),113 "errors":stats2.serialize_errors(),114 "user_count": 2,115 }, "fake_client"))116 self.assertEqual(700, master.stats.total.median_response_time)117 118 def test_master_current_response_times(self):119 import mock120 121 class MyTestLocust(Locust):122 pass123 124 start_time = 1125 with mock.patch("time.time") as mocked_time:126 mocked_time.return_value = start_time127 global_stats.reset_all()128 with mock.patch("locust.rpc.rpc.Server", mocked_rpc_server()) as server:129 master = MasterLocustRunner(MyTestLocust, self.options)130 mocked_time.return_value += 1131 server.mocked_send(Message("client_ready", None, "fake_client"))132 stats = RequestStats()133 stats.log_request("GET", "/1", 100, 3546)134 stats.log_request("GET", "/1", 800, 56743)135 server.mocked_send(Message("stats", {136 "stats":stats.serialize_stats(),137 "stats_total": stats.total.get_stripped_report(),138 "errors":stats.serialize_errors(),139 "user_count": 1,140 }, "fake_client"))141 mocked_time.return_value += 1142 stats2 = RequestStats()143 stats2.log_request("GET", "/2", 400, 2201)144 server.mocked_send(Message("stats", {145 "stats":stats2.serialize_stats(),146 "stats_total": stats2.total.get_stripped_report(),147 "errors":stats2.serialize_errors(),148 "user_count": 2,149 }, "fake_client"))150 mocked_time.return_value += 4151 self.assertEqual(400, master.stats.total.get_current_response_time_percentile(0.5))152 self.assertEqual(800, master.stats.total.get_current_response_time_percentile(0.95))153 154 # let 10 second pass, do some more requests, send it to the master and make155 # sure the current response time percentiles only accounts for these new requests156 mocked_time.return_value += 10157 stats.log_request("GET", "/1", 20, 1)158 stats.log_request("GET", "/1", 30, 1)159 stats.log_request("GET", "/1", 3000, 1)160 server.mocked_send(Message("stats", {161 "stats":stats.serialize_stats(),162 "stats_total": stats.total.get_stripped_report(),163 "errors":stats.serialize_errors(),164 "user_count": 2,165 }, "fake_client"))166 self.assertEqual(30, master.stats.total.get_current_response_time_percentile(0.5))167 self.assertEqual(3000, master.stats.total.get_current_response_time_percentile(0.95))168 169 def test_spawn_zero_locusts(self):170 class MyTaskSet(TaskSet):171 @task172 def my_task(self):173 pass174 175 class MyTestLocust(Locust):...
server.py
Source:server.py
...58 query['meta.host_id'] = request.args['host_id']59 query['meta.date'] = {}60 query['meta.date']['$gte'] = week61 stats = db_connection.get_documents(collection, query)62 serialized_stats = serialize_stats(stats, False)63 print "Info: Reponse from /get_stats_by_host OK"64 return json.dumps(serialized_stats)65@cross_origin()66@app.route('/get_general_stats', methods=['GET'])67def get_general_stats():68 if request.method == 'GET':69 collection = db_connection.get_collection(db=db, collection_name="hosts_statistics")70 week = datetime.datetime.utcnow() - datetime.timedelta(days=25)71 query = dict()72 query['meta.date'] = {}73 query['meta.date']['$gte'] = week74 stats = db_connection.get_documents(collection, query)75 serialized_stats = serialize_stats(stats, True)76 print "Info: Reponse from /get_general_stats OK",77 return json.dumps(serialized_stats)78def serialize_stats(stats, general_flag):79 host_list = ['oscompute-1', 'oscompute-2']80 if general_flag is True:81 day_stats = list()82 days = list()83 for s in stats:84 days.append(datetime.datetime.strptime(s['meta']['date'][:10], "%Y-%m-%d"))85 days = list(set(days))86 days.sort()87 response = []88 for h in host_list:89 for day in days:90 day_statsx = dict()91 day_statsx['meta'] = dict()92 day_statsx['meta']['date'] = str(day)...
serializers.py
Source:serializers.py
...11 id=player.id,12 name=player.name,13 gender=player.gender.value,14 online=player.online,15 stats=serialize_stats(player.stats),16 )17def serialize_stats(stats: PlayerStats) -> dict:18 return dict(19 level=stats.level,20 hp=stats.hp,21 mp=stats.mp,22 strength=stats.strength,23 dexterity=stats.dexterity,24 stamina=stats.stamina,...
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!!