AccessLogSampler implements AccessLogInterface { @Override public void log(AccessLogEntry accessLogEntry) { if (accessLogEntry.getStatusCode() != 200) { return; } String uriString = accessLogEntry.getRawPath(); if (! uriString.startsWith("/search")) { return; } final long count = accessLineCounter.incrementAndGet(); if (count >= CircularArrayAccessLogKeeper.SIZE && count % CircularArrayAccessLogKeeper.SIZE != 0) { return; } circularArrayAccessLogKeeper.addUri(uriString); } AccessLogSampler(CircularArrayAccessLogKeeper circularArrayAccessLogKeeper); @Override void log(AccessLogEntry accessLogEntry);  }
@Test public void testAFewLines() { accessLogSampler.log(createLogEntry(200, "/search/foo")); accessLogSampler.log(createLogEntry(500, "/search/bar")); accessLogSampler.log(createLogEntry(500, "bar")); accessLogSampler.log(createLogEntry(200, "/search/what")); assertThat(uris, contains("/search/foo", "/search/what")); }
@Test public void testSubSampling() { for (int i = 0; i < CircularArrayAccessLogKeeper.SIZE; i++) { accessLogSampler.log(createLogEntry(200, "/search/" + String.valueOf(i))); } assertThat(uris.size(), is(CircularArrayAccessLogKeeper.SIZE)); assertThat(uris, hasItems("/search/0", "/search/1", "/search/2", "/search/" + String.valueOf(CircularArrayAccessLogKeeper.SIZE - 1))); uris.clear(); for (int i = 0; i < CircularArrayAccessLogKeeper.SIZE; i++) { accessLogSampler.log(createLogEntry(200, "/search/fuzz")); } assertThat(uris, hasItem("/search/fuzz")); assertThat(uris.size(), is(1)); for (int i = 0; i < CircularArrayAccessLogKeeper.SIZE; i++) { accessLogSampler.log(createLogEntry(200, "/search/ball")); } assertThat(uris, hasItem("/search/ball")); assertThat(uris.size(), is(2)); }