jmeter的使用(四)

jmeter如何调用java程序呢,下面做简单介绍。
1、打开eclipse,新建项目,导入jmeter依赖的包ApacheJMeter_core.jar和ApacheJMeter_java.jar,这两个包在jmeter安装目录下的lib里,如需要其它jmeter的包,可在lib目录下找。

2、新建类继承AbstractJavaSamplerClient,完整代码如下:

package com.test;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;

import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingClientConnectionManager;
import org.apache.http.util.EntityUtils;
import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient;
import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext;
import org.apache.jmeter.samplers.SampleResult;

import com.sun.org.apache.bcel.internal.classfile.Code;


/** 
* @ClassName: FirstTest 
* @Description: TODO(这里用一句话描述这个类的作用) 
* @author qiaojiafei 
* @date 2016年1月5日 下午6:18:33 
*  
*/
public class FirstTest extends AbstractJavaSamplerClient {
    private String a;
    private String b;
    /** Holds the result data (shown as Response Data in the Tree display). */
    private String resultData;

    // 这个方法是用来自定义java方法入参的。
    // params.addArgument("num1","");表示入参名字叫num1,默认值为空。
    //设置可用参数及的默认值;
    public Arguments getDefaultParameters() {
        Arguments params = new Arguments();
        params.addArgument("username", "");
        params.addArgument("password", "");
        return params;
    }

    //每个线程测试前执行一次,做一些初始化工作;
    public void setupTest(JavaSamplerContext arg0) {
    }

    //开始测试,从arg0参数可以获得参数值;
    public SampleResult runTest(JavaSamplerContext arg0) {
        a = arg0.getParameter("username");
        b = arg0.getParameter("password");
        SampleResult sr = new SampleResult();
        sr.setSampleLabel( "Java请求开始");
        try {
            sr.sampleStart();// jmeter 开始统计响应时间标记
            // 通过下面的操作就可以将被测方法的响应输出到Jmeter的察看结果树中的响应数据里面了。
            resultData = FirstTest.login(a, b);
            if (resultData != null && resultData.length() > 0) {
                sr.setResponseData("请求结果是:"+resultData, null);
                sr.setDataType(SampleResult.TEXT);
            }
            // System.out.println(resultData);
            sr.setSuccessful(true);
        } catch (Throwable e) {
            sr.setSuccessful(false);
            e.printStackTrace();
        } finally {
            sr.sampleEnd();// jmeter 结束统计响应时间标记
        }
        return sr;
    }

    //测试结束时调用;
    public void teardownTest(JavaSamplerContext arg0) {

    }
    
    // main只是为了调试用,最后打jar包的时候注释掉。
    /*
      public static void main(String[] args) 
      { // TODO Auto-generated method stub
          Arguments params = new Arguments(); 
          params.addArgument("num1", "1");//设置参数,并赋予默认值1 
          params.addArgument("num2", "2");//设置参数,并赋予默认值2
          JavaSamplerContext arg0 = new JavaSamplerContext(params); 
          FirstTest test = new FirstTest(); 
          test.setupTest(arg0); 
          test.runTest(arg0);
          test.teardownTest(arg0); 
      }
     */
    
    public static String login(String name, String pwd) throws Exception{
            final String APPLICATION_JSON = "application/json";
          final String CONTENT_TYPE_TEXT_JSON = "text/json";
          
          DefaultHttpClient client = new DefaultHttpClient(
                    new PoolingClientConnectionManager());
        String url = "http://172.16.30.208:8092/svc/login";
            String js = "{\"username\":"+name+",\"password\":"+pwd+"}";
          
        HttpPost httpPost = new HttpPost(url);       
        httpPost.setHeader("Content-Type", "application/json;charset=UTF-8");

        
        StringEntity se = new StringEntity(js);
        se.setContentType(CONTENT_TYPE_TEXT_JSON);

        httpPost.setEntity(se);
        
        HttpResponse response = null;
        
        response = client.execute(httpPost);

        //----------判断是否重定向开始
        int code = response.getStatusLine().getStatusCode();
        String newuri="";
        if (code == 302) {
            org.apache.http.Header header = response.getFirstHeader("location"); // 跳转的目标地址是在 HTTP-HEAD 中的
            newuri = header.getValue(); // 这就是跳转后的地址,再向这个地址发出新申请,以便得到跳转后的信息是啥。
            System.out.println(newuri);
            System.out.println(code);
            
            httpPost = new HttpPost(newuri);
            httpPost.setHeader("Content-Type", "application/json;charset=UTF-8");
             
            se = new StringEntity(js);
            se.setContentType(CONTENT_TYPE_TEXT_JSON);

            httpPost.setEntity(se);

            response = client.execute(httpPost);
            code = response.getStatusLine().getStatusCode();
            System.out.println("login"+code);
        }

        //------------重定向结束
        HttpEntity entity = null;
        entity = response.getEntity();
        String s2 = EntityUtils.toString(entity, "UTF-8");
        System.out.println(s2);
        return s2;
        
    
}
}

3、将该项目打成jar包,放入jmeter安装目录下的apache-jmeter-2.13\lib\ext里,其它依赖的包放入apache-jmeter-2.13\lib里

4、启动jmeter,新建组->java请求->查看结果树,即可看到如下:

5、运行后,查看结果树:

注意:

程序依赖的包一定记得放入lib目录里,如果使用httpclient,jmeter自带的httclientjar包比较老,可删除老的,放入新版的jar包

posted on 2016-01-05 18:33  乔叶叶  阅读(1260)  评论(0编辑  收藏  举报

导航