上一篇文章中,提到怎么生成jar包讓jmeter使用,這次我們來試試做參數(shù),因為發(fā)現(xiàn)調(diào)包的時候其實更多還是參數(shù)化,那么開始改造吧
成都創(chuàng)新互聯(lián)長期為成百上千客戶提供的網(wǎng)站建設(shè)服務(wù),團隊從業(yè)經(jīng)驗10年,關(guān)注不同地域、不同群體,并針對不同對象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺,與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為紫云企業(yè)提供專業(yè)的成都網(wǎng)站設(shè)計、成都網(wǎng)站制作、外貿(mào)網(wǎng)站建設(shè),紫云網(wǎng)站改版等技術(shù)服務(wù)。擁有10多年豐富建站經(jīng)驗和眾多成功案例,為您定制開發(fā)。
1.在httpclientpost這個類中替換參數(shù),且打印參數(shù)
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"); Listparams = new ArrayList (); 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中實現(xiàn)getDefaultParameters方法,這步很重要
//參數(shù)化方法 @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(); //事務(wù)啟動 try { //參數(shù)化方法的使用 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();//事務(wù)關(guān)閉 return result; } public static void main(String[] args) { new addcomputerInfo().runTest(new JavaSamplerContext(new Arguments())); }
三、重新打包
四:jmeter使用
可以看到已經(jīng)加載了兩個參數(shù),我們給下面參數(shù)做個參數(shù)化,增加一個計數(shù)器
增加計數(shù)器,每次遞增1,每次填充0001
使用:
運行,因為都是沒有注冊的用戶,所以都是錯誤
以上就是本次介紹的全部相關(guān)知識點,感謝大家的學(xué)習(xí)和對創(chuàng)新互聯(lián)的支持。