JMeter: more than nine parameters from Jenkins - jenkins

I am trying to pass more than nine parameters from Jenkins to JMeter4.0.
As I was reading, I found out that JMeter does not accept more than 9 parameters. As a workaround, I want to pass all the parameters as a string and split it in JMeter BeanShell.
java -jar -Xms512m -Xmx2048m C:\JMeter4\bin\ApacheJMeter.jar -Jjmeter.save.saveservice.output_format=csv -Jjenkinsparams="%Timetorun%,%Users%" -n -t %JMeterPath%\bin\tests\tests.jmx -l %WORKSPACE%\Results.csv
The tests run on a Windows machine. From this call I have
jenkinsparams = "300,2"
I use a BeanShell PreProcessor like this:
String line = "${__P(jenkinsparams)}";
String[] words = line.split(",");
vars.put("timetorun",words[0]);
vars.put("users",words[1]);
log.info(words[1]);
log.info(users);
I tried few log.info to check the values. For words[1] I have the correct value sent from Jenkins: 2. For the users the value displayed is: void.
I am trying to use it for Number of Threads as: ${__P(users,1)}.
What am I doing wrong? The values clearly arrive from Jenkins but I have a problem passing it to my variable. Thank you

You don't have a script variable named users, so you should either log words[0]:
log.info(words[0]);
Or you can log the value of JMeter variable called users:
log.info(vars.get("users"));
Or you can assign words[0] to variable called users:
String users = words[0];
log.info(users);
Also you are saving it as variable, not property, so you can retrieve it elsewhere in script as
${users}
The syntax __P refers to property, so if you want to use it as property, you need to change how you are saving it:
props.put("users", words[1]);
If you do that, ${__P(users,1)} should work
Now, if you want to use this value as number of threads, then you need to do this:
Have Setup thread group with 1 thread, and your script
In the script you must save users as property, otherwise it won't pass between threads
Next thread group then can use it as number of threads

As long as your command line fits into 8191 characters it should not be a problem to pass as many arguments to JMeter as you want, here is an evidence from Debug Sampler and View Results Tree listener combination
So keep calm and pass as many parameters as needed via -J command line arguments.
Be aware that starting from JMeter version 3.1 users are recommended to use JSR223 Test Elements and Groovy language instead of Beanshell so going forward please consider switching to Groovy.

Related

Jenkins multi job project choice parameter

I have a multi job project which accepts some parameters and one of them is a choice parameter, because
I'm new to Jenkins it is defined manually through UI and without using groovy.
When the parameters selected or passed, there is a single build that will run for the defined parameters.
I would like to apply some changes and achieve the following behavior:
Execute this same multi job project with all parameters per selected options in the choice parameter.
e.g If selected 2 options in the choice parameter - It will trigger the build twice sequentially or in parallel, some sort of a loop with the parameters it received.
I tried to get some information regarding this online but because I'm not familiar with the proper terminology to search for, all I get is groovy scripts or answers which not related to what I need.
How I can achieve this?
Thanks in advance.
After additional search I came with the following solution:
change the Choice parameter to Extended choice parameter to allow multiple selection.
Create another job that will do the following:
a. Parse the received parameters from the Extended Choice parameter using shell script with the delimiter that is set in the 'Extended choice parameter options'
b. Execute build on the desired job from a loop by using API and passing the relevant parameters.
echo $OPTIONS
IFS=',' read -ra options_array <<< "$OPTIONS"
for option in "${options_array[#]}"
do
echo $option
curl -X POST "https://<user>:<password>#<jenkins_host>/job/<job_name>/buildWithParameters?parameter=${option}"
sleep 5
done
In case there are no free executors - increase the number of executors to allow multiple job executions
Edit job configuration that needs to be executed multiple times and enable the 'Execute concurrent builds if necessary' option.

Get Jenkins Workspace Number

if I am running concurrent Jenkins jobs, eg,
TestPipeline
TestPipeline#2
TestPipeline#3
How do I get the value of #2, #3, etc? Is there an env variable, or do I have to back it out from the path? It isn't EXECUTOR_NUMBER, which doesn't always match.
As #ben5556 told you in the comment, you have to parse the WORKSPACE environment variable.
For me, the best way to get the number (without using sh) is:
"#${env.WORKSPACE.split('#').last()}"
You can remove the # at the beginning of the string if you only want the number.

Using property/variable value from Jenkins

I got stuck to a really annoying issue. Looks pretty simple to solve but I can't see what am I doing wrong.
All started from JMeter: more than nine parameters from Jenkins.
I managed to get the values from Jenkins in Jmeter by spiting an array.
String line = "${__P(jenkinsparams)}";
String[] words = line.split(",");
String looks like:
-Jjenkinsparams="999,999,8443,1433,SQL2012,sa"
So I have:
words[0] = 999;
words[1] = 999;
words[2] = 8443;
[...]
words[5] = sa;
This operation is made inside a BeanShell Sampler with 1 thread. How can I use these values as further on? Even in different Thread Groups.
I've tried:
props.put("SqlIP",words[0]);
props.put("SqlInstance", words[1]);
but ${__P(SqlIP)} doesn't retrieve the value when used in JDBC Connection Configuration as:
jdbc:sqlserver://${__P(SqlIP)}\\${__P(SqlInstance)}
How can I use properties/variables to send data from that array to build an JDBC connection? I need them for: SQL IP, SQL instance, SQL username and SQL password. All sent in that array from Jenkins. Thank you
Because JDBC Connection Configuration is a Configuration element and according to Execution Order it's kicked off before any Beanshell test elements. You will have to come up with another way of setting the value.
I'm not aware of any parameters number limit on JMeter level, if you're running an exotic shell or a form of custom JMeter startup script which introduces this limitation you can work it around by putting your configuration into user.properties file or a custom properties file which can be passed via -q parameter, check out Apache JMeter Properties Customization Guide to learn more about setting and overriding JMeter Properties

ansible loop over comma separated list from jenkins

I have an environment variable injected by Jenkins like:
CUSTOMERS="foo,bar"
Now i need to loop over these values.
Is there any way to access these values AS items in ansible?
Any help including other suggestions how to to solve this is welcome.
You can pass in the environment variable to ansible with --extra-vars, but that's only part of the solution, you need to get the string value into a data format that ansible understands.
One straightforward option is to write a simple Python (or your preferred language) script to convert the environment variable to a JSON list and pass the JSON file to ansible as extra vars with --extra-vars "#customers.json" (JSON file input is available in ansible 1.3), see Ansible Variable documentation.
import sys
import os
import json
DEFAULT_VAR="CUSTOMERS"
def var_to_json(var_name, list_sep = ','):
var_dict = {var_name: os.environ[var_name].split(list_sep)}
return json.dumps(var_dict)
var_name=DEFAULT_VAR
if len(sys.argv) > 1:
var_name = sys.argv[1]
print var_to_json(var_name)
The script above could be generalized further (or customized to your situation). I'll have to leave it to you to hook the pieces together in your build environment.
Alternatively, as this previous answer explains, you can define a custom filter in ansible to process the input value. You could create a filter that converts the variable value to a list, then use the filter in your playbook when referencing the variable (presumably passed in via --extra-vars).

How to set Fitnesse variables in query string

I'm currently setting up Fitnesse, with FitSharp and the .net implementation of dbfit.
I understand how to trigger tests or suites from the submission of a URL, or from a command line, eg:
java -jar fitnesse-standalone.jar -c "MyTest?test&format=text"
What I can't figure out is how to submit variable values in this query string.
So, if I have a test containing a Sql statement which has a Fitnesse variable referenced in the Where clause, and the value of this variable is defined in a sibling static page, I would like to be able to run this test from the command line and submit a value for this variable which overrides the value in the static page. Something like:
java -jar fitnesse-standalone.jar -c "MyTest?test&format=text&${myVar}=abc"
Is this possible at all?
Thanks
Mark
There are two ways to pass variables from the command line, both involving environment variables.
(1) Define an environment variable (or identify one that already exists). You can use general purpose system variables (like %TMP% or %HOMEPATH%) or your own user-defined variables (e.g. %JAVA_HOME%) or create your own. My short Fitnesse launcher (a .CMD file) is this:
set SEED=%RANDOM%
set FITNESSE_PORT=9999
java -jar fitnesse-standalone.jar -p %FITNESSE_PORT% -e 0
The FITNESSE_PORT variable is defined just for use in the very next line. The SEED variable, however, does magic: it allows several people to run the same test simultaneously by generating unique values per session. (This assumes that each user runs their own FitNesse server, so each will thereby have a unique session.) I then instrument my tests by defining ids relative to the seed, e.g.
!define TestClient (MyTestClient_${SEED})
(2) Pass an environment variable setting scoped to just the java process that instantiates the FitNesse runner. This technique gives you precisely the same results with just a different implementation:
java -DSEED=%RANDOM% -jar fitnesse-standalone.jar -p %FITNESSE_PORT% -e 0
This yields precisely the same result within FitNesse, giving you access to the %SEED% environment variable as ${SEED}.
For more, see part 2 of my seven-part series on Acceptance Testing with FitNesse published on Simple-Talk.com.

Resources