Simple Groovy Script to Read Csv File
HTTP Simple Table Server
Download
Functioning testing with JMeter can be done with several JMeter injectors (on a remote host) and 1 JMeter controller (with GUI, on your local host). Scripts are sent to JMeter injectors using RMI protocol. Results are brought back periodically to the JMeter controller. Unfortunately the dataset and csv files aren't transferred from the controller to injectors.
The principal idea is to use a tiny http server in JMeter Plugins to manage the dataset files with simple commands to become / add rows of data in files.
Configuration
In jmeter.properties file:
jmeterPlugin.sts.port=9191 jmeterPlugin.sts.addTimestamp=truthful jmeterPlugin.sts.datasetDirectory=D:/jmeter/dataset jmeterPlugin.sts.loadAndRunOnStartup=false
Do not use '\' in the path directory, it doesn't work well, apply '/' or '\' instead. It is also recommended to apply UTF-8 as the encoding:
sampleresult.default.encoding=UTF-8
If you want automatically start a Simple Tabular array Server on JMeter STARTUP simply add "simple-table-server.bsh" in the "beanshell.init.file" property. Be sure that simple-table-server.bsh file is in your JMETER_HOME/bin directory.
The Elementary Table Server is a tiny http server which can send http GET/Mail service requests on port 9191 (by default). You can set up a custom port through the graphical user interface or by overriding the jmeterplugin.sts.port belongings.
Distributed architecture for JMeter
The Simple Table Server runs on the JMeter controller (master) and load generators (slaves) make calls to the STS to get or add some data. At the beginning of the examination, the first load generator will load data in memory (initial call) and at the end of the exam information technology asks the STS saving values in a file. All the load generators ask information from the aforementioned STS which is started on the JMeter controller.
Getting Started
There are dissimilar ways to showtime the STS:
- with JMeter GUI:
- with simple-table-server.cmd (.sh for UNIX) script located in your JMETER_HOME/bin directory. Parameters are read in the jmeter.properties or y'all could besides set parameters like simple-table-server.cmd -DjmeterPlugin.sts.addTimestamp=true -DjmeterPlugin.sts.datasetDirectory=D:/jmeter/dataset
- on JMeter CLI startup (Windows : jmeter-n.cmd or Linux jmeter -due north) following backdrop in jmeter.backdrop file or in user.backdrop :
jmeterPlugin.sts.loadAndRunOnStartup=truthful jmeterPlugin.sts.port=9191 beanshell.init.file=uncomplicated-tabular array-server.bsh
When the STS is running go to http://<HOST>:<PORT>/sts/ to see all available commands.
Calls are synchronized, all commands are executed one by one.Example of a dataset file logins.csv:
login1;password1 login2;password2 login3;password3 login4;password4 login5;password5
INITFILE
Load file in retentiveness. Lines are stored in a linked listing, 1 line = 1 chemical elementThe filename is limited to 128 characters maxi and must not contains characters \ / : or ..
http://hostname:port/sts/INITFILE?FILENAME=logins.csv
HTML format:
<html><title>OK</championship>
<torso>5</body><!-- number of lines read -->
</html>
Linked list after this control:
login1;password1 login2;password2 login3;password3 login4;password4 login5;password5
READ
Get i line from listhttp://hostname:port/sts/READ?READ_MODE={FIRST, LAST, RANDOM}&Go on={TRUE, Imitation}&FILENAME=logins.csv
HTML format:
<html><title>OK</title>
<torso>login1;password1</body>
</html>
Bachelor options:
- READ_MODE=FIRST => login1;password1
- READ_MODE=Concluding => login5;password5
- READ_MODE=RANDOM => login?;password?
- Go along=TRUE => the data is kept and put to the end of list
- Keep=Fake => the data is removed
KEEP=TRUE, READ_MODE=Start => login1;password1
Linked list afterward this command:
login2;password2 login3;password3 login4;password4 login5;password5 login1;password1
Proceed=TRUE, READ_MODE=LAST => login5;password5
Linked list after this command:
login1;password1 login2;password2 login3;password3 login4;password4 login5;password5
Keep=Truthful, READ_MODE=RANDOM => login2;password2
Linked list after this control:
login1;password1 login3;password3 login4;password4 login5;password5 login2;password2
KEEP=Fake (delete style), READ_MODE=FIRST => login1;password1
Linked list after this command:
login2;password2 login3;password3 login4;password4 login5;password5
Proceed=FALSE, READ_MODE=LAST => login5;password5
Linked list after this control:
login1;password1 login2;password2 login3;password3 login4;password4
KEEP=FALSE, READ_MODE=RANDOM => login2;password2
Linked list afterwards this command:
login1;password1 login3;password3 login4;password4 login5;password5
Add
Add a line into a file: (Become OR Postal service HTTP protocol)Get Protocol
http://hostname:port/sts/ADD?FILENAME=dossier.csv&LINE=D0001123&ADD_MODE={FIRST, Concluding}&UNIQUE={FALSE, TRUE}
GET Parameters : FILENAME=dossier.csv&LINE=D0001123&ADD_MODE={FIRST, Concluding}&UNIQUE={FALSE, Truthful}
POST Protocol http://hostname:port/sts/Add together
Mail Parameters : FILENAME=dossier.csv, LINE=D0001123, ADD_MODE={FIRST, LAST}, UNIQUE={Imitation, TRUE}
HTML format:
<html><championship>OK</title>
<torso></trunk>
</html>
Available options:
- ADD_MODE=FIRST => add to the top
- ADD_MODE=Final => add to the end
- FILENAME=dossier.csv => if doesn't already exist information technology creates a LinkList in memory
- LINE=1234;98763 => the line to add
- UNIQUE=True => do not add line if the list already contains such line
Mail service Protocol with parameters
LENGTH
Return the number of remaining lines of a linked listinghttp://hostname:port/sts/LENGTH?FILENAME=logins.csv
HTML format:
<html><title>OK</title>
<body>5</body><!-- remaining lines -->
</html>
STATUS
Display the listing of loaded files and the number of remaining lines for each linked listinghttp://hostname:port/sts/STATUS
HTML format:
<html><championship>OK</title>
<body>
logins.csv = 5<br />
dossier.csv = 1<br />
</torso></html>
SAVE
Save the specified linked list in a file to the specified locationhttp://hostname:port/sts/Salve?FILENAME=logins.csv
If jmeterPlugin.sts.addTimestamp is set to true so a timestamp volition exist add to the filename, the file is stored in the custom directory specified by editing the jmeterPlugin.sts.datasetDirectory property or in JMETER_HOME/bin directory past default:
20140520T16h33m27s.logins.csv
HTML format:
<html><title>OK</title>
<body>5</body><!-- number of lines saved -->
</html>
RESET
Remove all of the elements from the specified listhttp://hostname:port/sts/RESET?FILENAME=logins.csv
HTML format:
<html><championship>OK</title>
<trunk></body>
</html>
STOP
Shutdown the Unproblematic Table Serverhttp://hostname:port/sts/Terminate
See examples below for further information.
Using STS in a Test Plan
Initialize file using a "setUp Thread Group" by calling URL with one or more HTTP Asking Sampler.
Reading a row of data is done past calling READ method at each iteration by a HTTP Asking Sampler. And so you can employ a Regular Expression Extractor to parse the response data.
Reading login:
Reading password:
At the stop of your Test Plan you tin salvage remaining/calculation information with a HTTP Asking Sampler in a "tearDown Thread Grouping".
If you lot need to parse more than 2 columns, you lot could use a Post-Processeur neat JSR223 like :
// E.grand. read line = <body>COL1VALUE;COL2VALUE;COL3VALUE;COL4VALUE;COL5VALUE</body>
// cavalcade separator = ';'
String lineData = org.apache.commons.lang3.StringUtils.substringBetween (prev.getResponseDataAsString ( ) , "<torso>" , "</trunk>" ) ;
// lineData = COL1VALUE;COL2VALUE;COL3VALUE;COL4VALUE;COL5VALUE
def tabParam = org.apache.eatables.lang3.StringUtils.splitPreserveAllTokens (lineData, ';' ) ; // use column separator = ';'
if (tabParam.length == 5 ) {
vars.put ( "V_COL1PARAM" , tabParam[ 0 ] ) ; // variable V_COL1PARAM = COL1VALUE (String value)
vars.put ( "V_COL2PARAM" , tabParam[ i ] ) ; // variable V_COL2PARAM = COL2VALUE (String value)
vars.put ( "V_COL3PARM" , tabParam[ 2 ] ) ;
vars.put ( "V_COL4PARAM" , tabParam[ 3 ] ) ;
vars.put ( "V_COL5PARM" , tabParam[ 4 ] ) ;
}
else {
log.info ( "Mistake, Need v columns" ) ;
}
Example
- Put the logins.csv file in your JMETER_HOME/bin directory:
- Run the Simple Table Server manually with the elementary-tabular array-server.cmd file or automatically with beanShell configuration.
- Run 1 of the following scripts:
In a loop, read random values from a file containing a login and a password at each row:
Download Example Test Plan 1 Read random values example
Read value from a file containing a login and a countersign at each row, each value is unique and cannot exist read anymore:
Download Example Test Programme two Consume login lines example
Add rows in a new linked listing and salve information technology in a file when the test is done:
Download Example Exam Plan 3 Save list example
Read in a random mode a dataset located on the controller machine with severals slaves. The start injector loads the dataset in memory while the other injectors are waiting few seconds. The different injectors read randomly the data containing logins and passwords. When the test is washed the kickoff injector save the values in a file with a timestamp every bit prefix:
Download Case Test Program 4 Multi JMeter injectors and salvage list examples
Y'all can override STS settings using command-line options:
- -DjmeterPlugin.sts.port=<port number>
- -DjmeterPlugin.sts.loadAndRunOnStartup=<truthful/imitation>
- -DjmeterPlugin.sts.datasetDirectory=<path/to/your/directory>
- -DjmeterPlugin.sts.addTimestamp=<true/false>
jmeter.bat -DjmeterPlugin.sts.loadAndRunOnStartup=truthful -DjmeterPlugin.sts.port=9191 -DjmeterPlugin.sts.datasetDirectory=d:/information -north –t testdemo.jmx
When it's washed see results in the Listener Tree View.
If y'all want to use the Http Simple Server with the JMeter Maven plugin, you could :
- put your csv files in <project>/src/test/jmeter directory (e.g : logins.csv)
- put the simple-tabular array-server.bsh (beanshell script) in <project>/src/test/jmeter directory
- put the your jmeter script in <project>/src/test/jmeter directory (e.g : test_login.jmx)
- declare in the maven build section, in the configuration > jmeterExtensions > declare the artifact kg.apc:jmeter-plugins-table-server:<version>
- declare user backdrop for STS configuration and automatic first
Extract pom.xml defended to Http Unproblematic Tabular array Server :
<build>
<plugins>
<plugin>
<groupId>com.lazerycode.jmeter </groupId>
<artifactId>jmeter-maven-plugin</artifactId>
<version>3.iv.0</version>
...
<configuration>
<jmeterExtensions>
<artifact>kg.apc:jmeter-plugins-table-server:2.four </artifact>
</jmeterExtensions>
<propertiesUser>
<!-- properties configuration for Http Simple Table Server with automatic commencement when JMeter start -->
<jmeterPlugin.sts.port > 9191 </jmeterPlugin.sts.port >
<jmeterPlugin.sts.addTimestamp >true</jmeterPlugin.sts.addTimestamp >
<jmeterPlugin.sts.datasetDirectory >${projection.build.directory }/jmeter/testFiles</jmeterPlugin.sts.datasetDirectory >
<jmeterPlugin.sts.loadAndRunOnStartup >true</jmeterPlugin.sts.loadAndRunOnStartup
<beanshell.init.file >${projection.build.directory }/jmeter/testFiles/simple-table-server.bsh </beanshell.init.file >
</propertiesUser>
</configuration>
</plugin>
</plugins>
</build>
Source: https://jmeter-plugins.org/wiki/HttpSimpleTableServer/
0 Response to "Simple Groovy Script to Read Csv File"
Post a Comment