利用JMeter 的 BeanShell 测试SDK

JMeter中有个BeanShellSampler

截图如下:


顾名思义,就是直接写BeanShell来发起请求,其本质上是java代码,如果import相关的jar包,需要将相关的jar包加入jmeter可以引的路径下。

这里,给出一段示例代码:

[java]  view plain  copy
  1. import java.io.IOException;  
  2. import java.io.UnsupportedEncodingException;  
  3. import java.util.ArrayList;  
  4. import java.util.HashMap;  
  5. import java.util.LinkedList;  
  6. import java.util.List;  
  7. import java.util.Map;  
  8.   
  9. import org.apache.http.HttpEntity;  
  10. import org.apache.http.HttpResponse;  
  11. import org.apache.http.NameValuePair;  
  12. import org.apache.http.client.ClientProtocolException;  
  13. import org.apache.http.client.HttpClient;  
  14. import org.apache.http.client.entity.UrlEncodedFormEntity;  
  15. import org.apache.http.client.methods.HttpPost;  
  16. import org.apache.http.impl.client.DefaultHttpClient;  
  17. import org.apache.http.message.BasicNameValuePair;  
  18. import org.apache.http.util.EntityUtils;  
  19.   
  20. public static void testHttpClinet(){  
  21.         HttpClient httpClient = new DefaultHttpClient();  
  22.         HttpPost httpPost = new HttpPost("http://localhost:8080/TimOne/category/queryCategory.do");  
  23.         List<NameValuePair> params = new ArrayList<NameValuePair>(); //这句话需要修改为List params = new ArrayList();  
  24.         params.add(new BasicNameValuePair("type"""));  
  25.         params.add(new BasicNameValuePair("page""1"));  
  26.         params.add(new BasicNameValuePair("rows""5"));  
  27.         UrlEncodedFormEntity entity = null;  
  28.         try {  
  29.             entity = new UrlEncodedFormEntity(params,"utf-8");  
  30.         } catch (UnsupportedEncodingException e) {  
  31.             // TODO Auto-generated catch block  
  32.             e.printStackTrace();  
  33.         }  
  34.         httpPost.setEntity(entity);  
  35.         try {  
  36.             HttpResponse httpResponse = httpClient.execute(httpPost);  
  37.             if(httpResponse.getStatusLine().getStatusCode() == 200){  
  38.                 System.out.println("请求和响应成功");  
  39.                 HttpEntity httpEntity = httpResponse.getEntity();  
  40.                 String response = EntityUtils.toString(httpEntity);  
  41.                 System.out.println(response);  
  42.             }  
  43.         } catch (ClientProtocolException e) {  
  44.             // TODO Auto-generated catch block  
  45.             e.printStackTrace();  
  46.         } catch (IOException e) {  
  47.             // TODO Auto-generated catch block  
  48.             e.printStackTrace();  
  49.         }  
  50. }  
  51.   
  52. testHttpClinet(); //执行方法  

上述的BeanShell Sample需要注意的地方有,需要定义一个静态方法,如果需要执行,则直接调用静态方法。

执行testHttpClient(),发现报如下错误

2017/04/28 23:52:04 INFO  - jmeter.threads.JMeterThread: Threadstarted: Thread Group 1-1

2017/04/28 23:52:04 ERROR -jmeter.util.BeanShellInterpreter: Error invoking bsh method: eval   In file: inline evaluation of: ``importjava.io.IOException; import java.io.UnsupportedEncodingException; import  . . . '' Encountered "=" at line23, column 36.

 2017/04/28 23:52:04 WARN  - jmeter.protocol.java.sampler.BeanShellSampler:org.apache.jorphan.util.JMeterException: Error invoking bsh method: eval In file: inline evaluation of: ``importjava.io.IOException; import java.io.UnsupportedEncodingException; import  . . . '' Encountered "=" at line23, column 36.

也就是报这句话异常:List<NameValuePair> params = new ArrayList<NameValuePair>();

修改来修改去,也发现不了为什么这句话有异常,后来改为:

[java]  view plain  copy
  1. List params = new ArrayList ();  

程序可以成功运行。同理,Map中也不能加具体类型Map<String, String>也不可以。

总结:

BeanShell其实就是让我们自己写Java代码来进行接口调用,当然这种方式可以用来测试SDK

猜你喜欢

转载自blog.csdn.net/qq_27791709/article/details/79733169
今日推荐