Reputation: 25
I want to build a new jdbc test in Java program, this is my code:
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);
Could anyone show me the correct way?
Upvotes: 2
Views: 965
Reputation: 11
I use this example, it also happened Something bad happened when loading bean info for bean class org.apache.jmeter.protocol.jdbc.sampler.JDBCSampler java.lang.NullPointerException
, and I find the problem by debugging the source code.
at the beginning of this function, we need to add a java.util.Locale instance
:
JMeterUtils.setLocale(Locale.getDefault());
Upvotes: 1
Reputation: 168002
Minimal working configuration would be something like:
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());
JDBC Request Sampler:
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());
Putting everything together:
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);
}
}
Upon execution the above test plan will generate jdbc.jmx
test plan in "bin" folder of your JMeter installation and jdbc.jtl
result file at the same location if everything goes well.
References:
Upvotes: 1