-
Notifications
You must be signed in to change notification settings - Fork 6
/
HelloFunction.java
114 lines (96 loc) · 3.84 KB
/
HelloFunction.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package hello.jmeter.functions;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import org.apache.jmeter.engine.util.CompoundVariable;
import org.apache.jmeter.samplers.SampleResult;
import org.apache.jmeter.samplers.Sampler;
import org.apache.jmeter.threads.JMeterVariables;
import org.apache.jmeter.util.JMeterUtils;
import org.apache.jmeter.functions.AbstractFunction;
import org.apache.jmeter.functions.InvalidVariableException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
* 创建一个自定义函数__hello, 实现: Hello xxx, today is xxx
*
* Create a custom function __hello to: Hello xxx, today is xxx
*/
public class HelloFunction extends AbstractFunction {
private static final List<String> desc = new LinkedList<>();
private static final String KEY = "__hello";
static {
desc.add("Hello Words");
desc.add("Name of variable in which to store the result (optional)");
}
private CompoundVariable varName;
private CompoundVariable helloWords;
public HelloFunction() {}
/**
* 核心功能
* 更多关于execute:https://jmeter.apache.org/api/org/apache/jmeter/functions/Function.html#execute()
*
* Core features
* More info about the execute: https://jmeter.apache.org/api/org/apache/jmeter/functions/Function.html#execute()
*/
@Override
public String execute(SampleResult previousResult, Sampler currentSampler)
throws InvalidVariableException {
String hello = helloWords.execute().trim();
DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
Calendar cal = Calendar.getInstance();
Date date = cal.getTime();
String today = dateFormat.format(date);
String response = hello + ", today is " + today;
if (varName != null) {
JMeterVariables vars = getVariables();
final String varTrim = varName.execute().trim();
if (vars != null && varTrim.length() > 0){
vars.put(varTrim, response);
}
}
return response;
}
/**
* 为所有的参数保存值
* 更多关于setParameters:http://jmeter.apache.org/api/org/apache/jmeter/functions/Function.html#setParameters()
*
* Save all the values for the parameters in JMeter
* More info about the setParameters: http://jmeter.apache.org/api/org/apache/jmeter/functions/Function.html#setParameters()
*/
@Override
public void setParameters(Collection<CompoundVariable> parameters) throws InvalidVariableException {
checkParameterCount(parameters, 1, 2);
Object[] values = parameters.toArray();
helloWords = (CompoundVariable) values[0];
if (values.length>1){
varName = (CompoundVariable) values[1];
} else {
varName = null;
}
}
/**
* 在 Function Helper 中显示自定义的函数名
* 更多关于getReferenceKey:https://jmeter.apache.org/api/org/apache/jmeter/functions/Function.html#getReferenceKey()
*
* Show the custom function name in Function Helper
* More info about the getReferenceKey: https://jmeter.apache.org/api/org/apache/jmeter/functions/Function.html#getReferenceKey()
*/
@Override
public String getReferenceKey() {
return KEY;
}
/**
* 在 Function Helper 中显示每个参数的描述
* 更多关于getArgumentDesc:https://jmeter.apache.org/api/org/apache/jmeter/functions/Function.html#getReferenceKey()
*
* Show the parameter description in Function Helper
* More info about the getArgumentDesc: https://jmeter.apache.org/api/org/apache/jmeter/functions/Function.html#getReferenceKey()
*/
@Override
public List<String> getArgumentDesc() {
return desc;
}
}