使用 JMeter 测试 Java 类

发布于 2024-08-24 00:24:40 字数 630 浏览 1 评论 0原文

我想测试 Java 服务调用。 我的第一次尝试是使用 "Java Request Sampler" 文档说

这个采样器可以让你控制一个java 实现的类 JavaSamplerClient 接口。

我不确定如何获取 org.apache.jmeter.protocol.java.sampler.JavaSamplerClient 接口,因为 JMeter 端没有 Maven Artifact 也没有提供二进制文件。 只是一个 JMeter Maven 插件(这不是我要找的)。 我可以在本地 Maven 存储库中安装所需的二进制文件,但我只是不知道它们在哪里可用。

想知道是否有人在使用“Java Request Sampler”?

PS也许我应该尝试 JUnit Sampler

I'd like to test a Java Service Call.
My first attempt was to use the "Java Request Sampler"
The docu says

This sampler lets you control a java
class that implements the
JavaSamplerClient interface.

I'm not sure how to get the org.apache.jmeter.protocol.java.sampler.JavaSamplerClient Interface as there is no Maven Artifact nor provided binaries on the JMeter side.
Just a JMeter Maven Plugin (which is not what I'm looking for).
I could install the needed binaries in my local Maven Repository, I Just don't know where they are available.

Wondering if anybody at all is using the "Java Request Sampler" ?

P.S. Maybe I should try the JUnit Sampler

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白云不回头 2024-08-31 00:24:40

我们正在使用 ANT 进行构建并使用 JAR,这些 JAR 位于 来自 JMeter 的二进制文件\lib\ext\ 下。用于 Java 请求采样器的 AbstractJavaSamplerClient 位于文件 \lib\ext\ApacheJMeter_java.jar 中。为了使用这个抽象类,您还必须导入 JAR 文件 \lib\ext\ApacheJMeter_core.jar,它(例如)包含类 SampleResult

构建 Java 类后,我们将生成的 JAR 文件也放入文件夹 \lib\ext\ 中。
如果我们的类使用任何依赖项(第三部分 JAR),我们将其放在“\lib”文件夹中。
之后,您可以启动 JMeter,并且可以在 Java 请求采样器中选择您的 Java 类。

以下是此类 Java 请求采样器的示例:

public class JavaRequestSamplerDemo extends AbstractJavaSamplerClient {

  @Override
  public SampleResult runTest(JavaSamplerContext ctx) {
    JMeterVariables vars = JMeterContextService.getContext().getVariables();
    vars.put("demo", "demoVariableContent");

    SampleResult sampleResult = new SampleResult();
    sampleResult.setSuccessful(true);
    sampleResult.setResponseCodeOK();
    sampleResult.setResponseMessageOK();
    return sampleResult;
  }  
}

We are building with ANT and are using the JARs, which are located in the binary file from JMeter under \lib\ext\. The AbstractJavaSamplerClient which is used for the Java Request Sampler is located in the file \lib\ext\ApacheJMeter_java.jar. For working with this abstract class, you also have to import the JAR file \lib\ext\ApacheJMeter_core.jar, which is (for example) holding the class SampleResult.

After building our Java class we put the resulting JAR file also in the folder \lib\ext\.
If our class uses any dependency (3rd part JAR), we put it in '\lib' folder.
After that, you can start JMeter and you're able to select your Java class in a Java Request Sampler.

Here is an example of such a Java Request Sampler:

public class JavaRequestSamplerDemo extends AbstractJavaSamplerClient {

  @Override
  public SampleResult runTest(JavaSamplerContext ctx) {
    JMeterVariables vars = JMeterContextService.getContext().getVariables();
    vars.put("demo", "demoVariableContent");

    SampleResult sampleResult = new SampleResult();
    sampleResult.setSuccessful(true);
    sampleResult.setResponseCodeOK();
    sampleResult.setResponseMessageOK();
    return sampleResult;
  }  
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文