I am total newbie to Python. I am writing my first Python class this week to run Locust.io load test on our API. I have setup the code below.
import random
from locust import HttpUser, SequentialTaskSet, task, between
from datetime import datetime
class CredentialLoadTest(SequentialTaskSet):
@task
def post_credential(self):
print("==========================")
print("========== POST ==========")
print("==========================")
print("==========POST END===========")
@task
def get_credential(self):
print("==========================")
print("========== GET ===========")
print("==========================")
print("==========GET END===========")
@task
def put_credential(self):
print("==========================")
print("========== PUT ===========")
print("==========================")
print("==========PUT END===========")
class AwesomeUser(HttpUser):
tasks = [CredentialLoadTest]
host = "https://url.com"
# wait time between tasks, 5 and 9 seconds
wait_time = between(5, 9)
I have removed all of the actual testing code for my API endpoints here. I just simply wanted to validate if the tasks are running sequentially. When I run this code I get the output shown below.
[2020-07-29 21:14:54,067] 8a7f07ddbe29/INFO/locust.main: Starting web
interface at http://:8089 [2020-07-29 21:14:54,077]
8a7f07ddbe29/INFO/locust.main: Starting Locust 1.1 [2020-07-29
21:15:01,123] 8a7f07ddbe29/INFO/locust.runners: Hatching and swarming
2 users at the rate 2 users/s (0 users already running)...
==========================
========== GET ===========
==========================
==========GET END=========== [2020-07-29 21:15:01,624] 8a7f07ddbe29/INFO/locust.runners: All users hatched: AwesomeUser: 2 (0
already running)
==========================
========== GET ===========
==========================
==========GET END===========
==========================
========== POST ==========
==========================
==========POST END===========
==========================
========== POST ==========
==========================
==========POST END===========
==========================
========== PUT ===========
==========================
==========PUT END===========
==========================
========== PUT ===========
==========================
==========PUT END===========
==========================
========== GET ===========
==========================
==========GET END===========
==========================
========== GET ===========
==========================
==========GET END===========
As you can see the order of tasks running is GET, POST PUT. The order I want is POST, GET and PUT.
So I still could not get the sequential task set to work properly. So I kind of "hacked" a solution that is working for me. I basically created one task as my starting point and then it calls my other "tasks" which are just normal Python functions. This is working perfectly for me, its running each of my API operations in the order I originally wanted.
# GET newly created credential
def get_credential(self):
print_log("<========== GET ===========>")
print_log("<========== GET END ========>", True)
# PUT newly created credential
def put_credential(self):
print_log("<========== PUT ===========>")
print_log("<======== PUT END ========>", True)
# DELETE newly created credential
def delete_credential(self):
print_log("<========= DELETE =========>")
print_log("<======= DELETE END =======>", True)
class CredentialLoadTestPost(TaskSet):
print_log("*** LOCUST API LOAD TEST Started ***", True)
# Create new credentail
@task
def post_credential(self):
print_log("<========== POST ==========>")
print_log("<========= POST END========>", True)
get_credential(self)
put_credential(self)
delete_credential(self)
class AwesomeUser(HttpUser):
tasks = {CredentialLoadTestPost}
# wait time between tasks, 5 and 9 seconds assume someone who is browsing the Locust docs,
wait_time = between(5, 9)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With