0

I'm not able to have 2 or more @seq_task's to run in order, after getting and using the access token with the POST login.

  1. on_start get the access token
  2. GET using the token to an API endpoint
  3. POST using the same token to the same API endpoint
  4. stop the locusts when Number of total users to simulate = value set at start (this works)

It works perfectly using either 2 or 3 with the access token. It doesn't work with both, or by adding @task(1) before def projects(self) and def new_project(self). Using both the failure is on 3, 401 Client unauthorized ...

What am I missing?

from locust import HttpLocust, TaskSet, task, between, TaskSequence, seq_task
from locust.exception import StopLocust

class UserBehaviour(TaskSequence):
    def on_start(self):
        """ on_start is called when a Locust start before any task is scheduled """
        """self.client.verify = False"""
        self.login()

    def on_stop(self):
        """ on_stop is called when the TaskSet is stopping """
        self.logout()

    def login(self):
        self.response = self.client.post("/api/security/auth/token/obtain/", 
            {"username":"loadtester", "password":"Password1"})
        js = self.response.json()
        self.rspAcs = js["access"]
        print(self.rspAcs, ' ')

    def logout(self):
        self.client.post("/api/security/auth/token/obtain/", 
            {"username":"loadtester", "password":"Password1"})

    @seq_task(1)
    class some_process(TaskSequence):
        @seq_task(1)
        def projects(self):
            self.client.get("/api/analysis/project/project/", headers=
                {"Authorization": "Bearer " + self.parent.rspAcs})
            print(self.parent.rspAcs, ' ')

        @seq_task(2)
        def new_project(self):
            self.client.post("/api/analysis/project/project/", headers=
                {"Authorization": "Bearer " + self.parent.rspAcs}, json={"project_name":"new_project"})
            print(self.parent.rspAcs, ' ')

        @seq_task(3)
        def stop(self):
            self.interrupt()

    @seq_task(2)
    def done(self):
        raise StopLocust()

class WebsiteUser(HttpLocust):
    task_set = UserBehaviour
    wait_time = between(5, 9)

Logs for total number of users to simulate = 3, hatch rate = 1. Notice the 2 failed POSTs near the end.

standalone    | Starting Locust in standalone mode...
standalone    | $ locust --web-port 8089 --loglevel DEBUG -f /mnt/locust/locustfile.py -H http://10.22.162.62
standalone    | [2020-04-10 00:08:21,004] standalone/INFO/locust.main: Starting web monitor at http://*:8089
standalone    | [2020-04-10 00:08:21,004] standalone/INFO/locust.main: Starting Locust 0.14.5
standalone    | [2020-04-10 00:08:30,134] standalone/INFO/locust.runners: Hatching and swarming 3 users at the rate 1 users/s (0 users already running)...
standalone    | [2020-04-10 00:08:30,136] standalone/DEBUG/urllib3.connectionpool: Starting new HTTP connection (1): 10.22.162.62:80
standalone    | [2020-04-10 00:08:30,257] standalone/DEBUG/urllib3.connectionpool: http://10.22.162.62:80 "POST /api/security/auth/token/obtain/ HTTP/1.1" 200 569
standalone    | [2020-04-10 00:08:30,258] standalone/INFO/stdout: eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ0b2tlbl90eXBlIjoiYWNjZXNzIiwiZXhwIjoxNTg2NDgwOTEwLCJqdGkiOiI2NzUyODJhMDk1YzE0YWMzOGI3MDdmZTRlOTZkZmYxYiIsInVzZXJfaWQiOjMsIm9yaWdpbmFsX2lhdCI6MTU4NjQ3NzMxMCwicmVmcmVzaF9pbnRlcnZhbCI6MTIwfQ.r8RZgaDl5wDc3Fe_KEFMsq9b1nhI85z30u8L5b_DkHY
standalone    | [2020-04-10 00:08:30,258] standalone/INFO/stdout: 
standalone    | [2020-04-10 00:08:30,258] standalone/INFO/stdout: 
standalone    | [2020-04-10 00:08:30,258] standalone/INFO/stdout: 
standalone    | [2020-04-10 00:08:30,291] standalone/DEBUG/urllib3.connectionpool: http://10.22.162.62:80 "GET /api/analysis/project/project/ HTTP/1.1" 200 614
standalone    | [2020-04-10 00:08:30,292] standalone/INFO/stdout: eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ0b2tlbl90eXBlIjoiYWNjZXNzIiwiZXhwIjoxNTg2NDgwOTEwLCJqdGkiOiI2NzUyODJhMDk1YzE0YWMzOGI3MDdmZTRlOTZkZmYxYiIsInVzZXJfaWQiOjMsIm9yaWdpbmFsX2lhdCI6MTU4NjQ3NzMxMCwicmVmcmVzaF9pbnRlcnZhbCI6MTIwfQ.r8RZgaDl5wDc3Fe_KEFMsq9b1nhI85z30u8L5b_DkHY
standalone    | [2020-04-10 00:08:30,292] standalone/INFO/stdout: 
standalone    | [2020-04-10 00:08:30,292] standalone/INFO/stdout: 
standalone    | [2020-04-10 00:08:30,292] standalone/INFO/stdout: 
standalone    | [2020-04-10 00:08:31,137] standalone/DEBUG/urllib3.connectionpool: Starting new HTTP connection (1): 10.22.162.62:80
standalone    | [2020-04-10 00:08:31,255] standalone/DEBUG/urllib3.connectionpool: http://10.22.162.62:80 "POST /api/security/auth/token/obtain/ HTTP/1.1" 200 569
standalone    | [2020-04-10 00:08:31,256] standalone/INFO/stdout: eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ0b2tlbl90eXBlIjoiYWNjZXNzIiwiZXhwIjoxNTg2NDgwOTExLCJqdGkiOiJlNTU1YzRhY2MwNzQ0ZTYyYjA5ODRmMzNmMWM4NGZiMyIsInVzZXJfaWQiOjMsIm9yaWdpbmFsX2lhdCI6MTU4NjQ3NzMxMSwicmVmcmVzaF9pbnRlcnZhbCI6MTIwfQ.ybVAOGWznEpctHzZmsoO2yXBySY_cZkDjK-vQp3yRis
standalone    | [2020-04-10 00:08:31,256] standalone/INFO/stdout: 
standalone    | [2020-04-10 00:08:31,256] standalone/INFO/stdout: 
standalone    | [2020-04-10 00:08:31,257] standalone/INFO/stdout: 
standalone    | [2020-04-10 00:08:31,291] standalone/DEBUG/urllib3.connectionpool: http://10.22.162.62:80 "GET /api/analysis/project/project/ HTTP/1.1" 200 614
standalone    | [2020-04-10 00:08:31,292] standalone/INFO/stdout: eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ0b2tlbl90eXBlIjoiYWNjZXNzIiwiZXhwIjoxNTg2NDgwOTExLCJqdGkiOiJlNTU1YzRhY2MwNzQ0ZTYyYjA5ODRmMzNmMWM4NGZiMyIsInVzZXJfaWQiOjMsIm9yaWdpbmFsX2lhdCI6MTU4NjQ3NzMxMSwicmVmcmVzaF9pbnRlcnZhbCI6MTIwfQ.ybVAOGWznEpctHzZmsoO2yXBySY_cZkDjK-vQp3yRis
standalone    | [2020-04-10 00:08:31,292] standalone/INFO/stdout: 
standalone    | [2020-04-10 00:08:31,292] standalone/INFO/stdout: 
standalone    | [2020-04-10 00:08:31,292] standalone/INFO/stdout: 
standalone    | [2020-04-10 00:08:32,136] standalone/INFO/locust.runners: All locusts hatched: WebsiteUser: 3 (0 already running)
standalone    | [2020-04-10 00:08:32,139] standalone/DEBUG/urllib3.connectionpool: Starting new HTTP connection (1): 10.22.162.62:80
standalone    | [2020-04-10 00:08:32,258] standalone/DEBUG/urllib3.connectionpool: http://10.22.162.62:80 "POST /api/security/auth/token/obtain/ HTTP/1.1" 200 569
standalone    | [2020-04-10 00:08:32,259] standalone/INFO/stdout: eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ0b2tlbl90eXBlIjoiYWNjZXNzIiwiZXhwIjoxNTg2NDgwOTEyLCJqdGkiOiIxYjY2MzI2MTYyMzQ0YTAyYmNiM2MyZGIwMTFkZTAxOSIsInVzZXJfaWQiOjMsIm9yaWdpbmFsX2lhdCI6MTU4NjQ3NzMxMiwicmVmcmVzaF9pbnRlcnZhbCI6MTIwfQ.DSIK-2TnOJbb0191fVdTb9sKjathJFLGgSf7vko-xi0
standalone    | [2020-04-10 00:08:32,259] standalone/INFO/stdout: 
standalone    | [2020-04-10 00:08:32,259] standalone/INFO/stdout: 
standalone    | [2020-04-10 00:08:32,259] standalone/INFO/stdout: 
standalone    | [2020-04-10 00:08:32,293] standalone/DEBUG/urllib3.connectionpool: http://10.22.162.62:80 "GET /api/analysis/project/project/ HTTP/1.1" 200 614
standalone    | [2020-04-10 00:08:32,293] standalone/INFO/stdout: eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ0b2tlbl90eXBlIjoiYWNjZXNzIiwiZXhwIjoxNTg2NDgwOTEyLCJqdGkiOiIxYjY2MzI2MTYyMzQ0YTAyYmNiM2MyZGIwMTFkZTAxOSIsInVzZXJfaWQiOjMsIm9yaWdpbmFsX2lhdCI6MTU4NjQ3NzMxMiwicmVmcmVzaF9pbnRlcnZhbCI6MTIwfQ.DSIK-2TnOJbb0191fVdTb9sKjathJFLGgSf7vko-xi0
standalone    | [2020-04-10 00:08:32,293] standalone/INFO/stdout: 
standalone    | [2020-04-10 00:08:32,294] standalone/INFO/stdout: 
standalone    | [2020-04-10 00:08:32,294] standalone/INFO/stdout: 
standalone    | [2020-04-10 00:08:38,272] standalone/DEBUG/urllib3.connectionpool: http://10.22.162.62:80 "POST /api/analysis/project/project/ HTTP/1.1" 401 65
standalone    | [2020-04-10 00:08:38,273] standalone/INFO/stdout: eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ0b2tlbl90eXBlIjoiYWNjZXNzIiwiZXhwIjoxNTg2NDgwOTExLCJqdGkiOiJlNTU1YzRhY2MwNzQ0ZTYyYjA5ODRmMzNmMWM4NGZiMyIsInVzZXJfaWQiOjMsIm9yaWdpbmFsX2lhdCI6MTU4NjQ3NzMxMSwicmVmcmVzaF9pbnRlcnZhbCI6MTIwfQ.ybVAOGWznEpctHzZmsoO2yXBySY_cZkDjK-vQp3yRis
standalone    | [2020-04-10 00:08:38,273] standalone/INFO/stdout: 
standalone    | [2020-04-10 00:08:38,273] standalone/INFO/stdout: 
standalone    | [2020-04-10 00:08:38,273] standalone/INFO/stdout: 
standalone    | [2020-04-10 00:08:38,983] standalone/DEBUG/urllib3.connectionpool: http://10.22.162.62:80 "POST /api/analysis/project/project/ HTTP/1.1" 401 65
standalone    | [2020-04-10 00:08:38,984] standalone/INFO/stdout: eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ0b2tlbl90eXBlIjoiYWNjZXNzIiwiZXhwIjoxNTg2NDgwOTEwLCJqdGkiOiI2NzUyODJhMDk1YzE0YWMzOGI3MDdmZTRlOTZkZmYxYiIsInVzZXJfaWQiOjMsIm9yaWdpbmFsX2lhdCI6MTU4NjQ3NzMxMCwicmVmcmVzaF9pbnRlcnZhbCI6MTIwfQ.r8RZgaDl5wDc3Fe_KEFMsq9b1nhI85z30u8L5b_DkHY
standalone    | [2020-04-10 00:08:38,984] standalone/INFO/stdout: 
standalone    | [2020-04-10 00:08:38,984] standalone/INFO/stdout: 
standalone    | [2020-04-10 00:08:38,984] standalone/INFO/stdout: 
standalone    | [2020-04-10 00:08:40,566] standalone/DEBUG/urllib3.connectionpool: http://10.22.162.62:80 "POST /api/analysis/project/project/ HTTP/1.1" 201 62
standalone    | [2020-04-10 00:08:40,567] standalone/INFO/stdout: eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ0b2tlbl90eXBlIjoiYWNjZXNzIiwiZXhwIjoxNTg2NDgwOTEyLCJqdGkiOiIxYjY2MzI2MTYyMzQ0YTAyYmNiM2MyZGIwMTFkZTAxOSIsInVzZXJfaWQiOjMsIm9yaWdpbmFsX2lhdCI6MTU4NjQ3NzMxMiwicmVmcmVzaF9pbnRlcnZhbCI6MTIwfQ.DSIK-2TnOJbb0191fVdTb9sKjathJFLGgSf7vko-xi0
standalone    | [2020-04-10 00:08:40,567] standalone/INFO/stdout: 
standalone    | [2020-04-10 00:08:40,567] standalone/INFO/stdout: 
standalone    | [2020-04-10 00:08:40,567] standalone/INFO/stdout: 
standalone    | [2020-04-10 00:08:41,982] standalone/DEBUG/urllib3.connectionpool: http://10.22.162.62:80 "POST /api/security/auth/token/obtain/ HTTP/1.1" 200 569
standalone    | [2020-04-10 00:08:42,101] standalone/DEBUG/urllib3.connectionpool: http://10.22.162.62:80 "POST /api/security/auth/token/obtain/ HTTP/1.1" 200 569
standalone    | [2020-04-10 00:08:42,222] standalone/DEBUG/urllib3.connectionpool: http://10.22.162.62:80 "POST /api/security/auth/token/obtain/ HTTP/1.1" 200 569
hombre
  • 11
  • 2

1 Answers1

1

Discovered in one of the Locust examples on Github, where they have added SequentialTaskSet. Currently in the dockerhub :master image. Documentation hasn't caught up yet but the example might be enough.

hombre
  • 11
  • 2