2

I need to make sure,whether table has data or not ,based on the environment,say for example i have two two database,one is development another one is production like given below.

Production 
host1
dbuser1
dbpassword1
dburl
Tablename : studentinfo

Development
dbuser2
dbpassword2
dburl2
Tablename : studentinfo

FYI : studentinfo has same structure in both environment .

In Jmeter ,User Defined Variable( UDV), I have configured two set of database information. Using BeanShell Processor I have trying to change the database connectivity information, Is there any way to change the Db Config dynamically ?

Given below is my Jmeter UDV

env : prod
prod_db_url: dburl
prod_db_user:usr
prod_db_password:password

dev_db_url: dburl
dev_db_user:usr
dev_db_password:password

In My Beanshell Preprocessor

String env=vars.get("env");
if(env.equlas("prod")){
   // Load the Prod db into vars
} else if (env.equals("dev")){
   // Load the Dev db into vars
}

Here ,I am setting the values in vars, and trying to get the information from DB Configuration variables. but i am not able get values the in DB config.

Can anyone explain ? what went wrong or what is the approach to get connection?

Vasanth
  • 474
  • 2
  • 9
  • 31
  • Similar question ,http://stackoverflow.com/questions/36779835/manipulating-jmeter-jdbc-connection-fields-in-java-beanshell-code?rq=1 – Vasanth Apr 25 '16 at 17:49

2 Answers2

5

JDBC Connection Configuration is initialized after User Defined Variables but before any PreProcessor, that's why you don't see values changes.

Consider using JMeter Properties instead of JMeter Variables, i.e. change ${dburl} in JDBC Connection Configuration to ${__P(dburl,)}. Do the same for credentials variables.

Depending on how you run your test you can set properties value:

  1. Via -J command-line argument like:

    jmeter -Jdburl=jdbc://somedb:port/etc -Juser=foo -Jpassword=bar 
    
  2. Put it into user.properties file (located in JMeter's "bin" folder) like

    dburl=jdbc://somedb:port/etc
    user=foo
    password=bar
    

References:

Dmitri T
  • 159,985
  • 5
  • 83
  • 133
  • this is something that is hidden in the jmeter documentation can you point me where in the hell in the JMeter doc JDBC Configuration initialized before any PreProcessor?? Thanks!!! – user63898 Mar 06 '18 at 08:42
4

You do not need a Beanshell PreProcessor for this.

For the below UDV,

env : prod
prod_db_url: dburl
prod_db_user:usr
prod_db_password:password

dev_db_url: dburl
dev_db_user:usr
dev_db_password:password

Just by changing the value of env, you can access all the variables values by using

${__V(${env}_db_url)} // return prod or dev db url depends on the value of env.


Another nice solution:

Can you have the same variables and store them in 2 different property files?

prod.proeprties

db_url=dburl
db_user=usr
db_password=password

dev.properties

db_url=dburl
db_user=usr
db_password=password

You can use the JMeter - Property File Reader.

Property reader file path would be /path/to/${env}.properties Access all the variables using ${__P(db_url)}, ${__P(db_user)}

vins
  • 15,030
  • 3
  • 36
  • 47