一、下载源码
jmeter源码下载地址(最新版):http://jmeter.apache.org/download_jmeter.cgi
下载对应的Source的源码包
jmeter源码历史版本:https://archive.apache.org/dist/jmeter/source/
建议下载3.x版本,因为有些版本没有配置文件eclipse.classpath和eclipse.project
* 修改eclipse.classpath和eclipse.project文件为:
.classpath
.project
如果无法直接修改成功的话,可以使用CMD命令:
`ren .\eclipse.classpath .classpath`
`ren .\eclipse.project .project`
二、IDEA导入JMeter源码
- 1.打开intellij idea,file–>new–>project from existing source,如下图所示
- 2.如下图进行选择,注意,一定要选择按eclipse项目导入
- 3.点击下一步,默认操作即可
- 4.等intellij idea打开此项目后,在最右边的“边栏辅助工具”中,找到“ant build”,点开,再找到上方的“+号”,点击,会弹开如下图所示,选择build.xml
-
5.打开build.xml后,找到download_jars,双击,下载jmeter所需要的所有jar包,下载到%jmeter_src%/lib目录下,如下图所示:
-
编译成功后,如下图所示:
-
6.设置项目属性,如下图所示
-
7.设置dependencies,就是导入jar包,先将所有出错的jar包删除,如下图
-
9.重新导入所有jmeter所需要的jar包,如下图,点击那个+号,选择jars or derectories:
-
8.删除所有错误的jar包后,如下图所示,注意,要点一下右下方的apply按钮
-
10.必须重新ant install一下,如下图
-
12.在运行之前建议先把这个VM配置一下,-Djmeter.home=D:\Software\Jmeter\apache-jmeter-3.2_src,这是你源码的位置
-
11.ctrl+n,在弹出对话框中,输入NewDriver,会打开这个类,这个类是jmeter的main class,在build.xml中有配置
-
自定义函数类:
package org.apache.jmeter.functions;
import org.apache.jmeter.engine.util.CompoundVariable;
import org.apache.jmeter.samplers.SampleResult;
import org.apache.jmeter.samplers.Sampler;
import java.util.Collection;
import java.util.List;
public class Hello extends AbstractFunction {
/**
* <p><b>
* N.B. execute() should be synchronized if function is operating with non-thread-safe
* objects (e.g. operates with files).
* </b></p>
* JMeter ensures setParameters() happens-before execute(): setParameters is executed in main thread,
* and worker threads are started after that.
*
* @param previousResult
* @param currentSampler
* @see Function#execute(SampleResult, Sampler)
*/
private static final String KEY = "__Hello";
@Override
public String execute(SampleResult previousResult, Sampler currentSampler) throws InvalidVariableException {
return "Hello Word!";
}
/**
* Note: This is always called even if no parameters are provided
* (versions of JMeter after 2.3.1)
*
* @param parameters
* @see Function#setParameters(Collection)
*/
@Override
public void setParameters(Collection<CompoundVariable> parameters) throws InvalidVariableException {
}
/**
* @see Function#getReferenceKey()
*/
@Override
public String getReferenceKey() {
return KEY;
}
/**
* Return a list of strings briefly describing each parameter your function
* takes. Please use JMeterUtils.getResString(resource_name) to grab a
* resource string. Otherwise, your help text will be difficult to
* internationalize.
* <p>
* This list is not optional. If you don't wish to write help, you must at
* least return a List containing the correct number of blank strings, one
* for each argument.
*
* @return List with brief descriptions for each parameter the function takes
*/
@Override
public List<String> getArgumentDesc() {
return null;
}
}
- 写好之后,编译一下运行NewDriver.java即可,会出现对应的JMeter GUI可视化界面
- 、将编译后的class文件放到ApacheJMeter_functions.jar中,使用WinRAR打开ApacheJMeter_functions.jar,复制Hello.class文件至\org\apache\jmeter\functions\目录下保存
- 重启JMeter即可
三、运行报错
logging only errors to the console. Set system property 'org.apache.logging.log4j.simplelog.StatusLogger.level' to TRACE to show Log4j2 internal initialization logging.
- 解决办法:
在工程的resources目录下新建一个文件:log4j2.xml ,然后在该文件中下入以下配置信息或者修改对应的log4j2.xml文件;建议修改之前把原来的代码复制出来,以便还原;
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Appenders>
<Console name="STDOUT" target="SYSTEM_OUT">
<PatternLayout pattern="%d %-5p [%t] %C{2} (%F:%L) - %m%n"/>
</Console>
<RollingFile name="RollingFile" fileName="logs/strutslog1.log"
filePattern="logs/$${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.log.gz">
<PatternLayout>
<Pattern>%d{MM-dd-yyyy} %p %c{1.} [%t] -%M-%L- %m%n</Pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy />
<SizeBasedTriggeringPolicy size="1 KB"/>
</Policies>
<DefaultRolloverStrategy fileIndex="max" max="2"/>
</RollingFile>
</Appenders>
<Loggers>
<Logger name="com.opensymphony.xwork2" level="WAN"/>
<Logger name="org.apache.struts2" level="WAN"/>
<Root level="warn">
<AppenderRef ref="STDOUT"/>
</Root>
</Loggers>
</Configuration>