Skip to content

Commit

Permalink
Fixing statistics reporting when evaluation count is 0 (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
noconnor authored Nov 15, 2018
1 parent 2942a9e commit 56f8fa2
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 9 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ apply plugin: 'jacoco'
group = 'com.github.noconnor'
sourceCompatibility = 1.8
// http://semver.org/
version = '1.9.1'
version = '1.9.2'

repositories {
mavenCentral()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package com.github.noconnor.junitperf.statistics.providers;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
import com.github.noconnor.junitperf.statistics.StatisticsCalculator;
import org.apache.commons.math.stat.descriptive.DescriptiveStatistics;
import org.apache.commons.math.stat.descriptive.SynchronizedDescriptiveStatistics;
import com.github.noconnor.junitperf.statistics.StatisticsCalculator;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;

public class DescriptiveStatisticsCalculator implements StatisticsCalculator {

Expand Down Expand Up @@ -48,27 +49,33 @@ public long getEvaluationCount() {

@Override
public float getLatencyPercentile(int percentile, TimeUnit unit) {
return (float)statistics.getPercentile((double)(percentile)) / unit.toNanos(1);
float value = (float) statistics.getPercentile((double) (percentile));
return value > 0 ? value / unit.toNanos(1) : 0;
}

@Override
public float getMaxLatency(TimeUnit unit) {
return (float)statistics.getMax() / unit.toNanos(1);
float max = (float) statistics.getMax();
return max > 0 ? max / unit.toNanos(1) : 0;
}

@Override
public float getMinLatency(TimeUnit unit) {
return (float)statistics.getMin() / unit.toNanos(1);
float min = (float) statistics.getMin();
return min > 0 ? min / unit.toNanos(1) : 0;
}

@Override
public float getMeanLatency(TimeUnit unit) {
return (float)statistics.getMean() / unit.toNanos(1);
float mean = (float) statistics.getMean();
return mean > 0 ? mean / unit.toNanos(1) : 0;
}

@Override
public float getErrorPercentage() {
return ((float)errorCount.get() / (float)evaluationCount.get()) * 100;
float evalCount = evaluationCount.get();
float errCount = errorCount.get();
return evalCount > 0 ? (errCount / evalCount) * 100 : 0;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,30 @@ public void whenCallingGetPercentageError_thenPercentageErrorShouldBeCalculated(
evaluator.incrementEvaluationCount();
assertThat(evaluator.getErrorPercentage(), is(50F));
}

@Test
public void whenEvaluationCountIsZero_andGetPercentageErrorIsCalled_thenZeroShouldBeReturned() {
assertThat(evaluator.getErrorPercentage(), is(0F));
}

@Test
public void whenMeanLatencyIsZero_andGetMeanLatencyIsCalled_thenZeroShouldBeReturned() {
assertThat(evaluator.getMeanLatency(MILLISECONDS), is(0F));
}

@Test
public void whenMinLatencyIsZero_andGetMinLatencyIsCalled_thenZeroShouldBeReturned() {
assertThat(evaluator.getMinLatency(MILLISECONDS), is(0F));
}

@Test
public void whenMaxLatencyIsZero_andGetMaxLatencyIsCalled_thenZeroShouldBeReturned() {
assertThat(evaluator.getMaxLatency(MILLISECONDS), is(0F));
}

@Test
public void whenLatencyPercentilesAreZero_andGetLatencyPercentileIsCalled_thenZeroShouldBeReturned() {
assertThat(evaluator.getLatencyPercentile(90, MILLISECONDS), is(0F));
}

}

0 comments on commit 56f8fa2

Please sign in to comment.