如何为 Java 请求编写 JMeter 测试
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23286893/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to write a JMeter test for Java Request
提问by Dee
I need to do load testing on in memory databases.
我需要在内存数据库中进行负载测试。
I want to use JMeter and am under the impression I need to write a class that implements JavaSamplerClient.
我想使用 JMeter 并且我的印象是我需要编写一个实现 JavaSamplerClient 的类。
I simply have no idea where to start. What the JMeter website has to offer helps me very little. This is my first time doing something like this and I have been lost for days!
我根本不知道从哪里开始。JMeter 网站提供的内容对我帮助很小。这是我第一次做这样的事情,我已经迷路了好几天!
So maybe you can help by explaining like the basics of how my class should be set out? Packages I might need to import? Because whenever I try to tell it to implement JavaSamplerClient I get an error.
所以也许你可以通过解释我的课程应该如何设置的基础知识来提供帮助?我可能需要导入的包?因为每当我试图告诉它实现 JavaSamplerClient 时,我都会收到错误消息。
Also maybe a brief summary on how it all works? Like is a method run as many times as specified in JMeter? Or what is actually happening here?
也可能是关于这一切如何运作的简要总结?就像一个方法运行的次数与 JMeter 中指定的一样多?或者这里实际发生了什么?
回答by Dmitri T
Your custom class needs either implement JavaSamplerClientor to extend AbstractSamplerClient.
您的自定义类需要实现JavaSamplerClient或扩展AbstractSamplerClient。
The absolute minimum is runTest() method implementation.
绝对最小值是 runTest() 方法实现。
I'd recommend to look into sources for existing JavaTest and SleepTest Java Request samplers:
我建议查看现有 JavaTest 和 SleepTest Java Request 采样器的来源:
- /src/protocol/java/org/apache/jmeter/protocol/java/test/JavaTest.java
- /src/protocol/java/org/apache/jmeter/protocol/java/test/SleepTest.java
- /src/protocol/java/org/apache/jmeter/protocol/java/test/JavaTest.java
- /src/protocol/java/org/apache/jmeter/protocol/java/test/SleepTest.java
Sources are available from JMeter download page
来源可从JMeter 下载页面获得
Or there is a couple of guides which have example of simple working Java Requests.
或者有几个指南有简单的工作 Java 请求的例子。
See
看
- Beanshell vs JSR223 vs Java JMeter Scriptingcomparison benchmark - for something very basic like generating large random string
- WebSocket Testing With Apache JMeter- for fully functional Websocket client implementation via Java Request
- Beanshell vs JSR223 vs Java JMeter Scripting比较基准 - 对于一些非常基本的东西,比如生成大随机字符串
- 使用 Apache JMeter进行WebSocket 测试- 通过 Java 请求实现功能齐全的 Websocket 客户端
After compiling your classes package it to .jar and drop to /lib/ext folder of your JMeter installation. Your class should be available from Java Request drop down.
编译类后,将其打包为 .jar 并放到 JMeter 安装的 /lib/ext 文件夹中。您的类应该可以从 Java 请求下拉列表中获得。
Hope this helps.
希望这可以帮助。
回答by Imen CHOK
To use Java Request in JMeter you must create a Java class that inherits from JavaSamplerClient.
To do that, you must download two jar files and add them to classpath if you are working with Eclipse.
This two jar files are ApacheJMeter_core.jarand ApacheJMeter_java.jarAn your class will look like that:
要在 JMeter 中使用 Java Request,您必须创建一个继承自JavaSamplerClient. 为此,如果您使用 Eclipse,您必须下载两个 jar 文件并将它们添加到类路径。这两个 jar 文件是ApacheJMeter_core.jar和ApacheJMeter_java.jar您的类将如下所示:
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;
public class javaRequest extends AbstractJavaSamplerClient {
@Override
public void setupTest(JavaSamplerContext context){
// TODO Auto-generated method stub
super.setupTest(context);
}
@Override
public Arguments getDefaultParameters() {
// TODO Auto-generated method stub
}
@Override
public SampleResult runTest(JavaSamplerContext arg0) {
// TODO Auto-generated method stub
SampleResult result = new SampleResult();
boolean success = true;
result.sampleStart();
// Write your test code here.
//
result.sampleEnd();
result.setSuccessful(success);
return result;
}
@Override
public void teardownTest(JavaSamplerContext context){
// TODO Auto-generated method stub
driver.quit();
String verificationErrorString = verificationErrors.toString();
if (!"".equals(verificationErrorString)) {
fail(verificationErrorString);
System.out.println(verificationErrorString);
}
super.teardownTest(context);
}
}
For more informations you can visit this link http://www.javacodegeeks.com/2012/05/apache-jmeter-load-test-whatever-you.html/comment-page-1/#comment-8288and this page too How to use CSV Data Set with junit request test in jmeter
有关更多信息,您可以访问此链接 http://www.javacodegeeks.com/2012/05/apache-jmeter-load-test-whatever-you.html/comment-page-1/#comment-8288以及此页面 如何在 jmeter 中使用带有 junit 请求测试的 CSV 数据集
回答by David Groomes
Dmitri and Imen's answers are correct.
德米特里和伊门的答案是正确的。
Here is a working example project.
这是一个工作示例项目。
It uses Gradle and includes instructions on how to build and execute it. It should be a good reference point.
它使用 Gradle 并包含有关如何构建和执行它的说明。它应该是一个很好的参考点。
https://github.com/dgroomes/jmeter-playground
https://github.com/dgroomes/jmeter-playground
Also, here is the official JMeter documentation on making your custom plugin available in the JMeter tool: https://jmeter.apache.org/usermanual/get-started.html#classpath.
此外,这里是关于在 JMeter 工具中提供自定义插件的官方 JMeter 文档:https: //jmeter.apache.org/usermanual/get-started.html#classpath。

