Skip to content

Commit

Permalink
Merge pull request #21 from mariusz-zawadzki/feat/improve_performance…
Browse files Browse the repository at this point in the history
…_for_high_amount_of_publishers

Reduce latency for high density information points.
  • Loading branch information
PiotrDebicki-TomTom authored Nov 8, 2019
2 parents 55df2db + 249cade commit aec5c19
Show file tree
Hide file tree
Showing 31 changed files with 1,149 additions and 203 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
language: java
jdk: openjdk8
sudo: true
script:
- chmod +x ./gradlew
Expand Down
3 changes: 2 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ subprojects {
awaitility : '3.0.0',
commonslang : '3.7',
javassist : '3.22.0-GA',
bytebuddytests: '1.6.4'
bytebuddytests: '1.6.4',
disruptor: '3.4.2'
]
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@

package com.tomtom.james.it.standalone;

import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;

class Service {

private static final Random rnd = new Random();
private int fieldValue = 10;

int doSomething(String arg) {
Expand All @@ -38,15 +37,17 @@ static int doSomethingStatic(String arg) {
}

private static void throwAtRandom(double probability) {
double v = rnd.nextDouble();
double v = ThreadLocalRandom.current()
.nextDouble();
if (v < probability) {
throw new RuntimeException(v + " < " + probability);
}
}

private static void sleep() {
try {
Thread.sleep(rnd.nextInt(500));
Thread.sleep(ThreadLocalRandom.current()
.nextInt(500));
} catch (InterruptedException e) {
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ class InformationPointsSpec extends BaseJamesSpecification {
ips[0].className == "foo.bar.className"
ips[0].methodName == "methodName"
ips[0].sampleRate == 100
ips[0].successSampleRate == 100
ips[0].errorSampleRate == 100

}

Expand All @@ -74,6 +76,8 @@ class InformationPointsSpec extends BaseJamesSpecification {
ipsBeforeRemove[0].className == "foo.bar.className2"
ipsBeforeRemove[0].methodName == "methodName2"
ipsBeforeRemove[0].sampleRate == 100
ipsBeforeRemove[0].successSampleRate == 100
ipsBeforeRemove[0].errorSampleRate == 100

ipsAfterRemove.isEmpty()
}
Expand All @@ -99,6 +103,8 @@ class InformationPointsSpec extends BaseJamesSpecification {
ips[0].className == "foo.bar.className3"
ips[0].methodName == "methodName3"
ips[0].sampleRate == 5
ips[0].successSampleRate == 5
ips[0].errorSampleRate == 5
}

def "Should parse v1 json with owner and index" () {
Expand Down Expand Up @@ -127,7 +133,66 @@ class InformationPointsSpec extends BaseJamesSpecification {
ips[0].className == "foo.bar.className3"
ips[0].methodName == "methodName3"
ips[0].sampleRate == 5
ips[0].successSampleRate == 5
ips[0].errorSampleRate == 5
ips[0].metadata.get("owner") == "jenkins"
ips[0].metadata.get("index") == "test"
}

def "Should create information point with default errorSampleRate" () {
given:
def controller = JamesControllerProvider.get()
def json = '''{
"className": "foo.bar.className3",
"methodName": "methodName3",
"script": [
"// First line of Information Point script",
"// Second line of Information Point script"
],
"successSampleRate": 5,
"metadata": {
"owner": "jenkins",
"index": "test"
}
}'''

when:
controller.createInformationPoint(json)
def ips = controller.informationPoints

then:
ips.size() == 1
ips[0].className == "foo.bar.className3"
ips[0].methodName == "methodName3"
ips[0].successSampleRate == 5
ips[0].errorSampleRate == 100
}
def "Should create information point with default successSampleRate" () {
given:
def controller = JamesControllerProvider.get()
def json = '''{
"className": "foo.bar.className3",
"methodName": "methodName3",
"script": [
"// First line of Information Point script",
"// Second line of Information Point script"
],
"errorSampleRate": 5,
"metadata": {
"owner": "jenkins",
"index": "test"
}
}'''

when:
controller.createInformationPoint(json)
def ips = controller.informationPoints

then:
ips.size() == 1
ips[0].className == "foo.bar.className3"
ips[0].methodName == "methodName3"
ips[0].successSampleRate == 100
ips[0].errorSampleRate == 5
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.tomtom.james.common.api.informationpoint;

import java.util.Objects;

public class ExtendedInformationPoint extends InformationPoint {
// name of the class that contains body of method
protected String methodBodyClassName;
Expand All @@ -13,6 +15,8 @@ public ExtendedInformationPoint(InformationPoint informationPoint, String method
this.methodName = informationPoint.getMethodName();
this.className = informationPoint.getClassName();
this.sampleRate = informationPoint.getSampleRate();
this.successSampleRate = informationPoint.getSuccessSampleRate();
this.errorSampleRate = informationPoint.getErrorSampleRate();
this.script = informationPoint.getScript().orElse("");
this.methodBodyClassName = methodBodyClassName;
}
Expand All @@ -21,4 +25,23 @@ public String getMethodBodyClassName() {
return methodBodyClassName;
}

@Override
public boolean equals(final Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
if (!super.equals(o)) {
return false;
}
final ExtendedInformationPoint that = (ExtendedInformationPoint) o;
return Objects.equals(methodBodyClassName, that.methodBodyClassName);
}

@Override
public int hashCode() {
return Objects.hash(super.hashCode(), methodBodyClassName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@

package com.tomtom.james.common.api.informationpoint;

import java.util.*;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

Expand All @@ -25,7 +29,10 @@ public class InformationPoint {
protected String className;
protected String methodName;
protected String script;
protected Integer sampleRate;
//unused. left for backward compatibility
protected int sampleRate = 100;
protected double successSampleRate = 100;
protected double errorSampleRate = 100;
protected Metadata metadata;
protected Boolean requiresInitialContext = Boolean.FALSE;

Expand All @@ -46,7 +53,15 @@ public Optional<String> getScript() {
}

public int getSampleRate() {
return Optional.ofNullable(sampleRate).orElse(100);
return sampleRate;
}

public double getSuccessSampleRate() {
return successSampleRate;
}

public double getErrorSampleRate() {
return errorSampleRate;
}

public List<String> splittedScriptLines() {
Expand All @@ -72,8 +87,12 @@ public String toString() {

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
InformationPoint that = (InformationPoint) o;
return Objects.equals(className, that.className) &&
Objects.equals(methodName, that.methodName);
Expand All @@ -97,6 +116,8 @@ public static class Builder {
private String methodName;
private String script;
private Integer sampleRate;
private Double successSampleRate;
private Double errorSampleRate;
private Metadata metadata;
private Boolean requireInitialContext = Boolean.FALSE;

Expand Down Expand Up @@ -133,13 +154,33 @@ public Builder withScript(String script) {
}

public Builder withSampleRate(Integer sampleRate) {
if(sampleRate!=null && errorSampleRate != null && successSampleRate!= null){
throw new IllegalStateException("Cannot set sampleRate when successSampleRate or errorSampleRate is set.");
}
this.sampleRate = sampleRate;
return this;
}

public Builder withSuccessSampleRate(Double successSampleRate) {
if(sampleRate != null && successSampleRate!= null){
throw new IllegalStateException("Cannot set successSampleRate when sampleRate is set.");
}
this.successSampleRate = successSampleRate;
return this;
}

public Builder withErrorSampleRate(Double errorSampleRate) {
if(sampleRate != null && errorSampleRate!= null){
throw new IllegalStateException("Cannot set errorSampleRate when sampleRate is set.");
}
this.errorSampleRate = errorSampleRate;
return this;
}

public Builder withMetadata(Metadata metadata) {
if(metadata != null)
if(metadata != null) {
this.metadata.putAll(metadata);
}
return this;
}

Expand All @@ -148,6 +189,8 @@ public Builder copyOf(InformationPoint copyFrom) {
this.methodName = copyFrom.methodName;
this.script = copyFrom.script;
this.sampleRate = copyFrom.sampleRate;
this.successSampleRate = copyFrom.successSampleRate;
this.errorSampleRate = copyFrom.errorSampleRate;
this.metadata.putAll(copyFrom.metadata);
this.requireInitialContext = copyFrom.requiresInitialContext;
return this;
Expand All @@ -158,7 +201,9 @@ public InformationPoint build() {
ip.className = Objects.requireNonNull(className);
ip.methodName = Objects.requireNonNull(methodName);
ip.script = script;
ip.sampleRate = sampleRate;
ip.sampleRate = Optional.ofNullable(sampleRate).orElse(100);
ip.successSampleRate = Optional.ofNullable(successSampleRate).orElse((double)ip.sampleRate);
ip.errorSampleRate = Optional.ofNullable(errorSampleRate).orElse((double)ip.sampleRate);
ip.metadata.putAll(metadata);
ip.requiresInitialContext = requireInitialContext;
return ip;
Expand Down
2 changes: 2 additions & 0 deletions james-agent/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ shadowJar {
relocate 'com.google', 'com.tomtom.james.repackaged.com.google'
relocate 'com.fasterxml.jackson', 'com.tomtom.james.repackaged.com.fasterxml.jackson'
relocate 'org.yaml.snakeyaml', 'com.tomtom.james.repackaged.org.yaml.snakeyaml'
relocate 'com.lmax.disruptor', 'com.tomtom.james.repackaged.com.lmax.disruptor'
}

dependencies {
Expand All @@ -51,6 +52,7 @@ dependencies {
compile group: 'org.codehaus.groovy', name: 'groovy-xml', version: versions.groovy
compile group: 'org.javassist', name: 'javassist', version: versions.javassist
compile group: 'org.apache.commons', name: 'commons-lang3', version: versions.commonslang
compile group: 'com.lmax', name: 'disruptor', version: versions.disruptor

testCompile group: 'org.spockframework', name: 'spock-core', version: versions.spock
testCompile group: 'org.awaitility', name: 'awaitility', version: versions.awaitility
Expand Down
Loading

0 comments on commit aec5c19

Please sign in to comment.