0

I've faced a problem using Taurus. Could someone help me, please? I'm trying to simulate 300 users but before sending those 300 users POST requests, I need to generate a token. The token is attached to the request in that way:

- url:  http://url?user_token=${auth_token}

Now I have the following scenario:

load_api:
    requests:
      - once:
          - url:  https://endpoint/authenticateUser
            method: POST
            headers:
              Content-Type: application/json
            body:
              username: username
              password: pass
              generateToken: true
            extract-jsonpath:
              auth_token:
                jsonpath: $.token
            label: get_token
          - url:  http://url/user_token=${auth_token}
            method: POST
            headers:
              Content-Type: application/json
            body-file: test_data/body.json
            label: sending_300

As you can see, a token is generated for each thread. And I need that it is generated before the script and then the token is attached to the URL as a parameter. I've tried to separate those for two scenarios but in that way variables from one script can't be used in the other. I was also looking at global variables but it seems like that kind of variable can be created only before executing. So, if someone could help me, I'll appreciate your time spent.

EDIT (thank you so much Dmitri T):

Here is a workable script:

execution:
  concurrency: 300
  scenario: load_test

    scenarios:
      load_test:
        requests:
        - if: ${__groovy(ctx.getThreadNum() == 0 && vars.getIteration() == 1,)}
          then:
          - url:  https://url/authenticateUser
            method: POST
            headers:
              Content-Type: application/json
            body:
              username: username
              password: pass
              generateToken: true
            extract-jsonpath:
              auth_token:
                jsonpath: $.token
            label: get_token
            jsr223: props.put('auth_token', vars.get('auth_token'))
          else:
          - url:  http://endpoint?user_token=${__P(auth_token,)}
            method: POST
            headers:
              Content-Type: application/json
            body-file: test_data/body.json
            label: sending_300_reqs
            think-time: 10s   # waiter for processing auth request

1 Answers1

0

If you want to generate a token once and share it across 300 threads:

  1. Generate token for 1st thread during the first iteration using If block and convert it to JMeter Property in JSR223 block. The condition for the If block would be:

    ${__groovy(ctx.getThreadNum() == 0 && vars.getIteration() == 1,)}
    

    and the code for JSR223 block:

    props.put('auth_token', vars.get('auth_token'))
    

    check out Top 8 JMeter Java Classes You Should Be Using with Groovy article to learn what these ctx, props and vars words mean

  2. In your 2nd request refer the property using __P() function

    http://url/user_token=${__P(auth_token,)}
    
Dmitri T
  • 159,985
  • 5
  • 83
  • 133