由于LR不支持PUT方式,所以只好通过java Vuser写压测脚本,使用httpclient jar包,我使用的httpclient4.5版本。
/* * LoadRunner Java script. (Build: _build_number_) * * Script Description: * */ import lrapi.lr; import lrapi.web; import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.List; import java.util.Date; import java.text.SimpleDateFormat; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; 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.client.methods.HttpPut; import org.apache.http.impl.client.HttpClients; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; public class Actions { public int init() throws Throwable { return 0; }//end of init public int action() throws Throwable { HttpClient client = HttpClients.createDefault(); HttpPut put = new HttpPut("http://xxxxcart.beta1.fn/xxxCart/rest/v1/cart/"); // lr.save_string(lr.eval_string("<owner>"), "user"); // lr.output_message("The value of str = " + lr.eval_string("<user>") + "."); String sourceStr = lr.eval_string("<name5529206105542>"); String sourceArray[] = sourceStr.split(","); List<BasicNameValuePair> parameters = new ArrayList<BasicNameValuePair>(); parameters.add(new BasicNameValuePair("owner",sourceArray[0])); lr.save_string(sourceArray[0], "user"); // parameters.add(new BasicNameValuePair("owner","name5529206105542")); for ( int i=2;i<sourceArray.length;i++ ) { lr.save_string(sourceArray[i],"id1"); // lr.output_message("id1 value is "+lr.eval_string("<id1>")); parameters.add(new BasicNameValuePair("area_seq","CS000016")); parameters.add(new BasicNameValuePair("is_login","1")); parameters.add(new BasicNameValuePair("mem_type","0")); // parameters.add(new BasicNameValuePair("cart_detail_list","[{\"cart_detail_id\":\"715\",\"qty\":2,\"selected\":true,\"campaign_seq\":\"\"}]")); parameters.add(new BasicNameValuePair("cart_detail_list","[{\"cart_detail_id\":\"<id1>\",\"qty\":<qty>,\"selected\":true,\"campaign_seq\":\"\"}]")); /* List<BasicNameValuePair> parameters = new ArrayList<BasicNameValuePair>(); parameters.add(new BasicNameValuePair("owner","<user>")); parameters.add(new BasicNameValuePair("area_seq","CS000016")); parameters.add(new BasicNameValuePair("is_login","1")); parameters.add(new BasicNameValuePair("mem_type","0")); parameters.add(new BasicNameValuePair("cart_detail_list","[{\"cart_detail_id\":\"<id>\",\"qty\":2,\"selected\":true,\"campaign_seq\":\"\"}]")); */ // System.out.println("parameters is " + parameters ); try { put.setEntity(new UrlEncodedFormEntity(parameters,"UTF-8")); lr.start_transaction("t1"); HttpResponse response = client.execute(put); HttpEntity entity = response.getEntity(); String result = EntityUtils.toString(entity, "UTF-8"); // System.out.println(result); boolean b = result.contains("\"code\":1"); // System.out.println(b); if ( b == true ) { // System.out.println("为真"); lr.end_transaction("t1", lr.PASS); } else { // System.out.println("为假"); // lr.end_transaction("t1", lr.FAIL); FileWriter writer = null; try { Date now = new Date(); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");//可以方便地修改日期格式 String hehe = dateFormat.format( now ); writer = new FileWriter("e://codeerror.txt", true); // writer.write(result); BufferedWriter buffer = new BufferedWriter(writer); buffer.write(hehe); buffer.write(lr.eval_string("<user>")); buffer.write(result); buffer.newLine(); buffer.flush(); buffer.close(); } catch (IOException e) { e.printStackTrace(); } finally { try { if(writer != null){ writer.close(); } } catch (IOException e) { e.printStackTrace(); } } } } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }//end of for return 0; }//end of action public int end() throws Throwable { return 0; }//end of end }
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/kash_chen007/article/details/47829969