There is a task that requires frequent order requests and analysis of the order request with no duplicate order number, the idea is to send an HTTP request with JMeter, use regular expressions to get the order number, and provide the order number and thread number as parameters to the Java request. Write the order number in the Java request to the named file that includes the thread number. The way it's done.
The steps are as follows:
1. Jmeter Send HTTP request
1) New HTTP header Manager
2) New HTTP request
2, after the next slip return some information as follows: "MSG": "Order Success", "Result": "1", "Sendcode": "96828628", "Weektm": "Tuesday, 12:00"
Create a new regular expression extractor, get the value 96828628 Sendcode followed by the regular expression: "Sendcode": "(\d{8}) so that the order number is stored in the variable Sendcode
3, to this, the order number Sendcode has been obtained, you need to save this order number to a file, you can use "Save response to file" to save the response to a thread-named file, but the bad thing is a file can only save a result, cannot append results to the results, for late Rollup inconvenient
This is done using a Java request, named after the thread file name, the same thread all the response results are saved to the same file, n threads to save n files, so that the use of Java requests, Java requests need to do
1) Create a new Java class AppendFile function is to append content to the file
Package editfile;import java.io.ioexception;import java.io.randomaccessfile;public class AppendFile {public static void main (string[] args) { appendfile.appendfile ("E:\\dd.txt", "1111"); Appendfile.appendfile ("E:\\dd.txt", "11111");} public static void AppendFile (string fileName, string content) { Try {//Open a random Access file stream, read and write Randomaccessfile randomfile = new Randomaccessfile (fileName, "RW");//File length, number of bytes long Filelength = randomfile.length ();//Move the Write file pointer to the end of the file. Randomfile.seek (filelength); randomfile.writebytes (content + \ r \ n); Randomfile.close ();} Catch ( IOException e) {e.printstacktrace (); }}}
2) Create a new Java request Class Writeresult, the role is to get order number and thread number from JMeter, and use the order number and thread number to write the person file, while JMeter performance testing of the Java request is also implemented, according to the format in the runtest to make various requests.
PackageEditfile;importOrg.apache.jmeter.config.arguments;importOrg.apache.jmeter.protocol.java.sampler.javasamplerclient;importOrg.apache.jmeter.protocol.java.sampler.javasamplercontext;importOrg.apache.jmeter.samplers.sampleresult;public class Writeresult Implementsjavasamplerclient {PrivateSampleresult results; PrivateString Sencode; PrivateString Threadnumber; Set incoming parameters from JMeter publicArguments getdefaultparameters () {Arguments params = newArguments (); Params.addargument ("Sencode", "0");//Set Sencode parameter Params.addargument ("Threadnumber", "0");//Set Threadnumber returnParams }//initialization method, performance test only once public voidSetuptest (Javasamplercontext arg0) {}//re-executing the test where public sampleresult runtest (Javasamplercontext arg0) {Sencode = Arg0.getparameter ("Sencode");//Get JMeter incoming parameter value, Threadnumber = Arg0.getparameter ("Threadnumber"); Gets the parameter value passed in JMeter, results = new sampleresult (); Results.samplestart ();//JMeter Start statistic response time marker Appendfile.appendfile ("e:\\jmeter\\" +threadnumber+ ". txt" , sencode); if (Sencode.length () ==8 ) { Results.setsuccessful (True ); Results.setresponsedata ("Threadnumber:" +threadnumber+ "|sencode:" +senCode, Null ); }else {results.setsuccessful (false ); Results.setresponsedata ("Threadnumber:" +threadnumber+ "| No verification code obtained ", null );} Results.sampleend ();//JMeter End Statistic Response Time token return results;}//End method, run only once per thread, execute public void after test method run ends Teardowntest (Javasamplercontext arg0) {} public static void main (string[] args) {//TODO auto-generated method Stu b }}
Export the Java project file to the jar package and place it in the JMeter Lib/ext folder
Open JMeter Add a jmeter Java request, check the test class, automatically display 2 parameters Sendcode and Threadnumber (these 2 parameters are set in the method of the Java Request class: public Arguments Getdefaultparameters ()
Sendcode = ${sendcode}
Threadnumber = ${__threadnum}
Executes multiple times with 3 threads, with the following results:
In addition, you can save the file with Bean Shel, you need to write the code in the bean shell
Import editfile.*; import org.apache.jmeter.threads.jmetercontextservice;int threadnumber = Jmetercontextservice.getnumberofthreads (); String Sendcode = Vars.get ("Sendcode"); Turn the acquired sendcode into the bean shell variable Value appendfile.appendfile ("E:\\jmeter\\result" +threadnumber+ ". txt", sendcode); System.out.println ("finished");
But get the thread number code is 1, so all the order number is saved in the same file, do not know whether it will be written in the same file caused the file is not complete, to test to know
JMeter sends an HTTP request and saves the requested order information to a file