-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathHelloJavaSampler.java
executable file
·126 lines (112 loc) · 3.88 KB
/
HelloJavaSampler.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
115
116
117
118
119
120
121
122
123
124
125
126
package hello.jmeter.hellojavasampler;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Properties;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient;
import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext;
import org.apache.jmeter.samplers.SampleResult;
// LoggingManager 在JMeter3.3已经弃用:https://jmeter.apache.org/api/deprecated-list.html
// LoggingManager is deprecated in JMeter 3.3: https://jmeter.apache.org/api/deprecated-list.html
// import org.apache.jorphan.logging.LoggingManager;
// import org.apache.log.Logger;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* 创建一个Java Sampler,生成: Hello xxx, today is xxx
*
* Create a Java Sampler to generate: Hello xxx, today is xxx
*/
public class HelloJavaSampler extends AbstractJavaSamplerClient {
// private static final Logger log = LoggingManager.getLoggerForClass();
private static final Logger log = LoggerFactory.getLogger(HelloJavaSampler.class);
private static final String ENCODING = "UTF-8";
private Properties props = new Properties();
/**
* hello words的参数
*
* Parameter for the hello words
*/
private static final String PARAMETER_HELLO_WORDS = "hello_words";
/**
* 测试前的准备工作,比如准备参数,初始化test client. JMeter对每一个thread只执行一次
* 这里实现了读取当前日期
*
* This is where you read test parameters and initialize your test client. JMeter calls this method only once for each test thread
* Here is used to get the current date
*/
@Override
public void setupTest(JavaSamplerContext context) {
DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
Calendar cal = Calendar.getInstance();
Date date = cal.getTime();
String today = dateFormat.format(date);
props.put("today", today);
}
/**
* 测试完成后执行清理工作
*
* Clear the mess after test
*/
@Override
public void teardownTest(JavaSamplerContext context) {
props.remove("today");
}
/**
* 设置参数的默认值,如果为空,则填null
*
* Set the default values for parameters, if empty, please set null
*/
@Override
public Arguments getDefaultParameters() {
Arguments defaultParameters = new Arguments();
defaultParameters.addArgument(PARAMETER_HELLO_WORDS, "Hello JMeter Plugin");
return defaultParameters;
}
/**
* 实现测试逻辑
*
* Fulfil the logic
*/
@Override
public SampleResult runTest(JavaSamplerContext context) {
log.info("Start the Hello Java Sampler");
SampleResult result = new SampleResult();
result.sampleStart();
try {
result.setDataEncoding(ENCODING);
result.setDataType(SampleResult.TEXT);
String hello = context.getParameter(PARAMETER_HELLO_WORDS);
String response = hello + ", today is " + props.get("today");
result.setSamplerData(response);
result.sampleEnd();
result.setSuccessful(true);
result.setResponseCodeOK();
result.setResponseMessage(response);
result.setResponseData(response, ENCODING);
} catch (Exception e) {
result.sampleEnd();
result.setSuccessful(false);
result.setResponseCode("500");
result.setResponseMessage("Exception: " + e);
result.setResponseData(getStackTrace(e), ENCODING);
}
return result;
}
/**
* 返回 stack trace
* Return the stack trace as a string.
*
* @param exception the exception containing the stack trace
* @return the stack trace
*/
private String getStackTrace(Exception exception) {
StringWriter stringWriter = new StringWriter();
exception.printStackTrace(new PrintWriter(stringWriter));
return stringWriter.toString();
}
}