java自定义jar包让jmeter使用的方法
2019-12-20 07:12
906 查看
上一篇文章中,提到怎么生成jar包让jmeter使用,这次我们来试试做参数,因为发现调包的时候其实更多还是参数化,那么开始改造吧
1.在httpclientpost这个类中替换参数,且打印参数
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; import org.apache.http.HttpEntity; import org.apache.http.NameValuePair; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.message.BasicNameValuePair; public class httpclientPost { public static int sendpost(String email,String value) throws ClientProtocolException, IOException { CloseableHttpClient httpclient = HttpClients.createDefault(); HttpPost post = new HttpPost("https://user.zaful.com/m-users-a-act_sign.htm?pipeline=zf"); List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair("email", email)); params.add(new BasicNameValuePair("password", value)); post.setEntity(new UrlEncodedFormEntity(params)); CloseableHttpResponse response2 = httpclient.execute(post); int code = response2.getStatusLine().getStatusCode(); HttpEntity http = response2.getEntity(); InputStream instreams = http.getContent(); BufferedReader reader = new BufferedReader(new InputStreamReader(instreams)); StringBuffer buffer = new StringBuffer(); String line; while ((line = reader.readLine()) != null) { buffer.append(line + "br "); } reader.close(); System.out.println("email: "+email +" password:"+value); System.out.println(code +" respones:"+buffer); return code; } }
在addcomputerInfo中实现getDefaultParameters方法,这步很重要
//参数化方法 @Override public Arguments getDefaultParameters() { // TODO Auto-generated method stub Arguments arguments=new Arguments(); arguments.addArgument("email",""); arguments.addArgument("value",""); return arguments; } public SampleResult runTest(JavaSamplerContext arg0) { // TODO Auto-generated method stub SampleResult result=new SampleResult(); result.sampleStart(); //事务启动 try { //参数化方法的使用 int responeCode=httpclientPost.sendpost(arg0.getParameter("email"),arg0.getParameter("value")); if (responeCode==200) { result.setSuccessful(true); }else { result.setSuccessful(false); } } catch (ClientProtocolException e) { result.setSuccessful(false); // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block result.setSuccessful(false); e.printStackTrace(); } result.sampleEnd();//事务关闭 return result; } public static void main(String[] args) { new addcomputerInfo().runTest(new JavaSamplerContext(new Arguments())); }
三、重新打包
四:jmeter使用
可以看到已经加载了两个参数,我们给下面参数做个参数化,增加一个计数器
增加计数器,每次递增1,每次填充0001
使用:
运行,因为都是没有注册的用户,所以都是错误
以上就是本次介绍的全部相关知识点,感谢大家的学习和对脚本之家的支持。
您可能感兴趣的文章:
相关文章推荐
- 在Jmeter中使用自定义编写的Java测试代码
- 使用Pyjnius 从python调用自定义Java方法
- 1. 【Java基础】jar, javac, java的具体使用方法
- java实现自定义数字验证码以及使用方法
- anroid 学习之java回调机制与自定义接口回调方法的使用
- java使用google的jar包生成二维码方法
- Java多线程——spring-boot 线程池 @Async 的使用、自定义Executor的配置方法
- [Java Eclipse] 使用Eclipse发布jar包以及制作exe文件的方法
- 在Jmeter中使用自定义编写的Java测试代码
- 最全面的使用idea和gradle把java项目打包成jar的方法
- 非java.util.zip,使用ant.jar制作zip压缩文件,以及相关中文解决方法
- Java加密部署,使用自定义的classloader解密的方法实例
- java项目中自定义的properties文件的使用方法
- Java注解(自定义方法注解使用及测试)
- 使用lombok.jar可以在Java代码中可以省略setter、getter方法了
- Redis(Windows安装方法与Java调用实例 & 配置文件参数说明 & Java使用Redis所用Jar包 & Redis与Memcached区别 & redis-cli.exe命令及示例)
- ecipse中Java项目导入jar包:1、使用maven,2、普通方法
- 在java中使用和创建自定义的native方法
- Java使用net.sf.fjep.fatjar第三包打包成jar文件方法
- eclipse 中添加自定义 classpath 的方法,以及 javac 和 java 的一些使用细节