使用jmeter 提供的API实现性能测试(一)

import org.apache.jmeter.JMeter;
import org.apache.jmeter.engine.JMeterEngineException;
import org.apache.jmeter.engine.StandardJMeterEngine;
import org.apache.jmeter.reporters.ResultCollector;
import org.apache.jmeter.reporters.Summariser;
import org.apache.jmeter.save.SaveService;
import org.apache.jmeter.services.FileServer;
import org.apache.jmeter.util.JMeterUtils;
import org.apache.jorphan.collections.HashTree;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;

public class Demo1 {

    public static void main(String[] args) throws IllegalAccessException, IOException, JMeterEngineException, NoSuchFieldException {
        StandardJMeterEngine standardJMeterEngine=new StandardJMeterEngine();
        System.setProperty(JMeter.JMETER_NON_GUI, "true");
        JMeterUtils.loadJMeterProperties("D:\\apache-jmeter-5.1\\bin\\jmeter.properties");
        JMeterUtils.loadJMeterProperties("D:\\apache-jmeter-5.1\\bin\\system.properties");
        JMeterUtils.loadJMeterProperties("D:\\apache-jmeter-5.1\\bin\\user.properties");
        JMeterUtils.loadJMeterProperties("D:\\apache-jmeter-5.1\\bin\\saveservice.properties");
        JMeterUtils.setJMeterHome("D:\\apache-jmeter-5.1");
        JMeterUtils.initLocale();
        JMeterUtils.initLogging();
        SaveService.loadProperties();
        File file=new File("G:\\DownLoad\\script\\search.jmx");
        FileServer.getFileServer().setBase(file);
        FileServer.getFileServer().setScriptName(file.getName());
        Field baseField = FileServer.getFileServer().getClass().getDeclaredField("base");
        baseField.setAccessible(true);//设置允许访问
        baseField.set(FileServer.getFileServer(), file.getAbsoluteFile().getParentFile());
        HashTree scripts=SaveService.loadTree(file);
        Summariser summariser = new Summariser(JMeterUtils.getPropDefault("summariser.name", "summary"));
        ResultCollector logger = new ResultCollector(summariser);
        logger.setFilename("G:\\DownLoad\\script\\baidu.jtl");
        scripts.add(scripts.getArray()[0],logger);
        JMeter.convertSubTree(scripts);
        standardJMeterEngine.configure(scripts);
        standardJMeterEngine.runTest();
    }
}
<?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">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>testjmeter</artifactId>
    <version>1.0-SNAPSHOT</version>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>6</source>
                    <target>6</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>

            <groupId>org.apache.jmeter</groupId>

            <artifactId>ApacheJMeter_java</artifactId>

            <version>5.1.1</version>

        </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.jmeter/ApacheJMeter_core -->

        <dependency>

            <groupId>org.apache.jmeter</groupId>

            <artifactId>ApacheJMeter_core</artifactId>

            <version>5.1.1</version>

        </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.jmeter/ApacheJMeter_http -->

        <dependency>

            <groupId>org.apache.jmeter</groupId>

            <artifactId>ApacheJMeter_http</artifactId>

            <version>5.1.1</version>

        </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.jmeter/ApacheJMeter_components -->

        <dependency>

            <groupId>org.apache.jmeter</groupId>

            <artifactId>ApacheJMeter_components</artifactId>

            <version>5.1.1</version>

        </dependency>

    </dependencies>
</project>
import org.apache.jmeter.JMeter;
import org.apache.jmeter.engine.JMeterEngineException;
import org.apache.jmeter.engine.StandardJMeterEngine;
import org.apache.jmeter.reporters.ResultCollector;
import org.apache.jmeter.reporters.Summariser;
import org.apache.jmeter.save.SaveService;
import org.apache.jmeter.services.FileServer;
import org.apache.jmeter.util.JMeterUtils;
import org.apache.jorphan.collections.HashTree;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;

public class JmeterDemo {
    public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException, IOException, JMeterEngineException {
        StandardJMeterEngine standardJMeterEngine = new StandardJMeterEngine();
        //     StandardJMeterEngine standardJMeterEngine=new StandardJMeterEngine("127.0.0.1"); //使用远程的jmeter server来进行性能测试,也就是分布式方式,IP地址可以带端口或不带端口
        JMeterUtils.loadJMeterProperties("D:\\apache-jmeter-5.1\\bin\\jmeter.properties");//加载Jmeter.properties属性文件
        JMeterUtils.loadJMeterProperties("D:\\apache-jmeter-5.1\\bin\\system.properties");//
        JMeterUtils.loadJMeterProperties("D:\\apache-jmeter-5.1\\bin\\user.properties");//
        JMeterUtils.setJMeterHome("D:\\apache-jmeter-5.1");//设置jmeter HOME目录
        JMeterUtils.initLocale();//加载用户工作目录或CLASSPATH目录下的properties文件,可选代码
        SaveService.loadProperties();//加载saveservice要使用的properties文件
        File file = new File("G:\\DownLoad\\script\\search.jmx");//得到脚本的文件对象
        FileServer.getFileServer().setBaseForScript(file);//设置jmeter脚本执行base目录
        FileServer.getFileServer().setScriptName(file.getName());//设置脚本名称
        Field baseField = FileServer.getFileServer().getClass().getDeclaredField("base");
        baseField.setAccessible(true);//设置允许访问
        baseField.set(FileServer.getFileServer(), file.getAbsoluteFile().getParentFile());
        HashTree jmxTree = SaveService.loadTree(file);//加载jmx脚本文件
        JMeter.convertSubTree(jmxTree, false);//false:删除脚本中禁用的元件、替换脚本中可替换的控制器;true:克隆整个脚本
        Summariser summer = new Summariser("summary");//生成概要结果对象
        ResultCollector logger = new ResultCollector(summer);//概要结果对象写入ResultCollector
        logger.setFilename("G:\\DownLoad\\script\\test.jtl");//设置输出的jtl文件的文件名
        jmxTree.add(jmxTree.getArray()[0], logger);//把ResultCollector对象添加到脚本中
        standardJMeterEngine.configure(jmxTree);//设置jmeter引擎需要执行的脚本
        standardJMeterEngine.runTest(); //使用该方法或run()方法效果相同
        //  standardJMeterEngine.run();
    }

}