Skip to content

Commit 50680d8

Browse files
Merge branch 'master' into master
2 parents e738c87 + 8d75e50 commit 50680d8

File tree

3 files changed

+40
-10
lines changed

3 files changed

+40
-10
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<modelVersion>4.0.0</modelVersion>
44
<groupId>net.delirius</groupId>
55
<artifactId>jmeter.backendlistener.elasticsearch</artifactId>
6-
<version>2.0.2</version>
6+
<version>2.1.0-SNAPSHOT</version>
77
<packaging>jar</packaging>
88
<name>jmeter.backendlistener.elasticsearch</name>
99
<url>http://maven.apache.org</url>

src/main/java/net/delirius/jmeter/backendlistener/elasticsearch/ElasticsearchBackend.java

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
import java.net.InetAddress;
44
import java.text.ParseException;
55
import java.text.SimpleDateFormat;
6+
import java.time.LocalDate;
7+
import java.time.LocalDateTime;
8+
import java.time.format.DateTimeFormatter;
69
import java.util.Calendar;
710
import java.util.Date;
811
import java.util.HashMap;
@@ -151,10 +154,18 @@ public Map<String, Object> getElasticData(SampleResult sr, BackendListenerContex
151154
jsonObject.put("URL", sr.getURL());
152155
jsonObject.put("Timestamp", sdf.format(new Date(sr.getTimeStamp())));
153156
jsonObject.put(ElasticsearchBackend.BUILD_NUMBER, this.buildNumber);
154-
Date elapsedDate = getElapsedDate();
155-
if(elapsedDate != null) {
156-
jsonObject.put("ElapsedTime", elapsedDate);
157+
158+
// If built from Jenkins, add the hard-coded version to be able to compare response time
159+
// of two builds over the elapsed time
160+
if(this.buildNumber != 0) {
161+
Date elapsedTimeComparison = getElapsedTime(true);
162+
if(elapsedTimeComparison != null)
163+
jsonObject.put("ElapsedTimeComparison", elapsedTimeComparison);
157164
}
165+
166+
Date elapsedTime = getElapsedTime(false);
167+
if(elapsedTime != null)
168+
jsonObject.put("ElapsedTime", elapsedTime);
158169
jsonObject.put("ResponseCode", (sr.isResponseCodeOK() &&
159170
StringUtils.isNumeric(sr.getResponseCode())) ?
160171
sr.getResponseCode() : context.getParameter(ES_STATUS_CODE));
@@ -173,12 +184,14 @@ public Map<String, Object> getElasticData(SampleResult sr, BackendListenerContex
173184
assertionArray[i] = assertionMap;
174185
i++;
175186
}
187+
jsonObject.put("AssertionResults", assertionArray);
176188
}
177189

178190
return jsonObject;
179191
}
180192

181-
public Date getElapsedDate() {
193+
public Date getElapsedTime(boolean forBuildComparison) {
194+
String sElapsed;
182195
//Calculate the elapsed time (Starting from midnight on a random day - enables us to compare of two loads over their duration)
183196
long start = JMeterContextService.getTestStartTime();
184197
long end = System.currentTimeMillis();
@@ -190,10 +203,20 @@ public Date getElapsedDate() {
190203
cal.set(Calendar.HOUR_OF_DAY, 0); //If there is more than an hour of data, the number of minutes/seconds will increment this
191204
cal.set(Calendar.MINUTE, (int) minutes);
192205
cal.set(Calendar.SECOND, (int) seconds);
193-
String sElapsed = String.format("2017-01-01 %02d:%02d:%02d",
194-
cal.get(Calendar.HOUR_OF_DAY),
195-
cal.get(Calendar.MINUTE),
196-
cal.get(Calendar.SECOND));
206+
207+
if(forBuildComparison) {
208+
sElapsed = String.format("2017-01-01 %02d:%02d:%02d",
209+
cal.get(Calendar.HOUR_OF_DAY),
210+
cal.get(Calendar.MINUTE),
211+
cal.get(Calendar.SECOND));
212+
} else {
213+
sElapsed = String.format("%s %02d:%02d:%02d",
214+
DateTimeFormatter.ofPattern("yyyy-mm-dd").format(LocalDateTime.now()),
215+
cal.get(Calendar.HOUR_OF_DAY),
216+
cal.get(Calendar.MINUTE),
217+
cal.get(Calendar.SECOND));
218+
}
219+
197220
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-mm-dd HH:mm:ss");
198221
try {
199222
return formatter.parse(sElapsed);

src/test/java/net/delirius/jmeter/backendlistener/elasticsearch/TestElasticSearchBackend.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,14 @@ public void setUp() throws Exception {
2929
@Test
3030
public void testGetElapsedTime() throws Exception {
3131
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
32-
Date testDate = this.instance.getElapsedDate();
32+
Date testDate = this.instance.getElapsedTime(false);
33+
assertNotNull("testDate = " + sdf.format(testDate), sdf.format(testDate));
34+
}
35+
36+
@Test
37+
public void testGetElapsedTimeJenkins() throws Exception {
38+
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
39+
Date testDate = this.instance.getElapsedTime(true);
3340
assertNotNull("testDate = " + sdf.format(testDate), sdf.format(testDate));
3441
}
3542
}

0 commit comments

Comments
 (0)