Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Made it doubles and added toMap
  • Loading branch information
bbakerman committed Sep 20, 2017
commit 86aa043036c902b2ee0b3114dcc7f20c8474bcc4
15 changes: 15 additions & 0 deletions src/main/java/org/dataloader/DataLoaderRegistry.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package org.dataloader;

import org.dataloader.stats.Statistics;
import org.dataloader.stats.StatisticsImpl;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
Expand Down Expand Up @@ -91,4 +94,16 @@ public Set<String> getKeys() {
public void dispatchAll() {
getDataLoaders().forEach(DataLoader::dispatch);
}

/**
* @return a combined set of statistics for all data loaders in this registry presented
* as the sum of all their statistics
*/
public Statistics getStatistics() {
Statistics stats = new StatisticsImpl();
for (DataLoader<?, ?> dataLoader : dataLoaders.values()) {
stats = stats.combine(dataLoader.getStatistics());
}
return stats;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public long incrementBatchLoadExceptionCount() {

@Override
public Statistics getStatistics() {
return new StatisticsImpl(loadCount.get(), batchLoadCount.get(), cacheHitCount.get(), batchLoadExceptionCount.get(), loadErrorCount.get());
return new StatisticsImpl(loadCount.get(), loadErrorCount.get(), batchLoadCount.get(), batchLoadExceptionCount.get(), cacheHitCount.get());
}

@Override
Expand Down
24 changes: 20 additions & 4 deletions src/main/java/org/dataloader/stats/Statistics.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.dataloader.stats;

import java.util.Map;

/**
* A simple set of statistics about {@link org.dataloader.DataLoader} operations
*/
Expand All @@ -17,7 +19,7 @@ public interface Statistics {
/**
* @return batchLoadCount / loadCount
*/
float getBatchLoadRatio();
double getBatchLoadRatio();

/**
* @return the number of times {@link org.dataloader.DataLoader#load(Object)} resulted in a cache hit
Expand All @@ -32,7 +34,7 @@ public interface Statistics {
/**
* @return cacheHits / loadCount
*/
float getCacheHitRatio();
double getCacheHitRatio();

/**
* @return the number of times the {@link org.dataloader.DataLoader} batch loader function throw an exception when trying to get any values
Expand All @@ -42,7 +44,7 @@ public interface Statistics {
/**
* @return batchLoadExceptionCount / loadCount
*/
float getBatchLoadExceptionRatio();
double getBatchLoadExceptionRatio();

/**
* @return the number of times the {@link org.dataloader.DataLoader} batch loader function return an specific object that was in error
Expand All @@ -53,6 +55,20 @@ public interface Statistics {
/**
* @return loadErrorCount / loadCount
*/
float getLoadErrorRatio();
double getLoadErrorRatio();


/**
* This will combine this set of statistics with another set of statistics so that they become the combined count of each
*
* @param statistics the other statistics to combine
*
* @return a new statistics object of the combined counts
*/
Statistics combine(Statistics statistics);

/**
* @return a map representation of the statistics, perhaps to send over JSON or some such
*/
Map<String, Number> toMap();
}
80 changes: 59 additions & 21 deletions src/main/java/org/dataloader/stats/StatisticsImpl.java
Original file line number Diff line number Diff line change
@@ -1,38 +1,69 @@
package org.dataloader.stats;

import java.util.LinkedHashMap;
import java.util.Map;

public class StatisticsImpl implements Statistics {

private final long loadCount;
private final long loadErrorCount;
private final long batchLoadCount;
private final long cacheHitCount;
private final long batchLoadExceptionCount;
private final long loadErrorCount;
private final long cacheHitCount;

public StatisticsImpl(long loadCount, long batchLoadCount, long cacheHitCount, long batchLoadExceptionCount, long loadErrorCount) {
/**
* Zero statistics
*/
public StatisticsImpl() {
this(0, 0, 0, 0, 0);
}

public StatisticsImpl(long loadCount, long loadErrorCount, long batchLoadCount, long batchLoadExceptionCount, long cacheHitCount) {
this.loadCount = loadCount;
this.batchLoadCount = batchLoadCount;
this.cacheHitCount = cacheHitCount;
this.batchLoadExceptionCount = batchLoadExceptionCount;
this.loadErrorCount = loadErrorCount;
}

private double ratio(long numerator, long denominator) {
return denominator == 0 ? 0f : ((double) numerator) / ((double) denominator);
}

@Override
public long getLoadCount() {
return loadCount;
}


@Override
public long getLoadErrorCount() {
return loadErrorCount;
}

@Override
public double getLoadErrorRatio() {
return ratio(loadErrorCount, loadCount);
}

@Override
public long getBatchLoadCount() {
return batchLoadCount;
}

@Override
public float getBatchLoadRatio() {
public double getBatchLoadRatio() {
return ratio(batchLoadCount, loadCount);
}

private float ratio(long numerator, long denominator) {
return denominator == 0 ? 0f : ((float) numerator) / ((float) denominator);
@Override
public long getBatchLoadExceptionCount() {
return batchLoadExceptionCount;
}

@Override
public double getBatchLoadExceptionRatio() {
return ratio(batchLoadExceptionCount, loadCount);
}

@Override
Expand All @@ -46,38 +77,45 @@ public long getCacheMissCount() {
}

@Override
public float getCacheHitRatio() {
public double getCacheHitRatio() {
return ratio(cacheHitCount, loadCount);
}

@Override
public long getBatchLoadExceptionCount() {
return batchLoadExceptionCount;
}

@Override
public long getLoadErrorCount() {
return loadErrorCount;
public Statistics combine(Statistics other) {
return new StatisticsImpl(
this.loadCount + other.getLoadCount(),
this.loadErrorCount + other.getLoadErrorCount(), this.batchLoadCount + other.getBatchLoadCount(),
this.batchLoadExceptionCount + other.getBatchLoadExceptionCount(), this.cacheHitCount + other.getCacheHitCount()
);
}

@Override
public float getBatchLoadExceptionRatio() {
return ratio(batchLoadExceptionCount, loadCount);
}
public Map<String, Number> toMap() {
Map<String, Number> stats = new LinkedHashMap<>();
stats.put("loadCount", getLoadCount());
stats.put("loadErrorCount", getLoadErrorCount());
stats.put("loadErrorRatio", getLoadErrorRatio());

@Override
public float getLoadErrorRatio() {
return ratio(loadErrorCount, loadCount);
stats.put("batchLoadCount", getBatchLoadCount());
stats.put("batchLoadRatio", getBatchLoadRatio());
stats.put("batchLoadExceptionCount", getBatchLoadExceptionCount());
stats.put("batchLoadExceptionRatio", getBatchLoadExceptionRatio());

stats.put("cacheHitCount", getCacheHitCount());
stats.put("cacheHitRatio", getCacheHitRatio());
return stats;
}

@Override
public String toString() {
return "StatisticsImpl{" +
"loadCount=" + loadCount +
", loadErrorCount=" + loadErrorCount +
", batchLoadCount=" + batchLoadCount +
", cacheHitCount=" + cacheHitCount +
", batchLoadExceptionCount=" + batchLoadExceptionCount +
", loadErrorCount=" + loadErrorCount +
", cacheHitCount=" + cacheHitCount +
'}';
}
}
33 changes: 33 additions & 0 deletions src/test/java/org/dataloader/DataLoaderRegistryTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.dataloader;

import org.dataloader.stats.Statistics;
import org.junit.Test;

import java.util.concurrent.CompletableFuture;
Expand Down Expand Up @@ -68,4 +69,36 @@ public void registries_can_be_combined() throws Exception {
assertThat(combinedRegistry.getKeys(), hasItems("a", "b", "c", "d"));
assertThat(combinedRegistry.getDataLoaders(), hasItems(dlA, dlB, dlC, dlD));
}

@Test
public void stats_can_be_collected() throws Exception {

DataLoaderRegistry registry = new DataLoaderRegistry();

DataLoader<Object, Object> dlA = new DataLoader<>(identityBatchLoader);
DataLoader<Object, Object> dlB = new DataLoader<>(identityBatchLoader);
DataLoader<Object, Object> dlC = new DataLoader<>(identityBatchLoader);

registry.register("a", dlA).register("b", dlB).register("c", dlC);

dlA.load("X");
dlB.load("Y");
dlC.load("Z");

registry.dispatchAll();

dlA.load("X");
dlB.load("Y");
dlC.load("Z");

registry.dispatchAll();

Statistics statistics = registry.getStatistics();

assertThat(statistics.getLoadCount(), equalTo(6L));
assertThat(statistics.getBatchLoadCount(), equalTo(3L));
assertThat(statistics.getCacheHitCount(), equalTo(3L));
assertThat(statistics.getLoadErrorCount(), equalTo(0L));
assertThat(statistics.getBatchLoadExceptionCount(), equalTo(0L));
}
}
12 changes: 6 additions & 6 deletions src/test/java/org/dataloader/stats/StatisticsCollectorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ public void ratios_work() throws Exception {
collector.incrementLoadCount();

Statistics stats = collector.getStatistics();
assertThat(stats.getBatchLoadRatio(), equalTo(0f));
assertThat(stats.getCacheHitRatio(), equalTo(0f));
assertThat(stats.getBatchLoadRatio(), equalTo(0d));
assertThat(stats.getCacheHitRatio(), equalTo(0d));


collector.incrementLoadCount();
Expand All @@ -53,7 +53,7 @@ public void ratios_work() throws Exception {
collector.incrementBatchLoadCount();

stats = collector.getStatistics();
assertThat(stats.getBatchLoadRatio(), equalTo(1f / 4f));
assertThat(stats.getBatchLoadRatio(), equalTo(1d / 4d));


collector.incrementLoadCount();
Expand All @@ -63,7 +63,7 @@ public void ratios_work() throws Exception {
collector.incrementCacheHitCount();

stats = collector.getStatistics();
assertThat(stats.getCacheHitRatio(), equalTo(2f / 7f));
assertThat(stats.getCacheHitRatio(), equalTo(2d / 7d));

collector.incrementLoadCount();
collector.incrementLoadCount();
Expand All @@ -72,7 +72,7 @@ public void ratios_work() throws Exception {
collector.incrementBatchLoadExceptionCount();

stats = collector.getStatistics();
assertThat(stats.getBatchLoadExceptionRatio(), equalTo(2f / 10f));
assertThat(stats.getBatchLoadExceptionRatio(), equalTo(2d / 10d));

collector.incrementLoadCount();
collector.incrementLoadCount();
Expand All @@ -82,7 +82,7 @@ public void ratios_work() throws Exception {
collector.incrementLoadErrorCount();

stats = collector.getStatistics();
assertThat(stats.getLoadErrorRatio(), equalTo(3f / 13f));
assertThat(stats.getLoadErrorRatio(), equalTo(3d / 13d));
}

@Test
Expand Down
43 changes: 43 additions & 0 deletions src/test/java/org/dataloader/stats/StatisticsImplTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package org.dataloader.stats;

import org.junit.Test;

import java.util.Map;

import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;

public class StatisticsImplTest {

@Test
public void combine_works() throws Exception {
StatisticsImpl one = new StatisticsImpl(1, 5, 2, 4, 3);
StatisticsImpl two = new StatisticsImpl(6, 10, 7, 9, 8);

Statistics combine = one.combine(two);

assertThat(combine.getLoadCount(), equalTo(7L));
assertThat(combine.getBatchLoadCount(), equalTo(9L));
assertThat(combine.getCacheHitCount(), equalTo(11L));
assertThat(combine.getBatchLoadExceptionCount(), equalTo(13L));
assertThat(combine.getLoadErrorCount(), equalTo(15L));
}

@Test
public void to_map_works() throws Exception {

StatisticsImpl one = new StatisticsImpl(10, 2, 3, 4, 5);
Map<String, Number> map = one.toMap();

assertThat(map.get("loadCount"), equalTo(10L));
assertThat(map.get("loadErrorCount"), equalTo(2L));
assertThat(map.get("loadErrorRatio"), equalTo(0.2d));
assertThat(map.get("batchLoadCount"), equalTo(3L));
assertThat(map.get("batchLoadRatio"), equalTo(0.3d));
assertThat(map.get("batchLoadExceptionCount"), equalTo(4L));
assertThat(map.get("batchLoadExceptionRatio"), equalTo(0.4d));
assertThat(map.get("cacheHitCount"), equalTo(5L));
assertThat(map.get("cacheHitRatio"), equalTo(0.5d));

}
}