使用 JMeter 测试 Java 类 [英] Testing Java Classes with JMeter

查看:24
本文介绍了使用 JMeter 测试 Java 类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

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

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.

想知道是否有人在使用Java 请求采样器"?

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

附言也许我应该试试 JUnit Sampler

P.S. Maybe I should try the JUnit Sampler

推荐答案

我们正在使用 ANT 构建并使用 JAR,它们位于 来自 JMeter 的二进制文件,位于 libext 下.用于 Java 请求采样器的 AbstractJavaSamplerClient 位于文件 libextApacheJMeter_java.jar 中.为了使用这个抽象类,您还必须导入 JAR 文件 libextApacheJMeter_core.jar,它(例如)保存类 SampleResult.

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

在构建我们的 Java 类之后,我们将生成的 JAR 文件也放在文件夹 libext 中.如果我们的类使用任何依赖项(第 3 部分 JAR),我们将它放在 'lib' 文件夹中.之后,您可以启动 JMeter,并且您可以在 Java 请求采样器中选择您的 Java 类.

After building our Java class we put the resulting JAR file also in the folder libext. 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.

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

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;
  }  
}

这篇关于使用 JMeter 测试 Java 类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆