gpt4 book ai didi

java - 如何在Java代码中创建jdbc请求的Jmeter测试

转载 作者:行者123 更新时间:2023-11-30 05:20:36 25 4
gpt4 key购买 nike

我想在Java程序中构建一个新的jdbc测试,这是我的代码:

JMeterUtils.loadJMeterProperties(jmeterPropertiesFile.getPath());

StandardJMeterEngine standardJMeterEngine = new StandardJMeterEngine();
HashTree testPlanTree = new HashTree();

TestPlan testPlan = new TestPlan("Create JMeter Script From Java Code");
testPlan.setTestPlanClasspath("C:\\Users\\jinzha\\.gradle\\caches\\modules-2\\files-2.1\\mysql\\mysql-connector-java\\5.1.20\\66a2a9c6561dc2c3c6f0b924681c305be78f3377\\mysql-connector-java-5.1.20.jar");

//jdbc configuration sample
DataSourceElement dataSourceElement = createDatasource();
//jdbc sample
JDBCSampler sampler = createJdbcSampler();

LoopController loopController = createLoopController();
loopController.addTestElement(sampler);

ThreadGroup threadGroup = createThreadGroup();
threadGroup.setSamplerController(loopController);

testPlanTree.add("testPlan", testPlan);
testPlanTree.add("loopController", loopController);
testPlanTree.add("threadGroup", threadGroup);
testPlanTree.add("JDBCDataSource", dataSourceElement);
testPlanTree.add("JDBCSampler",sampler);

谁能告诉我正确的方法吗?

最佳答案

最小工作配置类似于:

  1. JDBC Connection Configuration :

    DataSourceElement jdbcDataSource = new DataSourceElement();
    jdbcDataSource.setName("JDBC Connection Configuration");
    jdbcDataSource.setProperty("dataSource", "foo");
    jdbcDataSource.setProperty("dbUrl", "jdbc:mysql://192.168.99.100:3306/mysql");
    jdbcDataSource.setProperty("driver", "com.mysql.jdbc.Driver");
    jdbcDataSource.setProperty("username", "root");
    jdbcDataSource.setProperty("password", "secret");
    jdbcDataSource.setProperty("poolMax", "0");
    jdbcDataSource.setProperty("connectionAge", "5000");
    jdbcDataSource.setProperty("timeout", "10000");
    jdbcDataSource.setProperty("trimInterval", "60000");
    jdbcDataSource.setProperty(TestElement.TEST_CLASS, DataSourceElement.class.getName());
    jdbcDataSource.setProperty(TestElement.GUI_CLASS, TestBeanGUI.class.getName());
  2. JDBC Request采样器:

    JDBCSampler jdbcSampler = new JDBCSampler();
    jdbcSampler.setName("JDBC Request");
    jdbcSampler.setProperty("dataSource", "foo");
    jdbcSampler.setProperty("queryType", "Select Statement");
    jdbcSampler.setProperty("query", "select * from help_topic limit 5;");
    jdbcSampler.setProperty(TestElement.TEST_CLASS, JDBCSampler.class.getName());
    jdbcSampler.setProperty(TestElement.GUI_CLASS, TestBeanGUI.class.getName());

将所有内容放在一起:

package com.blazemeter;

import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.config.gui.ArgumentsPanel;
import org.apache.jmeter.control.LoopController;
import org.apache.jmeter.control.gui.LoopControlPanel;
import org.apache.jmeter.control.gui.TestPlanGui;
import org.apache.jmeter.engine.StandardJMeterEngine;
import org.apache.jmeter.protocol.jdbc.config.DataSourceElement;
import org.apache.jmeter.protocol.jdbc.sampler.JDBCSampler;
import org.apache.jmeter.reporters.ResultCollector;
import org.apache.jmeter.reporters.Summariser;
import org.apache.jmeter.save.SaveService;
import org.apache.jmeter.testbeans.gui.TestBeanGUI;
import org.apache.jmeter.testelement.TestElement;
import org.apache.jmeter.testelement.TestPlan;
import org.apache.jmeter.threads.ThreadGroup;
import org.apache.jmeter.threads.gui.ThreadGroupGui;
import org.apache.jmeter.util.JMeterUtils;
import org.apache.jorphan.collections.HashTree;

import java.io.FileOutputStream;

public class Main {

public static void main(String[] args) throws Exception {

final String jmeterHome = "c:/apps/jmeter";

StandardJMeterEngine jmeter = new StandardJMeterEngine();
JMeterUtils.setJMeterHome(jmeterHome);
JMeterUtils.loadJMeterProperties(jmeterHome + System.getProperty("file.separator") + "bin"
+ System.getProperty("file.separator") + "jmeter.properties");

HashTree testPlanTree = new HashTree();

DataSourceElement jdbcDataSource = new DataSourceElement();
jdbcDataSource.setName("JDBC Connection Configuration");
jdbcDataSource.setProperty("dataSource", "foo");
jdbcDataSource.setProperty("dbUrl", "jdbc:mysql://192.168.99.100:3306/mysql");
jdbcDataSource.setProperty("driver", "com.mysql.jdbc.Driver");
jdbcDataSource.setProperty("username", "root");
jdbcDataSource.setProperty("password", "secret");
jdbcDataSource.setProperty("poolMax", "0");
jdbcDataSource.setProperty("connectionAge", "5000");
jdbcDataSource.setProperty("timeout", "10000");
jdbcDataSource.setProperty("trimInterval", "60000");
jdbcDataSource.setProperty(TestElement.TEST_CLASS, DataSourceElement.class.getName());
jdbcDataSource.setProperty(TestElement.GUI_CLASS, TestBeanGUI.class.getName());

JDBCSampler jdbcSampler = new JDBCSampler();
jdbcSampler.setName("JDBC Request");
jdbcSampler.setProperty("dataSource", "foo");
jdbcSampler.setProperty("queryType", "Select Statement");
jdbcSampler.setProperty("query", "select * from help_topic limit 5;");
jdbcSampler.setProperty(TestElement.TEST_CLASS, JDBCSampler.class.getName());
jdbcSampler.setProperty(TestElement.GUI_CLASS, TestBeanGUI.class.getName());

LoopController loopController = new LoopController();
loopController.setLoops(1);
loopController.setFirst(true);
loopController.setProperty(TestElement.TEST_CLASS, LoopController.class.getName());
loopController.setProperty(TestElement.GUI_CLASS, LoopControlPanel.class.getName());
loopController.initialize();

ThreadGroup threadGroup = new ThreadGroup();
threadGroup.setName("Example Thread Group");
threadGroup.setNumThreads(1);
threadGroup.setRampUp(1);
threadGroup.setSamplerController(loopController);
threadGroup.setProperty(TestElement.TEST_CLASS, ThreadGroup.class.getName());
threadGroup.setProperty(TestElement.GUI_CLASS, ThreadGroupGui.class.getName());

TestPlan testPlan = new TestPlan("JMeter JDBC Test Plan");
testPlan.setProperty(TestElement.TEST_CLASS, TestPlan.class.getName());
testPlan.setProperty(TestElement.GUI_CLASS, TestPlanGui.class.getName());
testPlan.setUserDefinedVariables((Arguments) new ArgumentsPanel().createTestElement());

testPlanTree.add(testPlan);
HashTree threadGroupHashTree = testPlanTree.add(testPlan, threadGroup);
threadGroupHashTree.add(jdbcDataSource);
threadGroupHashTree.add(jdbcSampler);

SaveService.saveTree(testPlanTree, new FileOutputStream(jmeterHome + System.getProperty("file.separator")
+ "bin" + System.getProperty("file.separator") + "jdbc.jmx"));


Summariser summer = null;
String summariserName = JMeterUtils.getPropDefault("summariser.name", "summary");
if (summariserName.length() > 0) {
summer = new Summariser(summariserName);
}

String logFile = jmeterHome + System.getProperty("file.separator") +
"bin" + System.getProperty("file.separator") + "jdbc.jtl";
ResultCollector logger = new ResultCollector(summer);
logger.setFilename(logFile);
testPlanTree.add(testPlanTree.getArray()[0], logger);

jmeter.configure(testPlanTree);
jmeter.run();

System.exit(0);


}
}

执行后,上述测试计划将在 JMeter 安装的“bin”文件夹中生成 jdbc.jmx 测试计划,并在同一位置生成 jdbc.jtl 结果文件(如果一切顺利。

引用文献:

关于java - 如何在Java代码中创建jdbc请求的Jmeter测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59642867/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com