第一步: 創(chuàng)建一個(gè)maven項(xiàng)目
? ? ?這一步:網(wǎng)上很多教程,自行查看
第二步:導(dǎo)入依賴包:
<dependency>
<groupId>org.apache.jmeter</groupId>
<artifactId>ApacheJMeter_core</artifactId>
<version>5.3</version>
</dependency>
<dependency>
<groupId>org.apache.jmeter</groupId>
<artifactId>ApacheJMeter_java</artifactId>
<version>5.3</version>
</dependency>
<build>
? ? <finalName>vapeJmeter</finalName>
? ? <plugins>
? ? ? ? <plugin>
? ? ? ? ? ? <groupId>org.apache.maven.plugins</groupId>
? ? ? ? ? ? <artifactId>maven-compiler-plugin</artifactId>
? ? ? ? ? ? <version>3.5.1</version>
? ? ? ? ? ? <configuration>
? ? ? ? ? ? ? ? <source>1.8</source>
? ? ? ? ? ? ? ? <target>1.8</target>
? ? ? ? ? ? ? ? <encoding>UTF-8</encoding>
? ? ? ? ? ? </configuration>
? ? ? ? </plugin>
? ? </plugins>
</build>
注意? 包名必須含有??functions 邮辽,例如:?com.vape.functions
代碼:?
package com.vape.functions;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import org.apache.jmeter.engine.util.CompoundVariable;
import org.apache.jmeter.functions.AbstractFunction;
import org.apache.jmeter.functions.InvalidVariableException;
import org.apache.jmeter.samplers.SampleResult;
import org.apache.jmeter.samplers.Sampler;
import org.apache.jmeter.threads.JMeterVariables;
import org.apache.jmeter.util.JMeterUtils;
/**
*@author? ? created by derrick
*@date? 2021年4月19日---上午11:45:40
*@problem
*@answer
*@action
*/
public class MobileFunction extends AbstractFunction {
? ? //function名稱
? ? private static final String KEY = "__testMobile";
? ? private static final List<String> desc = new LinkedList<String>();
? ? private static final String[]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 CompoundVariable varName;
? ? /** 執(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();
? ? ? ? ? ? final String varTrim= varName.execute().trim();
? ? ? ? ? ? if (vars != null&& varTrim.length() > 0) {
? ? ? ? ? ? ? ? vars.put(varTrim, mobile);
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? return mobile;
? ? }
? /** 設(shè)置參數(shù)*/
? ? 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;
? ? }
? ? public List<String>getArgumentDesc() {
? ? ? ? return desc;
? ? }
? ? private static int getNum(int end)
? ? {
? ? ? ? return (int)(Math.random()*(end-1));
? ? }
}
然后使用maven打包?
最后把生成的jar丟到?\lib\ext 目錄下,重啟jmeter