利用JMeter 的 BeanShell 测试SDK
JMeter中有个BeanShellSampler
截图如下:
顾名思义,就是直接写BeanShell来发起请求,其本质上是java代码,如果import相关的jar包,需要将相关的jar包加入jmeter可以引的路径下。
这里,给出一段示例代码:
- import java.io.IOException;
- import java.io.UnsupportedEncodingException;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.LinkedList;
- import java.util.List;
- import java.util.Map;
- import org.apache.http.HttpEntity;
- import org.apache.http.HttpResponse;
- import org.apache.http.NameValuePair;
- import org.apache.http.client.ClientProtocolException;
- import org.apache.http.client.HttpClient;
- import org.apache.http.client.entity.UrlEncodedFormEntity;
- import org.apache.http.client.methods.HttpPost;
- import org.apache.http.impl.client.DefaultHttpClient;
- import org.apache.http.message.BasicNameValuePair;
- import org.apache.http.util.EntityUtils;
- public static void testHttpClinet(){
- HttpClient httpClient = new DefaultHttpClient();
- HttpPost httpPost = new HttpPost("http://localhost:8080/TimOne/category/queryCategory.do");
- List<NameValuePair> params = new ArrayList<NameValuePair>(); //这句话需要修改为List params = new ArrayList();
- params.add(new BasicNameValuePair("type", ""));
- params.add(new BasicNameValuePair("page", "1"));
- params.add(new BasicNameValuePair("rows", "5"));
- UrlEncodedFormEntity entity = null;
- try {
- entity = new UrlEncodedFormEntity(params,"utf-8");
- } catch (UnsupportedEncodingException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- httpPost.setEntity(entity);
- try {
- HttpResponse httpResponse = httpClient.execute(httpPost);
- if(httpResponse.getStatusLine().getStatusCode() == 200){
- System.out.println("请求和响应成功");
- HttpEntity httpEntity = httpResponse.getEntity();
- String response = EntityUtils.toString(httpEntity);
- System.out.println(response);
- }
- } catch (ClientProtocolException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- 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>();
修改来修改去,也发现不了为什么这句话有异常,后来改为:
- List params = new ArrayList ();
程序可以成功运行。同理,Map中也不能加具体类型Map<String, String>也不可以。
总结:
BeanShell其实就是让我们自己写Java代码来进行接口调用,当然这种方式可以用来测试SDK。