Preface:
One day to do performance testing, encountered a page of data extraction, at first thinking about using regular expressions are not extracted. Later carefully think about the wrong, a single extraction seems to be able to do, the dynamic variable number of extraction, but also splicing, it seems to write, even if it can be written to estimate is disgusting death. Look carefully, this is not the database inside the thing? directly from the database can not be finished. After searching all kinds of processors and discovering that there really is a JDBC processor that seems to be able to study, after a period of trying, finally this piece of the whole understanding.
Against the beginning of the demand, this is not to write a SQL Isolated data, and then stitching together the results of the query is not finished? Around so big a circle. Business is not ripe to kill people.
In the beginning, only on the JDBC processor, hoping to write a SQL to solve the problem, because it is a multi-line, multi-column data combination, did not get out for half a day, almost give up. Later, after colleagues to wake up, the last BeanShell Sampler did not knot, Balabala said the use of methods, finally understand the smell. Studied for half a day, and finally solved the problem satisfactorily. Now record it to save the dementia. Don't say much nonsense, open up.
JDBC Processor
To ensure the completeness of the description, or from the JDBC Connection Configuration step by step. Or take hyperpacer as an example. First, add a JDBC Connection configuration, see:
with database connection The students don't have to say a single field to write on the line. Connection pool name a little bit, is to give the database connection name, in the future when the call to make a distinction, such as to launch a number of different databases jdbc jdbc The connection configuration is under which node, It can all be used globally under the same scenario. To avoid confusion caused by duplicate names, it is recommended to have all jdbc Connect in one place, Easy to manage.
After the connection is configured, it is time to write SQL , which has two options: one is the jdbc preprocessor and one is the jdbc post processor. Two functions almost, simply speaking, one is executed before the request is sent, and one is executed after the request is sent. This seems to be the current treatment of the problem is not too large, did not do a detailed study, and will have time to do detailed research. This block takes the JDBC backend processor As an example, see:
Connection pool Name: The name that was written in the JDBC connection configuration that you just configured .
Statement type: Just what type of statement to execute, this time you need to write a query SQL, so directly with the default Select Statement. If you need to update the database after running to a request, it seems that you can use the update or something, no investigation, no say.
SQL statement: Just write the SQL directly to the line, because this piece does not add grammar check, preferably in the outside after debugging and then sticky, to avoid because of the spelling of what the impact of subsequent processing.
Variable name: One alias for each column for the data being looked up. The name assigned here is used to refer to each column of data later. Multiple columns separated by commas, or the old saying, try to use meaningful names to name variables, not with the C language of Lao Tan , to where all is the single letter ABC. (Looks like the age of exposure, eh!) The remaining several parameters temporarily useless, also did not go to scrutiny, waiting for the future to have time to die knock. The time is tight, the task is heavy, you understand.
Once this is configured, the variables can be used directly. For example, to query the results of the first line of recid, you can write vars.get ("Recid_1"), the second line of dbid, you can write vars.get ("db_2") . But what we want is to be multi-line splicing, more abominable is not sure the number of rows, this piece needs BeanShell show skill. Turn down a question,BeanShell how to play?
BeanShell Sampling Device
At first do not know what BeanShell is an east, after colleagues to know, this is true java Script(note space), basically java What can be written here, how to get here.
So according to the requirements, in the following box, write down this piece of code:
1 vars.put ("Dbdata", " 2 int i = 1; I<=integer.parseint (Vars.get ("dbid_#")); I++ i)); 4 5 vars.put ("Dbdata", Vars.get ("Dbdata"). Substring (1, vars.get ("Dbdata" ). Length ())); 6 Log.info ("dbdata:" + vars.get ("dbdata"));
Technically there is no difficulty, that is, there are a few to (keng le ) meaning ( wo hyperpacer followed by jmeter put Span style= "font-family: the song Body;" > Set the variable by get get variables. Due to the need for string stitching, lazy cancer at the end, do not want to study stringbuffer that pile of stuff. Directly on the string connection.
Like Java , an object is assigned an initial value, and if it is connected directly with a variable that does not have an initial value, it is a null -converted string. (First line)
Because it's a progressive data connection.,so go on. forloops. and theJavabasically the same, because the line number is from1started, soIthe initial value is1. The termination condition becomes hereInteger.parseint (Vars.get ("dbid_#"))Such a disgusting stuff,dbid_#is throughSQLquery out ofdbidthe number of variables. Seems to be because of the passageVARsof theGetthe method gets all the strings, so it's going to be converted to integer (if the dynamic language is easier). (second line)
Within the loop body, through the specified i, the data of different rows isolated from SQL can be connected sequentially. "recid_" + I is the data for row I of the recid column in the query results . String connection There is nothing to say, and Java is the same. (line three)
Because there was an extra comma in the beginning (why?) You guess? ), so take the remaining string from the second word character and return it. (Line fifth)
To ensure correctness, print a check. JMeter the print inside is not System.out.println () this, to write Long.info () , commonly used JMeter that's a basic knowledge. (line sixth)
Now that the relevant configuration is all over , all the components behind the BeanShell sampler can be obtained by vars.get ("Dbdata") .
As for the parameters, the configuration of the script files, there is a chance to do research, it has taken a lot of time.
Experience is: 工欲善其事, its prerequisite. Tools and flexible use, in order to enable the work to carry out the flexible and efficient rensili son.
Combined application of JDBC processor and BeanShell Sampler