2016/10/17 Java Request 的测试方法

Jmeter 是一款简单的性能测试工具,以前都是用来测试 API 接口,没试过测试 Java 程序,这是一个例子。

Jmeter 使用,可参考 Jmeter 使用入门

java request 测试

使用 jmeter 测试 java 程序,需要结合 jemter_java 编写测试代码。

  1. 创建 maven 工程
    pmx.xml
    ```xml
    <?xml version=”1.0” encoding=”UTF-8”?>
    <project xmlns=”http://maven.apache.org/POM/4.0.0
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    
    4.0.0
    com.rolex.jmeter
    java-request-sample
    1.0-SNAPSHOT
     <plugins>
         <plugin>
             <groupId>org.apache.maven.plugins</groupId>
             <artifactId>maven-compiler-plugin</artifactId>
             <version>2.3.2</version>
             <configuration>
                 <source>1.6</source>
                 <target>1.6</target>
                 <encoding>UTF-8</encoding>
             </configuration>
         </plugin>
         <plugin>
             <artifactId>maven-assembly-plugin</artifactId>
             <configuration>
                 <descriptorRefs>
                     <descriptorRef>jar-with-dependencies</descriptorRef>
                 </descriptorRefs>
             </configuration>
             <executions>
                 <execution>
                     <id>make-assembly</id>
                     <phase>package</phase>
                     <goals>
                         <goal>attached</goal>
                     </goals>
                 </execution>
             </executions>
         </plugin>
     </plugins>
     <resources>
         <resource>
             <targetPath>libs/</targetPath>
             <directory>libs/</directory>
             <includes>
                 <include>**/certNoToMd5.jar</include>
                 <include>**/Lite-20111106.jar</include>
                 <include>**/quantum-auth-1.0-SNAPSHOT.jar</include>
             </includes>
         </resource>
     </resources>
    


    com.rolex.jmeter.test
    jmeter-test
    1.0.0
    system
    ${project.basedir}/libs/certNoToMd5.jar



    org.apache.jmeter
    ApacheJMeter_core
    3.0



    org.apache.jmeter
    ApacheJMeter_java
    3.0



```java
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;

import com.seeks.support.lxcernointerface.LxcernoClientUtils;

public class JavaRequest extends AbstractJavaSamplerClient {

    SampleResult result;
    String param;

    @Override
    public void setupTest(JavaSamplerContext context) {
        result = new SampleResult();
        super.setupTest(context);
    }

    @Override
    public Arguments getDefaultParameters() {
        Arguments params = new Arguments();
        params.addArgument("arg1", "0");
        return params;

    }

    @Override
    public SampleResult runTest(JavaSamplerContext arg0) {

        boolean success = true;
        result.sampleStart();
        param = arg0.getParameter("arg1");
        try {
            // do some test
        } catch (Exception e) {
            e.printStackTrace();
            success = false;
        } finally {
            result.sampleEnd();
            result.setSuccessful(success);
        }
        return result;

    }

    @Override
    public void teardownTest(JavaSamplerContext context) {
        super.teardownTest(context);
    }

}