编程语言
首页 > 编程语言> > Java写jmeter的代码模板

Java写jmeter的代码模板

作者:互联网

1.jmeter 5.3版本及以上需要多添加一个oro-2.0.8.jar  ,然后通用 ApacheJMeter_core.jar ApacheJMeter_java.jar

2.上代码:

public class InsertTest implements JavaSamplerClient{
    public static void main(String[] args) {
        InsertTest insertTest = new InsertTest();
//        Arguments arguments = new Arguments();
//        JavaSamplerContext samplerContext = new JavaSamplerContext(arguments);
        Arguments arguments = insertTest.getDefaultParameters();
        JavaSamplerContext samplerContext = new JavaSamplerContext(arguments);  //增加一个菜篮子容器
        insertTest.setupTest(samplerContext);
        insertTest.runTest(samplerContext);
        insertTest.teardownTest(samplerContext);
    }
    /*
    声明全局变量
     */
    PreparedStatement ps;
    Connection conn;
    String sql = "insert into user (username,password,age)value (?,?,?)";
    @Override
    public void setupTest(JavaSamplerContext javaSamplerContext) {
        //1. 注册驱动,告诉JDBC链接数据库的类型,传入MySQL驱动类型
        try {
            Class.forName("com.mysql.jdbc.Driver");
            String dburl = javaSamplerContext.getParameter("dburl");
            String dbusername = javaSamplerContext.getParameter("dbusername");
            String dbpassword = javaSamplerContext.getParameter("dbpassword");
            //2.建立链接 :IP 端口 用户名 密码数据库名
            conn= DriverManager.getConnection(dburl, dbusername, dbpassword);
            //3. 对SQL语句进行预编译
            ps = conn.prepareStatement(sql);
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }
    }
    @Override
    public SampleResult runTest(JavaSamplerContext javaSamplerContext) {
        SampleResult sampleResult = new SampleResult(); //结果对象
        sampleResult.setSampleLabel("insert"); //给请求 取名
        sampleResult.sampleStart();  //请求开始时间
        //4 .对sql语句问好进行实参
        try {
            String username = javaSamplerContext.getParameter("username");
            String password = javaSamplerContext.getParameter("password");
            int age = javaSamplerContext.getIntParameter("age");

            ps.setString(1,"username");
            ps.setString(2,"password");
            ps.setInt(3,age);
            //5. 执行sql
            int row = ps.executeUpdate();
            System.out.println("执行结果的行数为:"+ row);
            if(row == 1){
                sampleResult.setSuccessful(true);
            }else {
                sampleResult.setSuccessful(false);
            }
            String res = "插入的元素" +row;
            sampleResult.setResponseData(res,"utf-8");  //给返回结果
        } catch (SQLException e) {
            e.printStackTrace();
        }
        sampleResult.sampleEnd();   //请求结束时间
        return sampleResult;   //返回请求结果
    }
    @Override
    public void teardownTest(JavaSamplerContext javaSamplerContext) {
        //6. 关闭数据库链接
        try {
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    @Override
    public Arguments getDefaultParameters() {
        // 初始化Java sampler 参数
        Arguments arg = new Arguments();
        arg.addArgument("dburl","http://localhost:8080/pinter");
        arg.addArgument("dbusername","root");
        arg.addArgument("dbpassword","123456");
        arg.addArgument("username","chevron");
        arg.addArgument("password","123456");
        arg.addArgument("age","22");
        return arg;
    }
}

 

标签:addArgument,Java,String,JavaSamplerContext,javaSamplerContext,arg,jmeter,模板,samp
来源: https://www.cnblogs.com/chevron123/p/15271769.html