Line 0
Link Here
|
|
|
1 |
/* |
2 |
* Licensed to the Apache Software Foundation (ASF) under one or more |
3 |
* contributor license agreements. See the NOTICE file distributed with |
4 |
* this work for additional information regarding copyright ownership. |
5 |
* The ASF licenses this file to You under the Apache License, Version 2.0 |
6 |
* (the "License"); you may not use this file except in compliance with |
7 |
* the License. You may obtain a copy of the License at |
8 |
* |
9 |
* http://www.apache.org/licenses/LICENSE-2.0 |
10 |
* |
11 |
* Unless required by applicable law or agreed to in writing, software |
12 |
* distributed under the License is distributed on an "AS IS" BASIS, |
13 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
14 |
* See the License for the specific language governing permissions and |
15 |
* limitations under the License. |
16 |
* |
17 |
*/ |
18 |
|
19 |
package org.apache.jmeter.visualizers.backend.apdex; |
20 |
|
21 |
import java.io.BufferedWriter; |
22 |
import java.io.File; |
23 |
import java.io.FileOutputStream; |
24 |
import java.io.OutputStreamWriter; |
25 |
import java.io.Writer; |
26 |
import java.util.HashMap; |
27 |
import java.util.HashSet; |
28 |
import java.util.List; |
29 |
import java.util.Map; |
30 |
import java.util.Set; |
31 |
import java.util.concurrent.atomic.AtomicLong; |
32 |
|
33 |
import org.apache.commons.lang3.StringUtils; |
34 |
import org.apache.jmeter.config.Arguments; |
35 |
import org.apache.jmeter.samplers.SampleResult; |
36 |
import org.apache.jmeter.util.JMeterUtils; |
37 |
import org.apache.jmeter.visualizers.SamplingStatCalculator; |
38 |
import org.apache.jmeter.visualizers.backend.AbstractBackendListenerClient; |
39 |
import org.apache.jmeter.visualizers.backend.BackendListenerContext; |
40 |
import org.apache.jorphan.logging.LoggingManager; |
41 |
import org.apache.jorphan.util.JOrphanUtils; |
42 |
import org.apache.log.Logger; |
43 |
|
44 |
import freemarker.template.Configuration; |
45 |
import freemarker.template.Template; |
46 |
import freemarker.template.TemplateExceptionHandler; |
47 |
|
48 |
/** |
49 |
* APDEX Computer |
50 |
* @since 2.13 |
51 |
*/ |
52 |
public class ApdexBackendListenerClient extends AbstractBackendListenerClient { |
53 |
private static final Logger LOGGER = LoggingManager.getLoggerForClass(); |
54 |
private static final String SEPARATOR = ";"; //$NON-NLS-1$ |
55 |
|
56 |
/** |
57 |
* T : Threshold for satistied users. |
58 |
* Response times <= satisfiedResponseTimeMS count towards satisfying |
59 |
*/ |
60 |
private long satisfiedResponseTimeMS; |
61 |
/** |
62 |
* F : Threshold for tolerated users. |
63 |
* Response times > satisfiedResponseTimeMS and <= toleratedResponseTimeMS count towards tolerated response times |
64 |
* If user does not provide it, it is equal to 4xT |
65 |
*/ |
66 |
private long toleratedResponseTimeMS; |
67 |
/** |
68 |
* Semicolon separated samplers to include in results |
69 |
*/ |
70 |
private String samplersList = ""; //$NON-NLS-1$ |
71 |
/** |
72 |
* Set of samplers to filter, computed from samplersList |
73 |
*/ |
74 |
private Set<String> samplersToFilter; |
75 |
|
76 |
/** |
77 |
* Number of Successful Responses for samplers in samplersToFilter with Response Time <= T |
78 |
*/ |
79 |
private AtomicLong numberOfSatistiedResponses = new AtomicLong(0); |
80 |
/** |
81 |
* Number of Successful Responses for samplers in samplersToFilter with Response Time > T and <= F |
82 |
*/ |
83 |
private AtomicLong numberOfToleratedResponses = new AtomicLong(0); |
84 |
/** |
85 |
* Number of Responses for samplers in samplersToFilter (OK + KO) |
86 |
*/ |
87 |
private AtomicLong totalResponses = new AtomicLong(0); |
88 |
private String resultsFile; |
89 |
private String reportFile; |
90 |
public ApdexBackendListenerClient() { |
91 |
super(); |
92 |
} |
93 |
|
94 |
|
95 |
/** |
96 |
* @return the samplersList |
97 |
*/ |
98 |
public String getSamplersList() { |
99 |
return samplersList; |
100 |
} |
101 |
|
102 |
/** |
103 |
* @param samplersList the samplersList to set |
104 |
*/ |
105 |
public void setSamplersList(String samplersList) { |
106 |
this.samplersList = samplersList; |
107 |
} |
108 |
|
109 |
@Override |
110 |
public void handleSampleResults(List<SampleResult> sampleResults, |
111 |
BackendListenerContext context) { |
112 |
// NOOP |
113 |
} |
114 |
|
115 |
@Override |
116 |
public void setupTest(BackendListenerContext context) throws Exception { |
117 |
String satisfiedResponseTimeAsString = context.getParameter("satisfiedResponseTimeMS", ""); |
118 |
if(!StringUtils.isEmpty(satisfiedResponseTimeAsString)) { |
119 |
try { |
120 |
satisfiedResponseTimeMS = Long.parseLong(satisfiedResponseTimeAsString); |
121 |
} catch (Exception e) { |
122 |
throw new IllegalArgumentException("Error parsing satisfiedResponseTimeMS '" |
123 |
+satisfiedResponseTimeAsString+"', message:"+e.getMessage()); |
124 |
} |
125 |
} else { |
126 |
throw new IllegalArgumentException("satisfiedResponseTimeMS is null or empty"); |
127 |
} |
128 |
|
129 |
String toleratedResponseTimeAsString = context.getParameter("toleratedResponseTimeMS", ""); |
130 |
if(!StringUtils.isEmpty(toleratedResponseTimeAsString)) { |
131 |
try { |
132 |
toleratedResponseTimeMS = Long.parseLong(toleratedResponseTimeAsString); |
133 |
} catch (Exception e) { |
134 |
throw new IllegalArgumentException("Error parsing toleratedResponseTimeMS '" |
135 |
+satisfiedResponseTimeAsString+"', message:"+e.getMessage()); |
136 |
} |
137 |
} else { |
138 |
toleratedResponseTimeMS = 4*satisfiedResponseTimeMS; |
139 |
} |
140 |
|
141 |
samplersList = context.getParameter("samplersList", ""); |
142 |
String[] samplers = samplersList.split(SEPARATOR); |
143 |
samplersToFilter = new HashSet<String>(); |
144 |
for (String samplerName : samplers) { |
145 |
samplersToFilter.add(samplerName.trim()); |
146 |
} |
147 |
|
148 |
resultsFile = context.getParameter("resultsFile"); |
149 |
reportFile = context.getParameter("reportFile"); |
150 |
|
151 |
numberOfSatistiedResponses.set(0); |
152 |
numberOfToleratedResponses.set(0); |
153 |
totalResponses.set(0); |
154 |
} |
155 |
|
156 |
@Override |
157 |
public void teardownTest(BackendListenerContext context) throws Exception { |
158 |
ReportVisualizer reportVisualizer = new ReportVisualizer(samplersToFilter, satisfiedResponseTimeMS, toleratedResponseTimeMS); |
159 |
ReportResultCollector collector = new ReportResultCollector(); |
160 |
collector.setFilename(resultsFile); |
161 |
collector.setListener(reportVisualizer); |
162 |
collector.loadExistingFile(); |
163 |
Map<String, SamplingStatCalculator> results = reportVisualizer.getResultsPerSampleLabel(); |
164 |
for (Map.Entry<String, SamplingStatCalculator> entry : results.entrySet()) { |
165 |
System.out.println(entry.getKey()+":"+entry.getValue()); |
166 |
} |
167 |
generateReport(reportFile, reportVisualizer); |
168 |
samplersToFilter.clear(); |
169 |
super.teardownTest(context); |
170 |
} |
171 |
|
172 |
private void generateReport(String reportFile, ReportVisualizer reportVisualizer) |
173 |
throws Exception { |
174 |
Configuration cfg = new Configuration(Configuration.VERSION_2_3_21); |
175 |
cfg.setDirectoryForTemplateLoading(new File(JMeterUtils.getJMeterHome(),"report_templates")); |
176 |
cfg.setDefaultEncoding("UTF-8"); |
177 |
cfg.setTemplateExceptionHandler(TemplateExceptionHandler.HTML_DEBUG_HANDLER); |
178 |
Writer writer = null; |
179 |
try { |
180 |
//Load template from source folder |
181 |
Template template = cfg.getTemplate("HTML_REPORT.ftl"); |
182 |
|
183 |
// Build the data-model |
184 |
Map<String, Object> data = new HashMap<String, Object>(); |
185 |
data.put("APDEX", "APDEX:"+Float.toString(reportVisualizer.computeApdex()) + " for thresholds T:"+satisfiedResponseTimeMS+"ms, F:"+toleratedResponseTimeMS |
186 |
+ "ms on samplers:"+samplersToFilter); |
187 |
Map<String, SamplingStatCalculator> map = reportVisualizer.getResultsPerSampleLabel(); |
188 |
SamplingStatCalculator total = map.get(ReportVisualizer.TOTAL_ROW_LABEL); |
189 |
data.put("TOTAL", total); |
190 |
map.remove(ReportVisualizer.TOTAL_ROW_LABEL); |
191 |
data.put("resultsPerSample", map); |
192 |
writer = new BufferedWriter(new OutputStreamWriter( |
193 |
new FileOutputStream(reportFile), "UTF-8")); |
194 |
// File output |
195 |
template.process(data, writer); |
196 |
} finally { |
197 |
JOrphanUtils.closeQuietly(writer); |
198 |
} |
199 |
} |
200 |
|
201 |
@Override |
202 |
public Arguments getDefaultParameters() { |
203 |
Arguments arguments = new Arguments(); |
204 |
arguments.addArgument("satisfiedResponseTimeMS", ""); |
205 |
arguments.addArgument("toleratedResponseTimeMS", ""); |
206 |
arguments.addArgument("samplersList", ""); |
207 |
arguments.addArgument("resultsFile", ""); |
208 |
arguments.addArgument("reportFile", ""); |
209 |
return arguments; |
210 |
} |
211 |
|
212 |
|
213 |
/* (non-Javadoc) |
214 |
* @see org.apache.jmeter.visualizers.backend.AbstractBackendListenerClient#createSampleResult(org.apache.jmeter.visualizers.backend.BackendListenerContext, org.apache.jmeter.samplers.SampleResult) |
215 |
*/ |
216 |
@Override |
217 |
public SampleResult createSampleResult(BackendListenerContext context, |
218 |
SampleResult result) { |
219 |
return result; |
220 |
} |
221 |
} |