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

 

1 Maven 配置jar包

<dependency>

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

            <artifactId>ApacheJMeter_http</artifactId>

            <version>${jmeter.version}</version>

            <exclusions>

                <exclusion>

                    <groupId>org.apache.logging.log4j</groupId>

                    <artifactId>log4j-slf4j-impl</artifactId>

                </exclusion>

            </exclusions>

        </dependency>

        <dependency>

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

            <artifactId>ApacheJMeter_functions</artifactId>

            <version>${jmeter.version}</version>

            <exclusions>

                <exclusion>

                    <groupId>org.apache.logging.log4j</groupId>

                    <artifactId>log4j-slf4j-impl</artifactId>

                </exclusion>

            </exclusions>

        </dependency>

        <dependency>

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

            <artifactId>ApacheJMeter_jdbc</artifactId>

            <version>${jmeter.version}</version>

            <exclusions>

                <exclusion>

                    <groupId>org.apache.logging.log4j</groupId>

                    <artifactId>log4j-slf4j-impl</artifactId>

                </exclusion>

            </exclusions>

        </dependency>

        <dependency>

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

            <artifactId>ApacheJMeter_java</artifactId>

            <version>${jmeter.version}</version>

            <exclusions>

                <exclusion>

                    <groupId>org.apache.logging.log4j</groupId>

                    <artifactId>log4j-slf4j-impl</artifactId>

                </exclusion>

            </exclusions>

        </dependency>

 

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:

 

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:

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<TestElementProperty> 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:

 

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:

 

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)

 

     2. 執行jmx文件,並調用

 

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();

    }

 

執行結果:

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章