Skip to content

Commit 165b8ac

Browse files
committed
readme updates
1 parent 5b5b1b8 commit 165b8ac

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,38 @@ In some circumstances you may wish to clear the cache for these individual probl
269269
});
270270
```
271271

272+
273+
## Statistics on what is happening
274+
275+
`DataLoader` keeps statistics on what is happening. It can tell you the number of objects asked for, the cache hit number, the number of objects
276+
asked for via batching and so on.
277+
278+
Knowing what the behaviour of your data is important for you to understand how efficient you are in serving the data via this pattern.
279+
280+
281+
```java
282+
Statistics statistics = userDataLoader.getStatistics();
283+
284+
System.out.println(format("load : %d", statistics.getLoadCount()));
285+
System.out.println(format("batch load: %d", statistics.getBatchLoadCount()));
286+
System.out.println(format("cache hit: %d", statistics.getCacheHitCount()));
287+
System.out.println(format("cache hit ratio: %d", statistics.getCacheHitRatio()));
288+
289+
```
290+
291+
`DataLoaderRegistry` can also roll up the statistics for all data loaders inside it.
292+
293+
You can configure the statistics collector used when you build the data loader
294+
295+
```java
296+
DataLoaderOptions options = DataLoaderOptions.newOptions().setStatisticsCollector(() -> new ThreadLocalStatisticsCollector());
297+
DataLoader<String,User> userDataLoader = DataLoader.newDataLoader(userBatchLoader,options);
298+
299+
```
300+
301+
Which collector you use is up to you. It ships with the following: `SimpleStatisticsCollector`, `ThreadLocalStatisticsCollector`, `DelegatingStatisticsCollector`
302+
and `NoOpStatisticsCollector`.
303+
272304
## The scope of a data loader is important
273305

274306
If you are serving web requests then the data can be specific to the user requesting it. If you have user specific data

src/test/java/ReadmeExamples.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,17 @@
55
import org.dataloader.Try;
66
import org.dataloader.fixtures.User;
77
import org.dataloader.fixtures.UserManager;
8+
import org.dataloader.stats.Statistics;
9+
import org.dataloader.stats.ThreadLocalStatisticsCollector;
810

911
import java.util.ArrayList;
1012
import java.util.List;
1113
import java.util.concurrent.CompletableFuture;
1214
import java.util.concurrent.CompletionStage;
1315
import java.util.stream.Collectors;
1416

17+
import static java.lang.String.format;
18+
1519
@SuppressWarnings("ALL")
1620
public class ReadmeExamples {
1721

@@ -75,7 +79,6 @@ public CompletionStage<List<User>> load(List<Long> userIds) {
7579
}
7680

7781

78-
7982
private void tryExample() {
8083
Try<String> tryS = Try.tryCall(() -> {
8184
if (rollDice()) {
@@ -185,4 +188,19 @@ private boolean rollDice() {
185188
}
186189

187190

191+
private void statsExample() {
192+
Statistics statistics = userDataLoader.getStatistics();
193+
194+
System.out.println(format("load : %d", statistics.getLoadCount()));
195+
System.out.println(format("batch load: %d", statistics.getBatchLoadCount()));
196+
System.out.println(format("cache hit: %d", statistics.getCacheHitCount()));
197+
System.out.println(format("cache hit ratio: %d", statistics.getCacheHitRatio()));
198+
}
199+
200+
private void statsConfigExample() {
201+
202+
DataLoaderOptions options = DataLoaderOptions.newOptions().setStatisticsCollector(() -> new ThreadLocalStatisticsCollector());
203+
DataLoader<String,User> userDataLoader = DataLoader.newDataLoader(userBatchLoader,options);
204+
}
205+
188206
}

0 commit comments

Comments
 (0)