如何使用Java 開發(fā)Apache JMeter Function插件
一.開發(fā)JMeter函數(shù)插件的目的
有時(shí)候,JMeter自帶的插件不能滿足自動(dòng)化/性能測試的需求团南,這個(gè)時(shí)候就需要自己來開發(fā)插件浅浮,解決這個(gè)問題
二.什么是Function組件?
通過打開函數(shù)助手,我們可以通過下拉菜單桩引,查看JMeter為我們默認(rèn)提供的一系列實(shí)用的函數(shù)功能,使用函數(shù)非常簡單收夸,比如”__testMobile”函數(shù)坑匠,功能是生成一個(gè)手機(jī)號碼,在后面的輸入框中只需輸入”${__
testMobile()}”便可以引入調(diào)用該函數(shù)方法所返回的值卧惜,所有的組件都可以對函數(shù)組件進(jìn)行引入
不帶參數(shù)的函數(shù)
帶參數(shù)的函數(shù)
三.接下來我們來實(shí)現(xiàn)上面兩個(gè)函數(shù)插件
介紹下我使用的是IDEA 2021版本厘灼,JDK1.8;創(chuàng)建MAVEN項(xiàng)目編譯打包
1咽瓷、首先創(chuàng)建Java工程设凹,F(xiàn)ile>New>Project,選擇maven工程溢陪,next>命名為hs-jmeter-auto>next
2滤钱、先在Java工程中引入主要的JMeter插件開發(fā)jar包【Apache JMeter Core和 Apache JMeter Java】修赞,可在https://mvnrepository.com/搜索需要的jar包
3.代碼實(shí)現(xiàn)
創(chuàng)建package hg.auto.functions鹏漆,在該包下創(chuàng)建CarId.Java類繼承AbstractFunction類光酣,并重寫該類的execute/setParameters/getReferenceKey/getArgumentDesc方法
打包后遭殉,把jar放到JMeter lib下的ext下廊谓,重新啟動(dòng)JMeter
最后附上代碼
/**
?*@author derrick
?*/
public class CarId extends AbstractFunction {
??? private static final List<String> desc = new LinkedList<>();
??? /**在函數(shù)助手中的顯示名稱**/
??? private static final String KEY = "__CarId";
??? static{
??????? //函數(shù)參數(shù)-省份簡稱
??????? desc.add(JMeterUtils.getResString("Province_param"));
??????? //函數(shù)參數(shù)-市級ID
??????? desc.add(JMeterUtils.getResString("CityId_param"));
??????? desc.add(JMeterUtils.getResString("function_name"));
??? }
??? private CompoundVariableProvince_param;
??? privateCompoundVariable CityId_param;
??? privateCompoundVariable varName;
??? @Override
??? public List<String>getArgumentDesc() {
??????? return desc;
??? }
??? @Override
??? public String execute(SampleResult
previousResult, Sampler currentSampler) throws InvalidVariableException{
??????? //獲取所輸入?yún)?shù)-省級簡稱
??????? String province = Province_param.execute().trim();
??????? //獲取所輸入?yún)?shù)-市級ID
??????? String city = CityId_param.execute().trim();
??????? //實(shí)現(xiàn)邏輯不變
??????? char[] license = new char[5];
??????? inti = 0;
??????? intk = 0;
??????? intn = 0;
??????? for(; i < 5; i++) {
??????????? if (Math.random() >0.5) {
??????????????? if (k < 3) {
??????????????????? license[i] = (char) ('A' + Math.random() *26);
??????????????????? k++;
??????????????? } else {
??????????????????? license[i] = (char) ('0' + Math.random() *10);
??????????????????? n++;
??????????????? }
??????????? }else {
??????????????? if (n < 4) {
??????????????????? license[i] = (char) ('0' + Math.random() *10);
??????????????????? n++;
??????????????? } else {
??????????????????? license[i] = (char) ('A' + Math.random() *26);
??????????????????? k++;
??????????????? }
??????????? }
??????? }
??????? String licenses = String.valueOf(license);
??????? String carId = province+city+licenses;
??????? if(varName != null) {
??????????? JMeterVariables vars =getVariables();
??????????? finalString varTrim = varName.execute().trim();
??? ????????if(vars != null && varTrim.length() > 0) {
??????????????? vars.put(varTrim, carId);
??????????? }
??????? }
??????? return carId;
??? }
??? @Override
??? public void setParameters(Collection<CompoundVariable> parameters) throws InvalidVariableException{
??????? checkParameterCount(parameters, 2, 3);
??????? Object[] values = parameters.toArray();
??????? Province_param =
(CompoundVariable) values[0];
??????? CityId_param =
(CompoundVariable) values[1];
??????? if(values.length > 2) {
??????????? varName =
(CompoundVariable) values[2];
??????? } else {
??????????? varName = null;
??????? }
??? }
??? @Override
??? public String getReferenceKey() {
??????? return KEY;
??? }
}
隨機(jī)生成手機(jī)號碼
/**
?*@author created by derrick
?*@date 2021年4月19日---上午11:45:40
?*/
public class MobileFunction extends AbstractFunction {
??? /** function名稱 */
??? private static final String KEY = "__testMobile";
??? private static finalList<String> desc = new LinkedList<String>();
??? private static finalString[] telFirst = "134,135,136,137,138,139,150,151,152,157,158,159,130,131,132,155,156,133,153".split(",");
??? /** function描述? */
??? static {
??????? desc.add(JMeterUtils.getResString("derrick test"));
??? }
??? private CompoundVariablevarName;
??? /** 執(zhí)行部分 */
??? @Override
??? public String execute(SampleResult
previousResult, Sampler currentSampler) throws InvalidVariableException{
??????? int index =getNum(telFirst.length - 1);
??????? String first = telFirst[index];
??????? String second = String.valueOf(getNum(999) + 10000).substring(1);
??????? String third = String.valueOf(getNum(9990) + 10000).substring(1);
??????? String mobile = first + second + third;
??????? if(varName != null) {
??????????? JMeterVariables vars =getVariables();
??????????? finalString varTrim = varName.execute().trim();
??????????? if(vars != null && varTrim.length() > 0) {
??????????????? vars.put(varTrim, mobile);
??????????? }
??????? }
??????? return mobile;
??? }
??? /** 設(shè)置參數(shù) */
??? @Override
??? public void setParameters(Collection<CompoundVariable> parameters) throws InvalidVariableException{
??????? checkParameterCount(parameters, 0, 1);
??????? Object[] values = parameters.toArray();
??????? if(values.length > 0) {
??????????? varName =
(CompoundVariable) values[0];
??????? } else {
??????????? varName = null;
??????? }
??? }
??? @Override
??? public String getReferenceKey() {
??????? return KEY;
??? }
??? @Override
??? public List<String>getArgumentDesc() {
??????? return desc;
??? }
??? private static int getNum(int end) {
??????? return (int) (Math.random() *(end -1));
??? }
}