Jmeter自定义功能

时间:2014-06-18 18:20:36

标签: java jmeter

我需要在Jmeter中创建一个自定义函数,由于性能问题,我不能使用beanshell。

我在http://gabenell.blogspot.com/2010/01/custom-functions-for-jmeter.htmlhttp://code4reference.com/2013/06/jmeter-custom-function-implementation/之后编写了一个java类,但是当我编译它时,我似乎无法让Jmeter识别它。

我的课程:

package custom.functions;

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 java.util.Collection;
import java.util.LinkedList;
import java.util.List;

public class Username extends AbstractFunction{
    private static final List<String> desc = new LinkedList<String>();
    private static final String KEY = "__Username";
    private int number = 0;

    static {
        desc.add("Pass a random value to get a valid username for the system.");
    }

    public Username() {
        super();
    }

    @Override
    public synchronized String execute(SampleResult previousResult, Sampler currentSampler)
            throws InvalidVariableException {
        try {
            return getValue(number);
        } catch(Exception e){
            throw new InvalidVariableException(e);
        }
    }

    public String getValue(int number){
        return "John-Smith";
    }

    @Override
    public synchronized void setParameters(Collection<CompoundVariable> parameters) throws InvalidVariableException {
        checkParameterCount(parameters, 1, 1);
        Object[] values = parameters.toArray();
        number = Integer.parseInt(((CompoundVariable) values[0]).execute().trim());
    }

    @Override
    public String getReferenceKey() {
        return KEY;
    }

    @Override
    public List<String> getArgumentDesc() {
        return desc;
    }
}

当我运行jar tf custom-functions.jar(以验证类文件是否在jar中)时:

META-INF/
META-INF/MANIFEST.MF
custom/
custom/functions/
custom/functions/Username.class

我将jar放在我的jmeter lib / ext目录中并尝试自行运行jmeter并使用-Jsearch_paths=../lib/ext/custom-functions.jar,但无论如何当我打开函数帮助工具时它没有列出,并且是一个简单的测试计划验证函数发送而不是%24%7B__Username%281%29%7D

我没有把文件放在正确的位置吗?它命名不正确吗?

2 个答案:

答案 0 :(得分:0)

你可以将groovy-all.jar放在Jmeter的类路径上,然后你就可以运行外部的.groovy脚本了,或者你可以添加一个JSR223-Groovy采样器。

答案 1 :(得分:0)

我的问题是我使用Java 8而不是Java 7编译了我的类,这是我用于jmeter的运行时。