Jmeter 自定义测试请求(功能完善)

之间写过一篇Jmeter 自定义测试请求,文章中只是简单描述了自定义java请求的过程,现在对SampleResult类进行补充,提取我们的请求response header信息和response信息,这样jmeter在进行压测时,就可以更好地管理我们的java请求了。其中使用了jmeter2.9中的 ApacheJMeter_core.jar、ApacheJmeter_java.jar和httpclient-4.2.3.jar。

 package com.yeetrack.jmeter;

import java.io.IOException;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
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;

/**
 * @author youthflies
 * 易踪网 yeetrack.com
 */
public class App extends AbstractJavaSamplerClient
{

    private SampleResult sampleResult();
    DefaultHttpClient httpClient = new DefaultHttpClient();
    @Override     
    public Arguments getDefaultParameters()     
    {         
        // TODO Auto-generated method stub         
        return super.getDefaultParameters();     
    }     
    @Override     
    public void setupTest(JavaSamplerContext context)     
    {         // TODO Auto-generated method stub         
        super.setupTest(context);     
    }     
    public SampleResult runTest(JavaSamplerContext arg0)     
    {         // TODO Auto-generated method stub         
        sampleResult = new SampleResult();         
        sampleResult.setSampleLabel("jmeter自定义测试请求 by yeetrack.com");             HttpHost target = new HttpHost("www.baidu.com", 80, "http");         
        HttpGet get = new HttpGet("http://www.baidu.com");         
        //设置request header         
        get.setHeader("Connection", "keep-alive");         
        get.setHeader("Host", "www.baidu.com");         
        get.setHeader("User-Agent", "Apache-HttpClient/4.2.3 (java 1.5)");              //处理request header         
        Header[] requestHeaders = get.getAllHeaders();         
        StringBuffer requestheaderBuffer = new StringBuffer();         
        for(Header header : requestHeaders)             
            requestheaderBuffer.append(header.getName()+":"+header.getValue()+"\n");         
            sampleResult.setRequestHeaders(requestheaderBuffer.toString());                 try         
            {             //开始计时             
                 sampleResult.sampleStart();             
                 //执行get方法             
                 HttpResponse response = httpClient.execute(target, get);                       //计时结束             
                 sampleResult.sampleEnd();             
                //处理response header             
                 Header[] headers = response.getAllHeaders();             
                 StringBuffer  headerBuffer = new StringBuffer();             
                 headerBuffer.append(response.getStatusLine()+"\n");                             for(Header header : headers)                 
                      headerBuffer.append(header.getName()+":"+header.getValue()+"\n");             
                      sampleResult.setResponseHeaders(headerBuffer.toString());             //处理response 文本             
                    HttpEntity httpEntity = response.getEntity();             
                     String responseString = EntityUtils.toString(httpEntity);             
                   System.out.println(responseString);             
                  sampleResult.setResponseData(responseString, "utf-8");             
                   sampleResult.setResponseCodeOK();             
                   if(response.getStatusLine().toString().contains("200"))                 
                               sampleResult.setResponseCode("200");             
                             sampleResult.setResponseMessageOK();             
                             sampleResult.setSuccessful(true);             
                            if(true) //添加自己的校验条件,当然也可以在jmeter中添加,不在代码中添加                 
                                    sampleResult.setSuccessful(true);         
                       } 
                catch (ClientProtocolException e)         
                {             // TODO Auto-generated catch block             
                       e.printStackTrace();         
                 } catch (IOException e)         
                {             // TODO Auto-generated catch block             
                         e.printStackTrace();         
                 }         return sampleResult;     
                 }     
                 @Override     
                 public void teardownTest(JavaSamplerContext context)     
                {         // TODO Auto-generated method stub         
                       super.teardownTest(context);   
                       httpClient.getConnectionManager().shutdown();  
     } }
版权声明

本站文章、图片、视频等(除转载外),均采用知识共享署名 4.0 国际许可协议(CC BY-NC-SA 4.0),转载请注明出处、非商业性使用、并且以相同协议共享。

© 空空博客,本文链接:https://www.yeetrack.com/?p=544