Java生成JMeter配置文件.jmx以及调用示例

 

1 Maven 配置jar包

            org.apache.jmeter

            ApacheJMeter_http

            ${jmeter.version}

            

                

                    org.apache.logging.log4j

                    log4j-slf4j-impl

                

            

        

        

            org.apache.jmeter

            ApacheJMeter_functions

            ${jmeter.version}

            

                

                    org.apache.logging.log4j

                    log4j-slf4j-impl

                

            

        

        

            org.apache.jmeter

            ApacheJMeter_jdbc

            ${jmeter.version}

            

                

                    org.apache.logging.log4j

                    log4j-slf4j-impl

                

            

        

        

            org.apache.jmeter

            ApacheJMeter_java

            ${jmeter.version}

            

                

                    org.apache.logging.log4j

                    log4j-slf4j-impl

                

            

        

 

2 Java生成JMeter的jmx配置文件

2.1加载配置文件

使用静态块加载JMeter配置文件

 

static{

        {

            JMeterUtils.setJMeterHome(new File("G:\\配置软件\\压测工具\\JMeter\\apache-jmeter-5.1.1").getAbsolutePath());

            JMeterUtils.loadJMeterProperties(new File("G:\\配置软件\\压测工具\\JMeter\\apache-jmeter-5.1.1\\bin/jmeter.properties").getAbsolutePath());

            JMeterUtils.setProperty("saveservice_properties", "G:\\配置软件\\压测工具\\JMeter\\apache-jmeter-5.1.1\\bin/saveservice.properties");

            JMeterUtils.setProperty("user_properties", "G:\\配置软件\\压测工具\\JMeter\\apache-jmeter-5.1.1\\bin/user.properties");

            JMeterUtils.setProperty("upgrade_properties", "G:\\配置软件\\压测工具\\JMeter\\apache-jmeter-5.1.1\\bin/upgrade.properties");

            JMeterUtils.setProperty("system_properties", "G:\\配置软件\\压测工具\\JMeter\\apache-jmeter-5.1.1\\bin/system.properties");

           JMeterUtils.setProperty("proxy.cert.directory", new File("").getAbsolutePath());

            JMeterUtils.setLocale(Locale.SIMPLIFIED_CHINESE);

            JMeterUtils.initLocale();

 

        }

    }

 

2.2 生成jmx配置文件

2.2.1 新建TestPlan

//TestPlan

        TestPlan testPlan = new TestPlan("Test Plan");

        testPlan.setFunctionalMode(false);

        testPlan.setSerialized(false);

        testPlan.setTearDownOnShutdown(true);

        testPlan.setProperty(TestElement.TEST_CLASS, TestPlan.class.getName());

        testPlan.setProperty(TestElement.GUI_CLASS,"TestPlanGui");

        testPlan.setProperty(new BooleanProperty(TestElement.ENABLED, true));

        testPlan.setProperty(new StringProperty("TestPlan.comments", ""));

        testPlan.setProperty(new StringProperty("TestPlan.user_define_classpath", ""));

        Arguments arguments = new Arguments();

        testPlan.setProperty(new TestElementProperty("TestPlan.user_defined_variables",arguments));

 

对应JMeter_GUI:

Java生成JMeter配置文件.jmx以及调用示例_第1张图片

 

2.2.2 新建ThreadGroup

对应设置请求次数、线程数等

        ThreadGroup threadGroup = new ThreadGroup();

        threadGroup.setNumThreads(1);

        threadGroup.setRampUp(1);

        threadGroup.setDelay(0);

        threadGroup.setDuration(0);

        threadGroup.setProperty(new StringProperty(ThreadGroup.ON_SAMPLE_ERROR, "continue"));

        threadGroup.setScheduler(false);

        threadGroup.setName("Group1");

        threadGroup.setProperty(TestElement.TEST_CLASS, ThreadGroup.class.getName());

        threadGroup.setProperty(TestElement.GUI_CLASS,"ThreadGroup");

        threadGroup.setProperty(new BooleanProperty(TestElement.ENABLED, true));

 

对应JMeter_GUI:

Java生成JMeter配置文件.jmx以及调用示例_第2张图片

2.2.3 新建HTTP Request

主要设置请求IP、请求端口、请求类型、请求参数

HTTPSamplerProxy httpSamplerProxy = new HTTPSamplerProxy();

        Arguments HTTPsamplerArguments = new Arguments();

        HTTPArgument httpArgument = new HTTPArgument();

        httpArgument.setProperty(new BooleanProperty("HTTPArgument.always_encode",false));

        httpArgument.setProperty(new StringProperty("Argument.value", request));

        httpArgument.setProperty(new StringProperty("Argument.metadata","="));

        ArrayList list1 = new ArrayList<>();

        list1.add(new TestElementProperty("",httpArgument));

        HTTPsamplerArguments.setProperty(new CollectionProperty("Arguments.arguments",list1));

        httpSamplerProxy.setProperty(new TestElementProperty("HTTPsampler.Arguments",HTTPsamplerArguments));

        httpSamplerProxy.setProperty(new StringProperty("HTTPSampler.domain", url));

        httpSamplerProxy.setProperty(new StringProperty("HTTPSampler.port", port));

        httpSamplerProxy.setProperty(new StringProperty("HTTPSampler.protocol", "http"));

        httpSamplerProxy.setProperty(new StringProperty("HTTPSampler.path", api));

        httpSamplerProxy.setProperty(new StringProperty("HTTPSampler.method", "POST"));

        httpSamplerProxy.setProperty(new StringProperty("HTTPSampler.contentEncoding", ""));

        httpSamplerProxy.setProperty(new BooleanProperty("HTTPSampler.follow_redirects", true));

        httpSamplerProxy.setProperty(new BooleanProperty("HTTPSampler.postBodyRaw", true));

        httpSamplerProxy.setProperty(new BooleanProperty("HTTPSampler.auto_redirects", false));

        httpSamplerProxy.setProperty(new BooleanProperty("HTTPSampler.use_keepalive", true));

        httpSamplerProxy.setProperty(new BooleanProperty("HTTPSampler.DO_MULTIPART_POST", false));

        httpSamplerProxy.setProperty(new StringProperty("TestElement.gui_class", "org.apache.jmeter.protocol.http.control.gui.HttpTestSampleGui"));

        httpSamplerProxy.setProperty(new StringProperty("TestElement.test_class", "org.apache.jmeter.protocol.http.sampler.HTTPSamplerProxy"));

        httpSamplerProxy.setProperty(new StringProperty("TestElement.name", "HTTP Request"));

        httpSamplerProxy.setProperty(new StringProperty("TestElement.enabled", "true"));

        httpSamplerProxy.setProperty(new BooleanProperty("HTTPSampler.postBodyRaw", true));

        httpSamplerProxy.setProperty(new StringProperty("HTTPSampler.embedded_url_re", ""));

        httpSamplerProxy.setProperty(new StringProperty("HTTPSampler.connect_timeout", ""));

        httpSamplerProxy.setProperty(new StringProperty("HTTPSampler.response_timeout", ""));

 

对应JMeter_GUI:

Java生成JMeter配置文件.jmx以及调用示例_第3张图片

 

2.2.4新建请求header

        Header header = new Header();

        header.setProperty(new StringProperty("Header.name","Content-Type"));

        header.setProperty(new StringProperty("Header.value","application/json"));

        TestElementProperty HeaderElement = new TestElementProperty("",header);

        list2.add(HeaderElement);

        headerManager.setProperty(new CollectionProperty("HeaderManager.headers",list2));

        headerManager.setProperty(new StringProperty("TestElement.test_class","org.apache.jmeter.protocol.http.control.HeaderManager"));

        headerManager.setProperty(new StringProperty("TestElement.name","HTTP Header Manager"));

        headerManager.setProperty(new StringProperty("TestElement.enabled","true"));

        headerManager.setProperty(new StringProperty("TestElement.gui_class","org.apache.jmeter.protocol.http.gui.HeaderPanel"));

 

对应JMeter_GUI:

Java生成JMeter配置文件.jmx以及调用示例_第4张图片

 

2.2.5 生成jmx文件

        ListedHashTree hashTreeResultCollectorAndHeaderManager = new ListedHashTree();

        hashTreeResultCollectorAndHeaderManager.add(resultCollector);

        hashTreeResultCollectorAndHeaderManager.add(headerManager);

 

        ListedHashTree hashTreeHTTPSamplerProxy = new ListedHashTree();

        hashTreeHTTPSamplerProxy.add(httpSamplerProxy,hashTreeResultCollectorAndHeaderManager);

 

        ListedHashTree hashTreeThreadGroup = new ListedHashTree();

        hashTreeThreadGroup.add(threadGroup,hashTreeHTTPSamplerProxy);

 

 

        ListedHashTree hashTreeTestPlan = new ListedHashTree();

        hashTreeTestPlan.add(testPlan,hashTreeThreadGroup);

 

        SaveService.saveTree(hashTreeTestPlan, new FileOutputStream("F:\\test.jmx"));

3 Java执行jmx文件

两种方式实现

3.1 调用cmd

    @Test

    public  void testCmd() throws  IOException {

        String command = "G:\\配置软件\\压测工具\\JMeter\\apache-jmeter-5.1.1\\bin\\jmeter -n -t F:\\test.jmx  -l F:\\test.jtl";

        Runtime.getRuntime().exec("cmd.exe /C start " + command);

    }

 

执行结果:

  1. 唤起windows的cmd。(linux同理也可以使用命令行执行JMeter)

Java生成JMeter配置文件.jmx以及调用示例_第5张图片

 

     2. 执行jmx文件,并调用

Java生成JMeter配置文件.jmx以及调用示例_第6张图片

 

3.2 使用JMeter_Java调用

 

    @Test

    public  void test() throws  IOException {

        File file = new File("F:\\test.jmx");

        HashTree hashTree = SaveService.loadTree(file);

        JMeterEngine engine = new StandardJMeterEngine();

        engine.configure(hashTree);

        ((StandardJMeterEngine) engine).run();

    }

 

执行结果:

 

Java生成JMeter配置文件.jmx以及调用示例_第7张图片

你可能感兴趣的:(测试工具)