很多人面试时候被问,有没有做过JMeter一些插件,不少人一头雾水,其实这个本身并不难,根据我下面介绍就能完成。
1.首先看下JMeter5.3的代码结构,这个只需要了解下就可以了;
解析:
Bin:执行文档,配置文件
Lib:依赖包
Src:源码
1)components—包含与协议无关的组件,如可视化、断言等等。
2)core —JMeter的核心代码,包括所有的核心接口和抽象类。
3)examples —演示采样器如何使用新bean框架的例子。
4)functions —所使用的组件的标准功能。
5)jorphan—提供常见实用功能的实用工具类
6) protocol—包含了JMeter支持的不同协议(ftp http、tcp—socket协议,没有webservice)
2 JMeter运行原理
3 JMeter二次开发插件
新建一个maven项目:
注意点新建的包名是固定的functions,否则之后放入JMeter不能识别,如图示意:
这里我做一个生成电话号码的插件:
Pom .xml文件配置:
<?xml version="1.0"encoding="UTF-8"?>
<projectxmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com</groupId>
<artifactId>test</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<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>
</dependencies>
<build>
<defaultGoal>compile</defaultGoal>
</build>
</project>
<?xml version="1.0"encoding="UTF-8"?>
<projectxmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com</groupId>
<artifactId>test</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<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>
</dependencies>
<build>
<defaultGoal>compile</defaultGoal>
</build>
</project>
代码:
package com.functions;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import org.apache.jmeter.engine.util.CompoundVariable;
importorg.apache.jmeter.functions.AbstractFunction;
importorg.apache.jmeter.functions.InvalidVariableException;
import org.apache.jmeter.samplers.SampleResult;
import org.apache.jmeter.samplers.Sampler;
import org.apache.jmeter.util.JMeterUtils;
import org.apache.jmeter.threads.JMeterVariables;
public class PhoneNumber extends AbstractFunction {
//定义function名称
private static final StringKEY = "__cctesterMobile";
private static finalList<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("Nameof variable in which to store the result (optional)"));
}
private CompoundVariable varName;
//执行部分
@Override
public Stringexecute(SampleResult previousResult, Sampler currentSampler) throws InvalidVariableException{
int index =getNum(0,telFirst.length - 1);
String first = telFirst[index];
String second = String.valueOf(getNum(1, 888) + 10000).substring(1);
String third = String.valueOf(getNum(1, 9100) + 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;
}
//设置参数
public voidsetParameters(Collection<CompoundVariable> parameters)throwsInvalidVariableException
{
checkParameterCount(parameters, 0, 1);
Object[] values =parameters.toArray();
if (values.length > 0) {
varName = (CompoundVariable)values[0];
} else {
varName = null;
}
}
/** {@inheritDoc} */
@Override
public StringgetReferenceKey() {
return KEY;
}
/** {@inheritDoc} */
public List<String>getArgumentDesc() {
return desc;
}
private static int getNum(int start,int end)
{
return (int)(Math.random()*(end-1));
}
}
将工程打包后,放到jmeter的lib下ext中
重启jmeter看下函数助手:
这样一个简单的插件就完成了,本身并不难哦!