In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains "how to write java requests by jmeter". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how jmeter writes java requests".
In general, it is recommended that you use jmeter to write Java requests
Have the following advantages
Scripts are easy to maintain
Easy debugging
The development script cycle is short
However, there are more articles about extending java requests on the Internet, through the implementation of org.apache.jmeter.protocol.java.sampler.JavaSamplerClient interfaces.
Or inherit the AbstractJavaSamplerClient virtual class to implement the java request
The general steps are as follows:
1. Establish java project
two。 Loading dependency packages into java project environment variables is required when writing ApacheJMeter_core.jar and ApacheJMeter_java.jar, and debugging requires loading all dependent packages of jmeter (all under jmeter_home/lib, actually not all, there are many packages, it is difficult to distinguish)
3. Write JavaSampler, inherit AbstractJavaSamplerClient, and override the following methods
[java] view plain copy
Public Arguments getDefaultParameters (); optional, define available parameters and default values
Public void setupTest (JavaSamplerContext arg0): optional, execute before testing, do some initialization work
Public SampleResult runTest (JavaSamplerContext arg0); required to implement custom request
Public void teardownTest (JavaSamplerContext arg0): optional, called at the end of the test
4. Pack the jar package and put it under the jmeter_home/lib/ext/
5. Remember to put all additional (custom protocol dependent, non-jmeter) dependency packages in the jmeter environment variable
6. Restart jmeter and create a java Sampler to select the test class you just defined
7. Adjust the parameters and start the test.
Introduction to writing templates
[java] view plain copy
Package com.sampler
/ / write packages to be loaded by the jmeter.sampler plug-in
Import org.apache.jmeter.config.Arguments
Import org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient
Import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext
Import org.apache.jmeter.samplers.SampleResult
/ / handle exception stack
Import java.io.PrintWriter
Import java.io.StringWriter
/ * *
*
* @ author wo niu
* @ func test ejb interface
*
* inherit the virtual class AbstractJavaSamplerClient
, /
Public class JavaSampler extends AbstractJavaSamplerClient {
/ / main is for debugging only. It is best to debug before packaging.
/ / Please load all jar packages under jmeter_home/lib into IDE tool environment variables before running
Public static void main (String [] args)
{
Arguments args0 = new Arguments ()
Args0.addArgument ("parm_1", "val_1")
Args0.addArgument ("parm_2", "val_2")
Args0.addArgument ("parm_N", "val_N")
JavaSampler test = new JavaSampler ()
JavaSamplerContext context = new JavaSamplerContext (args0)
Test.setupTest (context)
Test.runTest (context)
Test.teardownTest (context)
}
/ * *
* implement the runTest (JavaSamplerContext context) method
* runTest () specifically implements the test action
, /
Public SampleResult runTest (JavaSamplerContext context) {
/ *
* SampleResult can only be defined as a local variable to avoid multi-thread safety problems
* it is wrong to define some online posts as global variables.
, /
SampleResult results = new SampleResult ()
/ / the default request is successful
Results.setSuccessful (true)
Results.sampleStart (); / / start recording response time
Try {
/ / dynamic variables are read from context:
/ / String key = context.getParameter ("key")
/ / call the API TO-DO ejb
If (false) {/ / handle on failure
Results.setSuccessful (false)
Results.setResponseData ("response data", "utf8")
}
} catch (Throwable e) {
E.printStackTrace ()
Results.setSuccessful (false)
/ / the exception handling stack is String, and only String can write back the response data
Results.setResponseData (toStringStackTrace (e), "utf8")
}
Results.sampleEnd (); / / record response time ends
Return results
}
/ * *
* called at the beginning of the test to initialize
, /
Public void setupTest (JavaSamplerContext context) {
}
/ * *
* called at the end of the test
, /
Public void teardownTest (JavaSamplerContext context) {
}
/ * *
* define default parameters
, /
@ Override
Public Arguments getDefaultParameters () {
Arguments args = new Arguments ()
/ *
* test data
, /
Args.addArgument ("parm_1", "val_1")
Args.addArgument ("parm_2", "val_2")
Args.addArgument ("parm_N", "val_N")
Return args
}
/ * *
* the exception handling stack is String, and only String can write back response data
* @ param e
* @ return
, /
Private String toStringStackTrace (Throwable e) {
String exception = null
Try {
StringWriter sw = new StringWriter ()
PrintWriter pw = new PrintWriter (sw)
E.printStackTrace (pw)
Exception = sw.toString ()
Pw.close ()
Sw.close ()
} catch (Exception E1) {
E1.printStackTrace ()
}
Return exception
}
}
At this point, I believe you have a deeper understanding of "how jmeter writes java requests". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.