smicyk / groovy-jmeter

A Groovy-based DSL for building and running JMeter test plans from command line and more.
Apache License 2.0
13 stars 1 forks source link

100.00% Err when running scripts with parameters #53

Closed AntonioSun closed 2 years ago

AntonioSun commented 2 years ago

https://github.com/smicyk/groovy-jmeter/blob/8e22a6230524704da27dae5aca5ebf686166ddf9/examples/commandline/script.groovy#L7-L12

Based on that, I'm running my script of https://gist.githubusercontent.com/AntonioSun/d3068084f03fb5e5f2a63b9f3e73c5e1/raw/9e3a7e6922047aedf0520e22f74156e26f28519d/commandline.groovy

and this is what I got:

$ docker run --rm -u groovy -v "$(pwd)":"/home/groovy" -v "grapes-cache":"/home/groovy/.groovy/grapes" groovy:3.0.7-jdk11 groovy script.groovy -Vjmt_host=172.24.24.1 -Vjmt_users=3 -Vjmt_ramp=1 -Vjmt_user_nm=john -Vjmt_user_pw=john 
WARNING: An illegal reflective ...
 =     42 in 00:00:01 =   39.7/s Avg:     0 Min:     0 Max:     0 Err:    42 (100.00%)

This is not the first time that I'm seeing 100.00% Err when running groovy-jmeter scripts. In fact all of them are, as of now.

I posted the execution log here. This is actually my 3rd run, previously every line has problems, now at least the top few lines are OK.

The converted jmeter script runs fine in jmeter. I posted the jmeter script here -- https://gist.githubusercontent.com/AntonioSun/d3068084f03fb5e5f2a63b9f3e73c5e1/raw/77f4d18a970c6d8c2bdc57177649cb1bd8290094/commandline.jmx

image

smicyk commented 2 years ago

I assume you use mockserver from examples/mockserver.

So if you want to run your script with docker command you need to add the container to the same network as mockserver. Your script should be based on examples/docker as it is more what you want to achieve. The crucial part is network parameter --network network-books in your docker command (check run.sh in docker example).

Also host for mockserver in your script should be mockserver as it is named in docker compose file. The IP approach might work if you change network type to host for mockserver and docker in your command. In the examples I use the separate network so I can use names instead of IP which is more convenient.

Next thing, you should verify if your all command variables are properly put in the script. The groovy string interpolation only works if ${} is put in double quotes. So this:

argument(name: 'var_host', value: '${jmt_host}')

should be this

argument(name: 'var_host', value: "${jmt_host}")

Hope that helps

AntonioSun commented 2 years ago

Thanks a lot for your detailed explanation!

Just want to confirm:

https://github.com/smicyk/groovy-jmeter/blob/8e22a6230524704da27dae5aca5ebf686166ddf9/examples/commandline/script.groovy#L14

Is this domain: '${var_host}' a typo or somehow it works even if the ${} is not put in double quotes?

smicyk commented 2 years ago

Ah ok, so you should look at https://github.com/smicyk/groovy-jmeter#groovy-as-dsl.

So basically you have two types of variables substitutions, the one for groovy variables and one for jmeter variables. Since jmeter and has same syntax to substitute it can be confusing. In general the rule is if you want groovy substitution you should use double quotes, in any other case you should use single quotes.

In your case when you have:

argument(name: 'var_host', value: "${jmt_host}")

you define var_host variable based on groovy variable jmt_host and this substitution is only available during test plan build phase.

then

defaults(protocol: 'http', domain: '${var_host}', port: 1080) 

you use __var_host__ variable define before during test plan execution.

Note, that groovy substitution you don't have use string interplation to get variable and just use the variable name. It depends what you want to actually create as a variable.

AntonioSun commented 2 years ago

Thanks a lot for your detailed explanation!

In such case, would the following jmt_user_xx better be using single quotes, if I don't want to hard-code them?

https://github.com/smicyk/groovy-jmeter/blob/73e7ee4ee1709eee26a325c8de3815613d8e58fe/examples/commandline/script.groovy#L7-L12

smicyk commented 2 years ago

I'm not sure I understand, the example use jmt_user_nm and jmt_user_pw as variables from command line. In that case they must be in double quotes otherwise they will not be substituted by groovy.

AntonioSun commented 2 years ago

You explained well. thx.

I was just confused where is the DSL part and where is the groovy string part.
Another example,

https://github.com/smicyk/groovy-jmeter/blob/73e7ee4ee1709eee26a325c8de3815613d8e58fe/examples/commandline/script.groovy#L16

why the jmt_users in this case not in double quotes, and not in ${} either.

(I should have closed it awhile back)

smicyk commented 2 years ago

Yes, so jmt_users without quotes means you use the value alone, but with quotes you create a string. Example assume jmt_users=5, then:

# user will have just value 5 as integer
group(users: jmt_users)

but

# the var_user_description variable will have value 'I have 5 users to run' as a string
argument(name: 'var_user_description', value: "I have ${jmt_user_nm} users to run")