Jmeter在java代码中创建JDBC连接配置实例

Jmeter在java代码中创建JDBC连接配置实例,java,jdbc,jmeter,Java,Jdbc,Jmeter,我想问一下,是否有一种方法可以通过代码而不是GUI为JMeter创建JDBC连接配置实例 我有以下线程组: SetupThreadGroup threadGroup = new SetupThreadGroup(); threadGroup.setNumThreads(jMeterParam.getNumOfConnections()); threadGroup.setRampUp(0); threadGroup.setDuration(7200)

我想问一下,是否有一种方法可以通过代码而不是GUI为JMeter创建JDBC连接配置实例

我有以下线程组:

SetupThreadGroup threadGroup = new SetupThreadGroup();
        threadGroup.setNumThreads(jMeterParam.getNumOfConnections());
        threadGroup.setRampUp(0);
        threadGroup.setDuration(7200);



    JDBCSampler sampler = new JDBCSampler();
    sampler.setQuery("select top 1 * from Production.ProductPhoto;");
    sampler.setVariableNames("firstPrfile");
    sampler.setQueryType("Select Statement");
    ConstantTimer timer = new ConstantTimer();
    timer.setDelay("300");
我需要创建JDBCConnectionConfiguration实例,以便在JDBC连接配置JMeter GUI中设置JMeter变量名、最大连接数、池超时和所有可用参数


我需要编写java代码来配置测试计划并通过JMeter运行它。 我尝试了您的建议,创建JDBC连接,如下所示:

SetupThreadGroup threadGroup = new SetupThreadGroup();
        threadGroup.setNumThreads(jMeterParam.getNumOfConnections());
        threadGroup.setRampUp(0);
        threadGroup.setDuration(7200);
        DefaultPoolController defaultPoolController = new DefaultPoolController();

        JdbcConnectionFactory jdbcFactory = new JdbcConnectionFactory("jdbc:sqlserver://10.10.10.171:1401;databaseName=AdventureWorks","sa","1q@W3e4r",true,"True","com.microsoft.sqlserver.jdbc.SQLServerDriver");
        JdbcConnectionPool jdbcConnPool= new JdbcConnectionPool(jdbcFactory,defaultPoolController,0,10,true);


        JDBCSampler sampler = new JDBCSampler();
        sampler.setQuery("select top 1 * from Production.ProductPhoto;");
        sampler.setVariableNames("firstPrfile");
        sampler.setQueryType("Select Statement");
        ConstantTimer timer = new ConstantTimer();
        timer.setDelay("300");


        sampler.addTestElement(timer);

        // Test plan
        TestPlan testPlan = new TestPlan("MY TEST PLAN");
        hashTree.add("testPlan", testPlan);
        hashTree.add("threadGroup", threadGroup);
        hashTree.add("JDBC Connection Configuration", jdbcConnPool);
        hashTree.add("sampler", sampler);

        jm.configure(hashTree);

        jm.run();
运行警告消息时显示: 1) “加载bean类org.apache.jmeter.protocol.jdbc.sampler.jdbccsampler的bean信息时发生错误” 2) 未找到“…ApacheJMeter.jar/commons-io-2.2.jar/etc…”

你知道怎么解决吗

非常感谢,
Khilo

我个人通过JMeter GUI创建测试,不直接使用JMeter API。但考虑到你的问题,我认为你可以创建JDBC连接并执行所需的查询,而无需实例化JDBC采样器。或者你可以创建BSF采样器并在其主体中设置连接和查询。我使用了第二种方法(尽管我使用了GUI):创建BSF采样器,连接到DB并执行查询,然后使用Groovy操作数据