SampleHere I use main JMeter threads and reports to extend the Application category of "Java requests. To expand this application, lib/ext/ApacheJMeter_java.jar is used to encapsulate this application.
First, you must inherit the abstract class abstractpolicamplerclient in role. It provides four Abstract METHODS: void setupTest (abstramplercontext arg0), SampleResult runTest (abstramplercontext arg0), void teardownTest (abstramplercontext arg0 ), arguments getDefaultParameters (). SetupTest and teardownTest are similar to those in JUnit, which are executed at the start and end of the test respectively. RunTest is the main test method. All test programs are completed here. The parameter cyclamplercontext encapsulates all external data (such as configuration information) required in the test ). Its SampleResult encapsulates all test results (for example, is the test successful? Program execution time), getdefaparameparameters is used to display some of the test's default data to the JMeter client before the program runs, which can be used with runTest for data presentation and configuration.
The program is as follows:
Import org. apache. jmeter. config. Arguments;
Import org. apache. jmeter. protocol. java. sampler. abstract=amplerclient;
Import org. apache. jmeter. protocol. java. sampler. datagamplercontext;
Import org. apache. jmeter. samplers. SampleResult;
Import com. ebupt. wv. client. WVClient;
Public class Login2 extends act1_amplerclient {
Private static String label = "userLogin"
Private String name;
Private String address;
Public void setupTest (JavaSamplerContext arg0 ){
System. out. println ("setupTest ");
}
Public SampleResult runTest (datagamplercontext arg0 ){
Name = arg0.getParameter ("name ");
Address = arg0.getParameter ("address ");
SampleResult sr;
Sr = new SampleResult ();
Sr. setSampleLabel (label );
Try {
Sr. sampleStart (); // record the program execution time and execution result
Sr. setSuccessful (true );
System. out. println ("send over! ");
} Catch (Throwable e ){
Sr. setSuccessful (false );
} Finally {
Sr. sampleEnd ();
}
Return sr;
}
Public void teardownTest (datagamplercontext arg0 ){
}
Public Arguments getDefaultParameters (){
Arguments params = new Arguments ();
Params. addArgument ("name", "JMeter ");
Params. addArgument ("address", "localhost ");
Return params;
}
}
Next, package the written application and put it under lib/ext. You can find it at JMeter runtime. At the same time, you must place the packages on which your applications depend under lib. Run JMeter. After the interface is opened, right-click "test plan" and add a thread group. Right-click "thread group" and choose "Add-Sampler-Java request". Select the class you just wrote in the class name.
Here, you can add multiple thread groups, multiple java requests, and a listener to output different test results and reports, each request can be controlled by a logical controller.
Finally, click Run-start to save the corresponding project to start running. You can use the listener to write data during runtime to generate reports as needed.