Jmeter-Java secondary development

maven dependencies

<dependencies>
        <dependency>
            <groupId>org.apache.jmeter</groupId>
            <artifactId>ApacheJMeter_core</artifactId>
            <version>${jmeter-version}</version>
            <!--在本地测试时请注释掉scope -->
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.apache.jmeter</groupId>
            <artifactId>ApacheJMeter_java</artifactId>
            <version>${jmeter-version}</version>
            <!-- 在本地测试时请注释掉scope -->
            <scope>provided</scope>
        </dependency>
</dependencies>

<build>
        <plugins>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.5.3</version>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                        <configuration>
                            <descriptorRefs>
                                <descriptorRef>jar-with-dependencies</descriptorRef>
                            </descriptorRefs>
                            <descriptors>
                                <!--<descriptor>src/assembly/assembly.xml</descriptor>-->
                            </descriptors>
                        </configuration>
                    </execution>
                </executions>
                <configuration>
                    <!-- 最终打包名称 -->
                    <finalName>as_velocity_10_sampler</finalName>
                    <appendAssemblyId>false</appendAssemblyId>
                </configuration>
            </plugin>
        </plugins>
    </build>

PS1: When developing, please comment out the scope under the reference package ApacheJMeter_core and ApacheJMeter_java
PS2: Use the maven-assembly-plugin plugin to package the three-party packages that the project depends on. If you don't use plugins, you must copy all dependent packages to jmeter's lib/ext

Java development

The sampler class must inherit AbstractJavaSamplerClient

public class SimpleSampler extends AbstractJavaSamplerClient {

    // 用于设置传入的参数,可以设置多个,已设置的参数会显示到Jmeter参数列表中
    // Arguments类表示一组参数对象
    public Arguments getDefaultParameters() {
        Arguments params = new Arguments();
        params.addArgument(HOST, "10.57.32.196");
        params.addArgument(PORT, "3000");

        return params;
    }


    // 初始化方法,用于初始化性能测试时的每个线程,实际运行时每个线程仅执行一次
    //
    // JavaSamplerContext类用于向JavaSamplerClient实现提供上下文信息
    // 这当前由在GUI中指定的初始化参数组成,其他数据将来可以通过JavaSamplerContext访问
    public void setupTest(JavaSamplerContext context) {

    }

    // 性能测试时的线程运行体,即测试执行的循环体,根据线程数和循环次数的不同可执行多次
    public SampleResult runTest(JavaSamplerContext context) {

        return sampleResult;
    }

    // 测试结束方法,用于结束性能测试中的每个线程,实际运行时,每个线程仅执行一次,在测试方法运行结束后执行
    public void teardownTest(JavaSamplerContext context) {
        super.teardownTest(context);

    }

}

local test

Say it again: 注释掉refer to the scope under the package ApacheJMeter_core, ApacheJMeter_java

public class Demo {

    public static void main(String[] args) throws NoSuchMethodException, UnsupportedEncodingException {


        SimpleSampler jmeter = new SimpleSampler();

        Arguments arguments = jmeter.getDefaultParameters();
        jmeter.setupTest(new JavaSamplerContext(arguments));
        SampleResult sampleResult = jmeter.runTest(new JavaSamplerContext(arguments));
        jmeter.teardownTest(new JavaSamplerContext(arguments));

        System.out.println(new String(sampleResult.getResponseData(),"utf-8"));


    }

}

jmeter integration

Say it again: 打开注释refer to the scope under the package ApacheJMeter_core, ApacheJMeter_java

1. Throw the packaged package to jmeter/lib/ext

2. Restart jmeter

3. Add Sampler for java request
image_1c9jbdasanb96cji6t1gvu1ct9.png-136.8kB

4. Choose your own class

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325768165&siteId=291194637