Skip to content

Commit b86bba0

Browse files
committed
added delegating stats collector
1 parent 1032d81 commit b86bba0

File tree

3 files changed

+91
-1
lines changed

3 files changed

+91
-1
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package org.dataloader.stats;
2+
3+
import static org.dataloader.impl.Assertions.nonNull;
4+
5+
/**
6+
* This statistics collector keeps dataloader statistics AND also calls the delegate
7+
* collector at the same time. This allows you to keep a specific set of statistics
8+
* and also delegate the calls onto another collector.
9+
*/
10+
public class DelegatingStatisticsCollector implements StatisticsCollector {
11+
12+
private final StatisticsCollector collector = new SimpleStatisticsCollector();
13+
private final StatisticsCollector delegateCollector;
14+
15+
/**
16+
* @param delegateCollector a non null delegate collector
17+
*/
18+
public DelegatingStatisticsCollector(StatisticsCollector delegateCollector) {
19+
this.delegateCollector = nonNull(delegateCollector);
20+
}
21+
22+
@Override
23+
public long incrementLoadCount() {
24+
delegateCollector.incrementLoadCount();
25+
return collector.incrementLoadCount();
26+
}
27+
28+
@Override
29+
public long incrementBatchLoadCount() {
30+
delegateCollector.incrementBatchLoadCount();
31+
return collector.incrementBatchLoadCount();
32+
}
33+
34+
@Override
35+
public long incrementCacheHitCount() {
36+
delegateCollector.incrementCacheHitCount();
37+
return collector.incrementCacheHitCount();
38+
}
39+
40+
/**
41+
* @return the statistics of the this collector (and not its delegate)
42+
*/
43+
@Override
44+
public Statistics getStatistics() {
45+
return collector.getStatistics();
46+
}
47+
48+
/**
49+
* @return the statistics of the delegate
50+
*/
51+
public Statistics getDelegateStatistics() {
52+
return delegateCollector.getStatistics();
53+
}
54+
55+
}

src/main/java/org/dataloader/stats/ThreadLocalStatisticsCollector.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44
* This can collect statistics per thread as well as in an overall sense. This allows you to snapshot stats for a web request say
55
* as well as all requests.
66
*
7-
* You will want to call {@link #resetThread()} to clean up the thread local aspects of this object per request thread
7+
* You will want to call {@link #resetThread()} to clean up the thread local aspects of this object per request thread.
8+
*
9+
* ThreadLocals have their place in the Java world but be careful on how you use them. If you don't clean them up on "request boundaries"
10+
* then you WILL have misleading statistics.
811
*
912
* @see org.dataloader.stats.StatisticsCollector
1013
*/

src/test/java/org/dataloader/stats/StatisticsCollectorTest.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,4 +131,36 @@ public void thread_local_collection() throws Exception {
131131
assertThat(collector.getOverallStatistics().getBatchLoadCount(), equalTo(3L));
132132
assertThat(collector.getOverallStatistics().getCacheHitCount(), equalTo(3L));
133133
}
134+
135+
@Test
136+
public void delegating_collector_works() throws Exception {
137+
SimpleStatisticsCollector delegate = new SimpleStatisticsCollector();
138+
DelegatingStatisticsCollector collector = new DelegatingStatisticsCollector(delegate);
139+
140+
assertThat(collector.getStatistics().getLoadCount(), equalTo(0L));
141+
assertThat(collector.getStatistics().getBatchLoadCount(), equalTo(0L));
142+
assertThat(collector.getStatistics().getCacheHitCount(), equalTo(0L));
143+
assertThat(collector.getStatistics().getCacheMissCount(), equalTo(0L));
144+
145+
146+
collector.incrementLoadCount();
147+
collector.incrementBatchLoadCount();
148+
collector.incrementCacheHitCount();
149+
150+
assertThat(collector.getStatistics().getLoadCount(), equalTo(1L));
151+
assertThat(collector.getStatistics().getBatchLoadCount(), equalTo(1L));
152+
assertThat(collector.getStatistics().getCacheHitCount(), equalTo(1L));
153+
assertThat(collector.getStatistics().getCacheMissCount(), equalTo(0L));
154+
155+
assertThat(collector.getDelegateStatistics().getLoadCount(), equalTo(1L));
156+
assertThat(collector.getDelegateStatistics().getBatchLoadCount(), equalTo(1L));
157+
assertThat(collector.getDelegateStatistics().getCacheHitCount(), equalTo(1L));
158+
assertThat(collector.getDelegateStatistics().getCacheMissCount(), equalTo(0L));
159+
160+
assertThat(delegate.getStatistics().getLoadCount(), equalTo(1L));
161+
assertThat(delegate.getStatistics().getBatchLoadCount(), equalTo(1L));
162+
assertThat(delegate.getStatistics().getCacheHitCount(), equalTo(1L));
163+
assertThat(delegate.getStatistics().getCacheMissCount(), equalTo(0L));
164+
165+
}
134166
}

0 commit comments

Comments
 (0)