LoadRunner系列之—-03 用Java Vuser协议编写接口测试脚本
待测接口用java语言实现,且项目中调用该接口,请求报文直接送的对象,即需要用专门的jar包将数据转化为对象。
针对此类接口的LR压测脚本,可用Java Vuser协议实现,脚本中支持java语法。
代码样例如下:
/*
* LoadRunner Java script. (Build: _build_number_)
*
* Script Description:
*
*/
import lrapi.lr;
import java.util.Date;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.isoftstone.rules.premium.service.IPremiumCalculation;
public class Actions
{
static ApplicationContext context = new ClassPathXmlApplicationContext("remote-client-local.xml");
public int init() throws Throwable {
return 0;
}//end of init
public int action() throws Throwable {
String request = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><PACKET type=\"REQUEST\" version=\"1.0\"><HEAD><REQUEST_TYPE>0355-01</REQUEST_TYPE><SYSNAME>V7核心</SYSNAME><SYSCODE>00</SYSCODE><USERCDE>134518100</USERCDE><DPTCDE>34</DPTCDE><TYPE>I</TYPE><DATA>A</DATA><SERVICE_NO>0101020202</SERVICE_NO><SERVICENAME>premCalc-ShangGaiHou</SERVICENAME><USERID>70000001</USERID><PASSWORD>123456</PASSWORD></HEAD><BODY><COVERAGE_LIST><COVERAGE><INSRNC_CDE>035801</INSRNC_CDE><DEDUCTIBLE_CONVENTION></DEDUCTIBLE_CONVENTION><LIMIT_AMOUNT></LIMIT_AMOUNT><OPTIONAL_DI_DE></OPTIONAL_DI_DE><BASE_PRM>0.00</BASE_PRM><BEF_PRM>216.91</BEF_PRM><STANDARD_PREMIUM>130.15</STANDARD_PREMIUM><RATE>0.00</RATE><SERIAL_NO>35</SERIAL_NO><N_SEATS></N_SEATS><INSURED>0.00</INSURED><REFERENCE_PREMIUNS></REFERENCE_PREMIUNS><CANCEL_MARK>0</CANCEL_MARK><RISK_PRM>0.00</RISK_PRM><RISK_PRM_FLAG></RISK_PRM_FLAG><MAINTENANCE_FACTOR></MAINTENANCE_FACTOR></COVERAGE><COVERAGE><INSRNC_CDE>035001</INSRNC_CDE><DEDUCTIBLE_CONVENTION></DEDUCTIBLE_CONVENTION><LIMIT_AMOUNT></LIMIT_AMOUNT><OPTIONAL_DI_DE></OPTIONAL_DI_DE><BASE_PRM>939.90</BASE_PRM><BEF_PRM>1446.00</BEF_PRM><STANDARD_PREMIUM>867.60</STANDARD_PREMIUM><RATE>0.00</RATE><SERIAL_NO>0</SERIAL_NO><N_SEATS></N_SEATS><INSURED>7590.00</INSURED><REFERENCE_PREMIUNS></REFERENCE_PREMIUNS><CANCEL_MARK>0</CANCEL_MARK><RISK_PRM>939.9</RISK_PRM><RISK_PRM_FLAG>1</RISK_PRM_FLAG><MAINTENANCE_FACTOR></MAINTENANCE_FACTOR></COVERAGE><COVERAGE><INSRNC_CDE>035802</INSRNC_CDE><DEDUCTIBLE_CONVENTION></DEDUCTIBLE_CONVENTION><LIMIT_AMOUNT></LIMIT_AMOUNT><OPTIONAL_DI_DE></OPTIONAL_DI_DE><BASE_PRM>0.00</BASE_PRM><BEF_PRM>459.15</BEF_PRM><STANDARD_PREMIUM>275.49</STANDARD_PREMIUM><RATE>0.00</RATE><SERIAL_NO>36</SERIAL_NO><N_SEATS></N_SEATS><INSURED>0.00</INSURED><REFERENCE_PREMIUNS></REFERENCE_PREMIUNS><CANCEL_MARK>0</CANCEL_MARK><RISK_PRM>0.00</RISK_PRM><RISK_PRM_FLAG></RISK_PRM_FLAG><MAINTENANCE_FACTOR></MAINTENANCE_FACTOR></COVERAGE><COVERAGE><INSRNC_CDE>035002</INSRNC_CDE><DEDUCTIBLE_CONVENTION></DEDUCTIBLE_CONVENTION><LIMIT_AMOUNT></LIMIT_AMOUNT><OPTIONAL_DI_DE></OPTIONAL_DI_DE><BASE_PRM>1989.65</BASE_PRM><BEF_PRM>3061.00</BEF_PRM><STANDARD_PREMIUM>1836.60</STANDARD_PREMIUM><RATE>0.00</RATE><SERIAL_NO>1</SERIAL_NO><N_SEATS></N_SEATS><INSURED>300000.00</INSURED><REFERENCE_PREMIUNS></REFERENCE_PREMIUNS><CANCEL_MARK>0</CANCEL_MARK><RISK_PRM>0.00</RISK_PRM><RISK_PRM_FLAG></RISK_PRM_FLAG><MAINTENANCE_FACTOR></MAINTENANCE_FACTOR></COVERAGE><COVERAGE><INSRNC_CDE>035803</INSRNC_CDE><DEDUCTIBLE_CONVENTION></DEDUCTIBLE_CONVENTION><LIMIT_AMOUNT></LIMIT_AMOUNT><OPTIONAL_DI_DE></OPTIONAL_DI_DE><BASE_PRM>0.00</BASE_PRM><BEF_PRM>10.80</BEF_PRM><STANDARD_PREMIUM>6.48</STANDARD_PREMIUM><RATE>0.00</RATE><SERIAL_NO>37</SERIAL_NO><N_SEATS></N_SEATS><INSURED>0.00</INSURED><REFERENCE_PREMIUNS></REFERENCE_PREMIUNS><CANCEL_MARK>0</CANCEL_MARK><RISK_PRM>0.00</RISK_PRM><RISK_PRM_FLAG></RISK_PRM_FLAG><MAINTENANCE_FACTOR></MAINTENANCE_FACTOR></COVERAGE><COVERAGE><INSRNC_CDE>035003</INSRNC_CDE><DEDUCTIBLE_CONVENTION></DEDUCTIBLE_CONVENTION><LIMIT_AMOUNT></LIMIT_AMOUNT><OPTIONAL_DI_DE></OPTIONAL_DI_DE><BASE_PRM>46.80</BASE_PRM><BEF_PRM>72.00</BEF_PRM><STANDARD_PREMIUM>43.20</STANDARD_PREMIUM><RATE>4.68</RATE><SERIAL_NO>3</SERIAL_NO><N_SEATS>1</N_SEATS><INSURED>10000.00</INSURED><REFERENCE_PREMIUNS></REFERENCE_PREMIUNS><CANCEL_MARK>0</CANCEL_MARK><RISK_PRM>0.00</RISK_PRM><RISK_PRM_FLAG></RISK_PRM_FLAG><MAINTENANCE_FACTOR></MAINTENANCE_FACTOR></COVERAGE><COVERAGE><INSRNC_CDE>035804</INSRNC_CDE><DEDUCTIBLE_CONVENTION></DEDUCTIBLE_CONVENTION><LIMIT_AMOUNT></LIMIT_AMOUNT><OPTIONAL_DI_DE></OPTIONAL_DI_DE><BASE_PRM>0.00</BASE_PRM><BEF_PRM>6.91</BEF_PRM><STANDARD_PREMIUM>4.15</STANDARD_PREMIUM><RATE>0.00</RATE><SERIAL_NO>38</SERIAL_NO><N_SEATS></N_SEATS><INSURED>0.00</INSURED><REFERENCE_PREMIUNS></REFERENCE_PREMIUNS><CANCEL_MARK>0</CANCEL_MARK><RISK_PRM>0.00</RISK_PRM><RISK_PRM_FLAG></RISK_PRM_FLAG><MAINTENANCE_FACTOR></MAINTENANCE_FACTOR></COVERAGE><COVERAGE><INSRNC_CDE>035004</INSRNC_CDE><DEDUCTIBLE_CONVENTION></DEDUCTIBLE_CONVENTION><LIMIT_AMOUNT></LIMIT_AMOUNT><OPTIONAL_DI_DE></OPTIONAL_DI_DE><BASE_PRM>29.90</BASE_PRM><BEF_PRM>46.00</BEF_PRM><STANDARD_PREMIUM>27.60</STANDARD_PREMIUM><RATE>2.99</RATE><SERIAL_NO>4</SERIAL_NO><N_SEATS>1</N_SEATS><INSURED>10000.00</INSURED><REFERENCE_PREMIUNS></REFERENCE_PREMIUNS><CANCEL_MARK>0</CANCEL_MARK><RISK_PRM>0.00</RISK_PRM><RISK_PRM_FLAG></RISK_PRM_FLAG><MAINTENANCE_FACTOR></MAINTENANCE_FACTOR></COVERAGE></COVERAGE_LIST><BASE><PROD_NO>0355</PROD_NO><SALES_CHANNEL>19002</SALES_CHANNEL><ORIG_FLG>0</ORIG_FLG><DPT_CDE>34996603</DPT_CDE><SLS_CDE>134510951</SLS_CDE><SALEGRP_CDE>34996603005</SALEGRP_CDE><CMPNY_AGT_CDE></CMPNY_AGT_CDE><START_DATE>2016-12-11 00:00:00</START_DATE><END_DATE>2017-12-10 23:59:59</END_DATE><APP_DATE>2016-12-01</APP_DATE><SIGN_DATE>2016-12-01</SIGN_DATE><OPER_DATE>2016-12-01</OPER_DATE><SUMAMT>327590.00</SUMAMT><SUMPRM>3191.27</SUMPRM><PRM_CUR>01</PRM_CUR><RATIO>1.0</RATIO><OPR_TYP>0</OPR_TYP><PRNT_TYP>0</PRNT_TYP><CERTI_TYPE>124001</CERTI_TYPE><LASTPOLICYNO></LASTPOLICYNO><AGT_AGR_NO></AGT_AGR_NO><EXCHANGE_RATE>1.0</EXCHANGE_RATE><AMT_CUR>01</AMT_CUR><OPER_CDE>134518100</OPER_CDE><VHLGRP_FLG>0</VHLGRP_FLG><VHLGRP_PRO></VHLGRP_PRO><BSNS_NEW>191011</BSNS_NEW><CMM_PROP>0.38</CMM_PROP><CERTI_CODE></CERTI_CODE><SHORT_RATE>1</SHORT_RATE><REINSRC_FLG>0</REINSRC_FLG><DPT_CASCADE_CDE></DPT_CASCADE_CDE><COMPUTER_IP></COMPUTER_IP><USBKEY></USBKEY><LAST_YEAR_COMMERCIAL_WRECK_COUNT></LAST_YEAR_COMMERCIAL_WRECK_COUNT><INSURETYPE_FLAG></INSURETYPE_FLAG><CLAIM_AMOUNT></CLAIM_AMOUNT></BASE><VHL><BLD_YEAR></BLD_YEAR><BRND_CDE>KMD1282SDH</BRND_CDE><BRND_NME>凯马KMC5047D3XXY厢式运输车</BRND_NME><CAR_COLOUR></CAR_COLOUR><TFI_REF_CDE2></TFI_REF_CDE2><CONFIRM_CDE></CONFIRM_CDE><OWNER_NAME>王新祥</OWNER_NAME><ECDEMIC_VEHICLE_FLAG>0</ECDEMIC_VEHICLE_FLAG><ENGINE_NO>00938159</ENGINE_NO><DISPLACEMENT>2.16</DISPLACEMENT><USE_TYPE>345038013</USE_TYPE><VEHICLE_REGISTER_DATE>2009-12-14</VEHICLE_REGISTER_DATE><CAR_MARK>湾MM69531</CAR_MARK><VEHICLE_TYPE>345023002</VEHICLE_TYPE><PO_CATEGORY></PO_CATEGORY><VEHICLE_CATEGORY>345039006</VEHICLE_CATEGORY><VEHICLE_STYLE>345041055</VEHICLE_STYLE><QUERY_CDE>V0101ZSIC340016001480604351472</QUERY_CDE><RGN_CDE>002</RGN_CDE><USE_YEAR_CDE>345020004</USE_YEAR_CDE><USE_YEAR_NUM>6.992</USE_YEAR_NUM><VHL_BRAND></VHL_BRAND><RACK_NO>LWU2DM1C19KM08308</RACK_NO><VIN>LWU2DM1C19KM08308</VIN><RELATIONSHIP_WITH_VEHICLE>348001</RELATIONSHIP_WITH_VEHICLE><MADIN_CDE>34503601</MADIN_CDE><DEPRECIATION_COEFFICIENT>0.0090</DEPRECIATION_COEFFICIENT><MODELS_CONFIRMATION></MODELS_CONFIRMATION><BRAND_MODEL></BRAND_MODEL><EXTENDED_AREA_RADIUS></EXTENDED_AREA_RADIUS><PROPORTION_AGREEMENT></PROPORTION_AGREEMENT><TRAVEL_AREA_DESCRIPTION></TRAVEL_AREA_DESCRIPTION><VHL_GALSS>303011001</VHL_GALSS><INSURANCE_LAST_YEAR></INSURANCE_LAST_YEAR><LIABILITY_AMOUNT></LIABILITY_AMOUNT><LAST_YEAR_POLICY_NO></LAST_YEAR_POLICY_NO><ARE_TEAM>0</ARE_TEAM><LOAN_CAR_FLAG></LOAN_CAR_FLAG><TRANSFER_VEHICLE_LOGO>0</TRANSFER_VEHICLE_LOGO><FLEET_NO></FLEET_NO><NO_DAMAGE_YEARS></NO_DAMAGE_YEARS><LIMIT_LOAD_PERSON>2</LIMIT_LOAD_PERSON><LIMIT_LOAD>1.995</LIMIT_LOAD><NEWVHL_VAL>30000.00</NEWVHL_VAL><SUM_INSURED_TYPE></SUM_INSURED_TYPE><PACK></PACK><MODELS_ORIGINAL_PRICE></MODELS_ORIGINAL_PRICE><CHK_CDE>305005001</CHK_CDE><CHECK_MAN></CHECK_MAN><CHECK_TIME></CHECK_TIME><CHK_REC></CHK_REC><DEV_CDE></DEV_CDE><VEHICLE_MODEL></VEHICLE_MODEL><NEW_FLAG>1</NEW_FLAG><CERTIFICATE_DATE></CERTIFICATE_DATE><OWNER_TYPE>1</OWNER_TYPE><VHL_QUE_NO></VHL_QUE_NO><POWER>57</POWER><TRANSFER_DATE></TRANSFER_DATE><USE_YEAR_CDE1></USE_YEAR_CDE1><VEHICLE_CLASS_IFICATION></VEHICLE_CLASS_IFICATION></VHL><ADJUST><AVERAGE_MILE></AVERAGE_MILE><RGN_CDE /><GENDER /><AGE /><DRIVER_PERIOD /><CUSTOMER_LOYALTY /><NCD_PRIOR_YEAR>B13</NCD_PRIOR_YEAR><COEFFICIENT_SPECIAL_MATERIALS /><COVERAGE_NO /><EXPECTED_LOSS_RATIO /><SAFE_DRIVING /><AVERAGE_ANNUAL_MILEAGE /><DRIVER></DRIVER><AVERAGE_MILE_FACTOR></AVERAGE_MILE_FACTOR><MULTI_LINE_INSURANCE_FACTOR /><NO_COMPENSATI_COEFFICIENT>0.60</NO_COMPENSATI_COEFFICIENT><RISK_WARNING_FACTOR></RISK_WARNING_FACTOR><OVERALL_DISCOUNT>0.6</OVERALL_DISCOUNT><EE_LOSS_RATIO_FACTOR /><MANAGEMENT_LEVEL_FACTOR /><AVERAGE_ANNUAL_MILEAGE_FACTOR /><MORE_THAN_SAME_INSURING /><CAR_DAMAGE_MODEL_COEFFICIENT /><CLM_REC_VALUE /><CLM_REC_REASON /><KIND_ADJ_VALUE /><VHL_MDL_ADJ /><VHL_MDL_ADJ_UP /><VHL_MDL_ADJ_LOW /><PECC_REC_VALUE>1.0</PECC_REC_VALUE><PECC_REC_REASON></PECC_REC_REASON><MAG_ADJ /><MAG_ADJ_UPPER /><MAG_ADJ_LOW /><EXP_ADJ /><EXP_ADJ_UPPER /><EXP_ADJ_LOWER /><NO_CLAIMAD_JUST_REASON></NO_CLAIMAD_JUST_REASON><CONSULT_VALUE>7590.00</CONSULT_VALUE><PAYMENTRATE></PAYMENTRATE><CHA_FACTOR>0.85</CHA_FACTOR><UNDWRT_FACTOR>0.85</UNDWRT_FACTOR></ADJUST></BODY></PACKET>";
IPremiumCalculation service = (IPremiumCalculation) context.getBean("ilog");
lr.start_transaction("保费计算");
String response = service.invoke(request);
lr.end_transaction("保费计算", lr.AUTO);
System.out.println(response);
return 0;
}//end of action
public int end() throws Throwable {
return 0;
}//end of end
}
remote-client-local.xml(放在脚本对应文件夹目录下)
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean id="ilog" class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean"> <property name="serviceUrl"> <value>http://IP address/ilog_webapp/remote/PremiumCalculation</value> </property> <property name="serviceInterface"> <value>com.isoftstone.rules.premium.service.IPremiumCalculation</value> </property> </bean> </beans>
Java Environment settings: classpath配置如下
【Quality】
Quality is the value to someone who matters。做测试,首先要找到这个someone是谁,以及这个 someone重视的 value是什么。