Skip to content

Commit 17b2197

Browse files
storozhukBMRobWin
authored andcommitted
Issue ReactiveX#30 Switch from jacoco to cobertura
* test coverage added + jacoco excludes for benchmark classes * Issue#15 circular fifo bufer optimization (ReactiveX#29) * Issue#15 New optimized implementation of CircularFifoBuffer * Updated documentation * Updated documentation * Updated documentation * Updated documentation * Updated documentation * Updated documentation * Updated documentation * Updated documentation * Updated documentation * Updated documentation * Updated documentation * Move all test reporting to cobertura * Time alignment for decorator test * Additional tests and time alignment in spin loop
1 parent 73d7f75 commit 17b2197

15 files changed

+573
-109
lines changed

.travis.yml

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,33 @@
11
language: java
22
jdk:
33
- oraclejdk8
4+
45
before_install:
56
- chmod +x gradlew
7+
8+
script:
9+
- ./gradlew clean check allTests -x test
10+
611
after_success:
7-
- ./gradlew jacocoTestReport coveralls
12+
- ./gradlew coberturaReport coveralls
813
- ./gradlew artifactoryPublish -PbintrayUsername="${BINTRAY_USER}" -PbintrayApiKey="${BINTRAY_KEY}"
14+
15+
before_cache:
16+
- rm -f $HOME/.gradle/caches/modules-2/modules-2.lock
17+
- rm -fr $HOME/.gradle/caches/*/plugin-resolution/
18+
cache:
19+
directories:
20+
- $HOME/.gradle/caches/
21+
- $HOME/.gradle/wrapper/
22+
923
notifications:
1024
webhooks:
1125
urls:
1226
- https://webhooks.gitter.im/e/088d6d0266532abc60ee
1327
on_success: change
1428
on_failure: always
1529
on_start: false
30+
1631
env:
1732
global:
1833
- secure: OpcatJV2E2u1Li7KXacvTq6O+c/2TZor7dHl2dlo/8jDgGOZFK4rsZlTVWWqcwP1lOr5s5yOsysrJZ+3ZzFFpJ4VsTKyNQZEwzl7si40l4I29NU9fiySFSGOCYqIclXHh2lJgLhXPZonzr5KQVcEcRSNQHLDO22BVf8uUX8WOqM/ZMZyjQkYzgBfhaHoXnlRxuGoWWEolyBdKHf4Cr480u3Ep//7eZwKK/JpkM8DTu63TnjDh/Tpao95LQNY8RaRn7NAom6A0IlGR9gDLPnCNlziUPxHAL2KbUL4ePWICg/wYTiTvnh1D0gNGGHSC/jRfYE8BzBrXEwD9JUGkT+yMhUaWj+5/hxQbBivtGGuTrzMoJchiaBGjK2r17Xao4nkQF2MjsSJIzh5uE/td88tSrAVM1W0Ee7TlbtArR0knbvzZt4YyZjv26m0dEHmDx9Q+g1cjWKsCiPXlSvc8jOOoNlaq0dk8/bRu6D7yaHsxGhAokxcf/qzTF8zPp3ODrY5M6f5l4LFN8j7STFXt4GeR/B6W6uPL0JrPQhq5I54SJPGIv7amYv/QnmTMxLg1rCRA/Rnngz9IPKNzFcpNVVPL95v/W76xlk6UvCvm1aFBQd7KQ2qgGMZgSlxv5FX77Tzf3C97VNVm6JbBfkJEpIWiR1CUJAjWR6u2avS3jPShTk=

build.gradle

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ buildscript {
77
}
88
}
99
dependencies {
10-
classpath 'org.kt3k.gradle.plugin:coveralls-gradle-plugin:2.0.1'
10+
classpath 'net.saliman:gradle-cobertura-plugin:2.3.2'
11+
classpath 'org.kt3k.gradle.plugin:coveralls-gradle-plugin:2.7.1'
1112
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.2'
1213
classpath 'me.champeau.gradle:jmh-gradle-plugin:0.3.1'
1314
classpath 'com.github.jengelman.gradle.plugins:shadow:1.2.0'
@@ -20,7 +21,7 @@ buildscript {
2021
apply plugin: 'idea'
2122
apply plugin: 'java'
2223
apply plugin: 'maven-publish'
23-
apply plugin: 'jacoco'
24+
apply plugin: 'net.saliman.cobertura'
2425
apply plugin: 'com.github.kt3k.coveralls'
2526
apply plugin: 'com.jfrog.bintray'
2627
apply plugin: "com.jfrog.artifactory"
@@ -81,6 +82,12 @@ dependencies {
8182
jmh "org.openjdk.jmh:jmh-generator-annprocess:1.12"
8283
}
8384

85+
test {
86+
exclude 'io/github/robwin/circularbuffer/concurrent/'
87+
}
88+
89+
task allTests(type: Test) {}
90+
8491
task sourcesJar(type: Jar, dependsOn: classes) {
8592
classifier = 'sources'
8693
from sourceSets.main.allSource
@@ -96,11 +103,14 @@ artifacts {
96103
archives javadocJar
97104
}
98105

99-
jacocoTestReport {
100-
reports {
101-
xml.enabled = true // coveralls plugin depends on xml format report
102-
html.enabled = true
103-
}
106+
cobertura {
107+
coverageFormats = ['html', 'xml']
108+
// afterEvaluate {
109+
// classDirectories = files(classDirectories.files.collect {
110+
// fileTree(dir: it,
111+
// exclude: ['**/**Benchmark**'])
112+
// })
113+
// }
104114
}
105115

106116
tasks.coveralls {
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*
2+
*
3+
* Copyright 2016 Robert Winkler and Bohdan Storozhuk
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* 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 io.github.robwin.circularbuffer;
20+
21+
22+
import javaslang.collection.List;
23+
import javaslang.control.Option;
24+
import org.openjdk.jmh.annotations.Benchmark;
25+
import org.openjdk.jmh.annotations.BenchmarkMode;
26+
import org.openjdk.jmh.annotations.Fork;
27+
import org.openjdk.jmh.annotations.Group;
28+
import org.openjdk.jmh.annotations.GroupThreads;
29+
import org.openjdk.jmh.annotations.Measurement;
30+
import org.openjdk.jmh.annotations.Mode;
31+
import org.openjdk.jmh.annotations.OutputTimeUnit;
32+
import org.openjdk.jmh.annotations.Scope;
33+
import org.openjdk.jmh.annotations.Setup;
34+
import org.openjdk.jmh.annotations.State;
35+
import org.openjdk.jmh.annotations.Warmup;
36+
import org.openjdk.jmh.infra.Blackhole;
37+
38+
import java.util.concurrent.TimeUnit;
39+
40+
/**
41+
* @author bstorozhuk
42+
*/
43+
@State(Scope.Benchmark)
44+
@OutputTimeUnit(TimeUnit.NANOSECONDS)
45+
@BenchmarkMode(Mode.AverageTime)
46+
public class CircularBufferBenchmark {
47+
public static final int FORK_COUNT = 2;
48+
private static final int WARMUP_COUNT = 10;
49+
private static final int ITERATION_COUNT = 10;
50+
private static final int CAPACITY = 10;
51+
private CircularFifoBuffer<Object> circularFifoBuffer;
52+
private Object event;
53+
54+
@Setup
55+
public void setUp() {
56+
event = new Object();
57+
circularFifoBuffer = new ConcurrentCircularFifoBuffer<>(CAPACITY);
58+
}
59+
60+
@Benchmark
61+
@Warmup(iterations = WARMUP_COUNT)
62+
@Fork(value = FORK_COUNT)
63+
@Measurement(iterations = ITERATION_COUNT)
64+
@Group("circularBuffer")
65+
@GroupThreads(1)
66+
public void circularBufferAddEvent() {
67+
circularFifoBuffer.add(event);
68+
}
69+
70+
@Benchmark
71+
@Warmup(iterations = WARMUP_COUNT)
72+
@Fork(value = FORK_COUNT)
73+
@Measurement(iterations = ITERATION_COUNT)
74+
@Group("circularBuffer")
75+
@GroupThreads(1)
76+
public void circularBufferToList(Blackhole bh) {
77+
List<Object> events = circularFifoBuffer.toList();
78+
bh.consume(events);
79+
}
80+
81+
@Benchmark
82+
@Warmup(iterations = WARMUP_COUNT)
83+
@Fork(value = FORK_COUNT)
84+
@Measurement(iterations = ITERATION_COUNT)
85+
@Group("circularBuffer")
86+
@GroupThreads(1)
87+
public void circularBufferSize(Blackhole bh) {
88+
int size = circularFifoBuffer.size();
89+
bh.consume(size);
90+
}
91+
92+
@Benchmark
93+
@Warmup(iterations = WARMUP_COUNT)
94+
@Fork(value = FORK_COUNT)
95+
@Measurement(iterations = ITERATION_COUNT)
96+
@Group("circularBuffer")
97+
@GroupThreads(1)
98+
public void circularBufferTakeEvent(Blackhole bh) {
99+
Option<Object> event = circularFifoBuffer.take();
100+
bh.consume(event);
101+
}
102+
}

src/main/java/io/github/robwin/circuitbreaker/CircuitBreakerConfig.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public class CircuitBreakerConfig {
3737
private int ringBufferSizeInClosedState = DEFAULT_RING_BUFFER_SIZE_IN_CLOSED_STATE;
3838
private Duration waitDurationInOpenState = Duration.ofSeconds(DEFAULT_WAIT_DURATION_IN_OPEN_STATE);
3939
// The default exception predicate counts all exceptions as failures.
40-
private Predicate<Throwable> recordFailurePredicate = (exception) -> true;
40+
private Predicate<? super Throwable> recordFailurePredicate = (exception) -> true;
4141

4242
private CircuitBreakerConfig(){
4343
}
@@ -58,7 +58,7 @@ public int getRingBufferSizeInClosedState() {
5858
return ringBufferSizeInClosedState;
5959
}
6060

61-
public Predicate<Throwable> getRecordFailurePredicate() {
61+
public Predicate<? super Throwable> getRecordFailurePredicate() {
6262
return recordFailurePredicate;
6363
}
6464

@@ -158,7 +158,7 @@ public Builder ringBufferSizeInClosedState(int ringBufferSizeInClosedState) {
158158
* @param predicate the Predicate which evaluates if an exception should be recorded as a failure and thus trigger the CircuitBreaker
159159
* @return the CircuitBreakerConfig.Builder
160160
*/
161-
public Builder recordFailure(Predicate<Throwable> predicate) {
161+
public Builder recordFailure(Predicate<? super Throwable> predicate) {
162162
config.recordFailurePredicate = predicate;
163163
return this;
164164
}

src/main/java/io/github/robwin/circuitbreaker/event/CircuitBreakerOnErrorEvent.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
/**
2424
* A CircuitBreakerEvent which informs that an error has been recorded
2525
*/
26-
public class CircuitBreakerOnErrorEvent extends AbstractCircuitBreakerEvent{
26+
public class CircuitBreakerOnErrorEvent extends AbstractCircuitBreakerEvent {
2727

2828
private final Throwable throwable;
2929
private final Duration elapsedDuration;
@@ -48,12 +48,12 @@ public Type getEventType() {
4848
}
4949

5050
@Override
51-
public String toString(){
51+
public String toString() {
5252
return String.format("%s: CircuitBreaker '%s' recorded an error: '%s'. Elapsed time: %s ms",
53-
getCreationTime(),
54-
getCircuitBreakerName(),
55-
throwable.toString(),
56-
elapsedDuration.toMillis());
53+
getCreationTime(),
54+
getCircuitBreakerName(),
55+
getThrowable().toString(),
56+
getElapsedDuration().toMillis());
5757
}
5858

5959

src/main/java/io/github/robwin/circuitbreaker/event/CircuitBreakerOnIgnoredErrorEvent.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
/**
2424
* A CircuitBreakerEvent which informs that an error has been ignored
2525
*/
26-
public class CircuitBreakerOnIgnoredErrorEvent extends AbstractCircuitBreakerEvent{
26+
public class CircuitBreakerOnIgnoredErrorEvent extends AbstractCircuitBreakerEvent {
2727

2828
private final Throwable throwable;
2929
private final Duration elapsedDuration;
@@ -48,11 +48,11 @@ public Type getEventType() {
4848
}
4949

5050
@Override
51-
public String toString(){
51+
public String toString() {
5252
return String.format("%s: CircuitBreaker '%s' has ignored an error: '%s'. Elapsed time: %s ms",
53-
getCreationTime(),
54-
getCircuitBreakerName(),
55-
throwable.toString(),
56-
elapsedDuration.toMillis());
53+
getCreationTime(),
54+
getCircuitBreakerName(),
55+
getThrowable().toString(),
56+
getElapsedDuration().toMillis());
5757
}
5858
}

src/main/java/io/github/robwin/circuitbreaker/event/CircuitBreakerOnSuccessEvent.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
/**
2424
* A CircuitBreakerEvent which informs that a success has been recorded
2525
*/
26-
public class CircuitBreakerOnSuccessEvent extends AbstractCircuitBreakerEvent{
26+
public class CircuitBreakerOnSuccessEvent extends AbstractCircuitBreakerEvent {
2727

2828
private final Duration elapsedDuration;
2929

@@ -42,10 +42,10 @@ public Type getEventType() {
4242
}
4343

4444
@Override
45-
public String toString(){
45+
public String toString() {
4646
return String.format("%s: CircuitBreaker '%s' recorded a successful call. Elapsed time: %s ms",
47-
getCreationTime(),
48-
getCircuitBreakerName(),
49-
elapsedDuration.toMillis());
47+
getCreationTime(),
48+
getCircuitBreakerName(),
49+
getElapsedDuration().toMillis());
5050
}
5151
}

src/main/java/io/github/robwin/ratelimiter/internal/AtomicRateLimiter.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,14 @@ public long getNanosToWait() {
329329
return estimatedState.nanosToWait;
330330
}
331331

332+
/**
333+
* @return estimated current cycle
334+
*/
335+
public long getCycle() {
336+
State currentState = state.get();
337+
State estimatedState = calculateNextState(-1, currentState);
338+
return estimatedState.activeCycle;
339+
}
332340
}
333341

334342
/**

0 commit comments

Comments
 (0)