forked from pinpoint-apm/pinpoint
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[pinpoint-apm#4044] Add apdexScore in getServerMapDataV2
- Loading branch information
Showing
4 changed files
with
130 additions
and
3 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
web/src/main/java/com/navercorp/pinpoint/web/applicationmap/histogram/ApdexScore.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.navercorp.pinpoint.web.applicationmap.histogram; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import com.fasterxml.jackson.annotation.JsonValue; | ||
|
||
import java.util.Objects; | ||
|
||
public class ApdexScore { | ||
private final double apdexScore; | ||
|
||
public ApdexScore(Histogram histogram) { | ||
Objects.requireNonNull(histogram, "histogram"); | ||
this.apdexScore = floor(calculateApdexScore(histogram)); | ||
} | ||
|
||
private double calculateApdexScore(Histogram histogram) { | ||
final long satisfiedCount = histogram.getFastCount(); | ||
final long toleratingCount = histogram.getNormalCount(); | ||
final long totalCount = histogram.getTotalCount(); | ||
|
||
if ((totalCount - (satisfiedCount + (toleratingCount >> 1))) < (totalCount >> 10) && (totalCount - (satisfiedCount + (toleratingCount >> 1))) > 0) { | ||
return 0.999; | ||
} | ||
|
||
final double score = ((satisfiedCount + 0.5 * toleratingCount) / totalCount); | ||
|
||
return score; | ||
} | ||
|
||
@JsonProperty | ||
public double getApdexScore() { | ||
return this.apdexScore; | ||
} | ||
|
||
private double floor(double value) { | ||
return (Math.floor((value) * 1000) / 1000); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
web/src/test/java/com/navercorp/pinpoint/web/applicationmap/histogram/ApdexScoreTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package com.navercorp.pinpoint.web.applicationmap.histogram; | ||
|
||
import com.fasterxml.jackson.core.JsonFactory; | ||
import com.fasterxml.jackson.core.JsonGenerator; | ||
import com.fasterxml.jackson.databind.JsonSerializer; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.SerializerProvider; | ||
import com.fasterxml.jackson.databind.util.NameTransformer; | ||
import com.navercorp.pinpoint.common.trace.ServiceType; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
import java.io.IOException; | ||
import java.io.StringWriter; | ||
import java.io.Writer; | ||
import java.util.Objects; | ||
|
||
public class ApdexScoreTest { | ||
|
||
private static final ObjectMapper MAPPER = new ObjectMapper(); | ||
private final Logger logger = LogManager.getLogger(this.getClass()); | ||
|
||
@Test | ||
public void getApdexScore() { | ||
Histogram histogram = new Histogram(ServiceType.STAND_ALONE); | ||
|
||
histogram.addCallCount((short) 1000, 100); | ||
Assert.assertEquals(1.00, new ApdexScore(histogram).getApdexScore(), 0.001); | ||
|
||
histogram.addCallCount((short) 100000, 100); | ||
Assert.assertEquals(0.500, new ApdexScore(histogram).getApdexScore(), 0.001); | ||
} | ||
|
||
@Test | ||
public void getApdexScore_floatingPoint() { | ||
Histogram histogram = new Histogram(ServiceType.STAND_ALONE); | ||
histogram.addCallCount((short) 1000, Long.MAX_VALUE - 1); | ||
histogram.addCallCount((short) 100000, 1); | ||
|
||
Assert.assertEquals(0.999, new ApdexScore(histogram).getApdexScore(), 0.001); | ||
} | ||
|
||
@Test | ||
public void getApdexScore_format() throws IOException { | ||
Writer jsonWriter = new StringWriter(); | ||
JsonGenerator jgen = new JsonFactory().createGenerator(jsonWriter); | ||
ObjectMapper objectMapper = new ObjectMapper(); | ||
jgen.setCodec(objectMapper); | ||
SerializerProvider provider = objectMapper.getSerializerProvider(); | ||
|
||
Histogram histogram = new Histogram(ServiceType.STAND_ALONE); | ||
histogram.addCallCount((short) 1000, 100); | ||
|
||
// jgen.writeStartObject(); | ||
jgen.writeObject(new ApdexScore(histogram)); | ||
// jgen.writeObjectField("apdexScore", new ApdexScore(histogram)); | ||
// jgen.writeEndObject(); | ||
jgen.close(); | ||
|
||
Assert.assertEquals("{\"apdexScore\":1.0}", jsonWriter.toString()); | ||
} | ||
|
||
} |