You are viewing a plain text version of this content. The canonical link for it is here.
Posted to oak-commits@jackrabbit.apache.org by md...@apache.org on 2017/02/27 10:58:47 UTC
svn commit: r1784527 - in /jackrabbit/oak/trunk:
oak-core/src/test/java/org/apache/jackrabbit/oak/cache/CacheStatsTest.java
oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/RecordCacheStatsTest.java
Author: mduerig
Date: Mon Feb 27 10:58:46 2017
New Revision: 1784527
URL: http://svn.apache.org/viewvc?rev=1784527&view=rev
Log:
OAK-4619: Unify RecordCacheStats and CacheStats
Tests
Added:
jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/cache/CacheStatsTest.java
jackrabbit/oak/trunk/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/RecordCacheStatsTest.java
Added: jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/cache/CacheStatsTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/cache/CacheStatsTest.java?rev=1784527&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/cache/CacheStatsTest.java (added)
+++ jackrabbit/oak/trunk/oak-core/src/test/java/org/apache/jackrabbit/oak/cache/CacheStatsTest.java Mon Feb 27 10:58:46 2017
@@ -0,0 +1,189 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.apache.jackrabbit.oak.cache;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import java.util.concurrent.Callable;
+import java.util.concurrent.ExecutionException;
+
+import javax.annotation.Nonnull;
+
+import com.google.common.cache.Cache;
+import com.google.common.cache.CacheBuilder;
+import com.google.common.cache.Weigher;
+import org.junit.Before;
+import org.junit.Test;
+
+public class CacheStatsTest {
+ private static final String NAME = "cache stats";
+ private static final int KEYS = 100;
+
+ private final Weigher<Integer, Integer> weigher = new Weigher<Integer, Integer>() {
+ @Override
+ public int weigh(@Nonnull Integer key, @Nonnull Integer value) {
+ return 1;
+ }
+ };
+
+ private final Cache<Integer, Integer> cache = CacheBuilder.newBuilder()
+ .recordStats()
+ .maximumWeight(Long.MAX_VALUE)
+ .weigher(weigher)
+ .build();
+
+ private final CacheStats cacheStats =
+ new CacheStats(cache, NAME, weigher, Long.MAX_VALUE);
+
+ private int misses;
+ private int fails;
+ private long loadTime;
+
+ @Before
+ public void setup() {
+ for (int k = 0; k < KEYS; k++) {
+ cache.put(k, k);
+ }
+
+ for (int k = 0; k < 100; k++) {
+ final int key = 4 * k;
+ try {
+ cache.get(key, new Callable<Integer>() {
+ @Override
+ public Integer call() throws Exception {
+ long t0 = System.nanoTime();
+ try {
+ if (key % 10 == 0) {
+ fails++;
+ throw new Exception("simulated load failure");
+ } else {
+ misses++;
+ return key;
+ }
+ } finally {
+ loadTime += System.nanoTime() - t0;
+ }
+
+ }
+ });
+ } catch (ExecutionException ignore) { }
+ }
+ }
+
+ @Test
+ public void name() throws Exception {
+ assertEquals(NAME, cacheStats.getName());
+ }
+
+ @Test
+ public void getRequestCount() {
+ assertEquals(KEYS, cacheStats.getRequestCount());
+ }
+
+ @Test
+ public void getHitCount() {
+ assertEquals(KEYS - misses - fails, cacheStats.getHitCount());
+ }
+
+ @Test
+ public void getHitRate() {
+ assertEquals((KEYS - (double) misses - fails)/KEYS, cacheStats.getHitRate(), Double.MIN_VALUE);
+ }
+
+ @Test
+ public void getMissCount() {
+ assertEquals(misses + fails, cacheStats.getMissCount());
+ }
+
+ @Test
+ public void getMissRate() {
+ assertEquals(((double)misses + fails)/KEYS, cacheStats.getMissRate(), Double.MIN_VALUE);
+ }
+
+ @Test
+ public void getLoadCount() {
+ assertEquals(misses + fails, cacheStats.getLoadCount());
+ }
+
+ @Test
+ public void getLoadSuccessCount() {
+ assertEquals(misses, cacheStats.getLoadSuccessCount());
+ }
+
+ @Test
+ public void getLoadExceptionCount() {
+ assertEquals(fails, cacheStats.getLoadExceptionCount());
+ }
+
+ @Test
+ public void getLoadExceptionRate() {
+ assertEquals((double)fails/(misses + fails), cacheStats.getLoadExceptionRate(), Double.MIN_VALUE);
+ }
+
+ @Test
+ public void getTotalLoadTime() {
+ assertTrue(loadTime <= cacheStats.getTotalLoadTime());
+ }
+
+ @Test
+ public void getAverageLoadPenalty() {
+ assertTrue(((double)loadTime/(misses + fails)) <= cacheStats.getAverageLoadPenalty());
+ }
+
+ @Test
+ public void getEvictionCount() {
+ assertEquals(0, cacheStats.getEvictionCount());
+ }
+
+ @Test
+ public void getElementCount() {
+ assertEquals(KEYS + misses, cacheStats.getElementCount());
+ }
+
+ @Test
+ public void getMaxTotalWeight() {
+ assertEquals(Long.MAX_VALUE, cacheStats.getMaxTotalWeight());
+ }
+
+ @Test
+ public void estimateCurrentWeight() {
+ assertEquals(KEYS + misses, cacheStats.estimateCurrentWeight());
+ }
+
+ @Test
+ public void resetStats() {
+ cacheStats.resetStats();
+ assertEquals(0, cacheStats.getRequestCount());
+ assertEquals(0, cacheStats.getHitCount());
+ assertEquals(1.0, cacheStats.getHitRate(), Double.MIN_VALUE);
+ assertEquals(0, cacheStats.getMissCount());
+ assertEquals(0.0, cacheStats.getMissRate(), Double.MIN_VALUE);
+ assertEquals(0, cacheStats.getLoadCount());
+ assertEquals(0, cacheStats.getLoadSuccessCount());
+ assertEquals(0, cacheStats.getLoadExceptionCount());
+ assertEquals(0, cacheStats.getLoadExceptionRate(), Double.MIN_VALUE);
+ assertEquals(0, cacheStats.getTotalLoadTime());
+ assertEquals(0, cacheStats.getAverageLoadPenalty(), Double.MIN_VALUE);
+ assertEquals(0, cacheStats.getEvictionCount());
+ assertEquals(KEYS + misses, cacheStats.getElementCount());
+ assertEquals(Long.MAX_VALUE, cacheStats.getMaxTotalWeight());
+ assertEquals(KEYS + misses, cacheStats.estimateCurrentWeight());
+ }
+}
Added: jackrabbit/oak/trunk/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/RecordCacheStatsTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/RecordCacheStatsTest.java?rev=1784527&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/RecordCacheStatsTest.java (added)
+++ jackrabbit/oak/trunk/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/RecordCacheStatsTest.java Mon Feb 27 10:58:46 2017
@@ -0,0 +1,176 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.apache.jackrabbit.oak.segment;
+
+import static org.apache.jackrabbit.oak.segment.RecordCache.newRecordCache;
+import static org.junit.Assert.assertEquals;
+
+import java.io.IOException;
+import java.util.Random;
+
+import com.google.common.base.Supplier;
+import com.google.common.cache.CacheStats;
+import org.apache.jackrabbit.oak.segment.memory.MemoryStore;
+import org.junit.Before;
+import org.junit.Ignore;
+import org.junit.Test;
+
+public class RecordCacheStatsTest {
+ private static final String NAME = "cache stats";
+ private static final int KEYS = 100;
+
+ private final Random rnd = new Random();
+ private final MemoryStore store = new MemoryStore();
+
+ private final RecordCache<Integer> cache = newRecordCache(KEYS);
+ private final RecordCacheStats cacheStats =
+ new RecordCacheStats(NAME,
+ new Supplier<CacheStats>() {
+ @Override public CacheStats get() { return cache.getStats(); }
+ },
+ new Supplier<Long>() {
+ @Override public Long get() { return cache.size(); }
+ },
+ new Supplier<Long>() {
+ @Override public Long get() { return cache.estimateCurrentWeight(); }
+ });
+
+ private int hits;
+
+ public RecordCacheStatsTest() throws IOException {}
+
+ private RecordId newRecordId() {
+ return TestUtils.newRecordId(store, rnd);
+ }
+
+ @Before
+ public void setup() {
+ for (int k = 0; k < KEYS; k++) {
+ cache.put(k, newRecordId());
+ }
+
+ for (int k = 0; k < 100; k++) {
+ if (cache.get(4 * k) != null) {
+ hits++;
+ }
+ }
+ }
+
+ @Test
+ public void name() throws Exception {
+ assertEquals(NAME, cacheStats.getName());
+ }
+
+ @Test
+ public void getRequestCount() {
+ assertEquals(KEYS, cacheStats.getRequestCount());
+ }
+
+ @Test
+ public void getHitCount() {
+ assertEquals(hits, cacheStats.getHitCount());
+ }
+
+ @Test
+ public void getHitRate() {
+ assertEquals(((double)hits)/KEYS, cacheStats.getHitRate(), Double.MIN_VALUE);
+ }
+
+ @Test
+ public void getMissCount() {
+ assertEquals(KEYS - hits, cacheStats.getMissCount());
+ }
+
+ @Test
+ public void getMissRate() {
+ assertEquals((KEYS - (double)hits)/KEYS, cacheStats.getMissRate(), Double.MIN_VALUE);
+ }
+
+ @Test
+ public void getLoadCount() {
+ assertEquals(KEYS, cacheStats.getLoadCount());
+ }
+
+ @Test
+ public void getLoadSuccessCount() {
+ assertEquals(KEYS, cacheStats.getLoadSuccessCount());
+ }
+
+ @Test
+ public void getLoadExceptionCount() {
+ assertEquals(0, cacheStats.getLoadExceptionCount());
+ }
+
+ @Test
+ public void getLoadExceptionRate() {
+ assertEquals(0, cacheStats.getLoadExceptionRate(), Double.MIN_VALUE);
+ }
+
+ @Test
+ public void getTotalLoadTime() {
+ assertEquals(0, cacheStats.getTotalLoadTime());
+ }
+
+ @Test
+ public void getAverageLoadPenalty() {
+ assertEquals(0, cacheStats.getAverageLoadPenalty(), Double.MIN_VALUE);
+ }
+
+ @Test
+ public void getEvictionCount() {
+ assertEquals(0, cacheStats.getEvictionCount());
+ }
+
+ @Test
+ public void getElementCount() {
+ assertEquals(KEYS, cacheStats.getElementCount());
+ }
+
+ @Test
+ public void getMaxTotalWeight() {
+ assertEquals(-1, cacheStats.getMaxTotalWeight());
+ }
+
+ @Test
+ @Ignore
+ public void estimateCurrentWeight() {
+ assertEquals(KEYS, cacheStats.estimateCurrentWeight());
+ }
+
+ @Test
+ @Ignore
+ public void resetStats() {
+ cacheStats.resetStats();
+ assertEquals(0, cacheStats.getRequestCount());
+ assertEquals(0, cacheStats.getHitCount());
+ assertEquals(1.0, cacheStats.getHitRate(), Double.MIN_VALUE);
+ assertEquals(0, cacheStats.getMissCount());
+ assertEquals(0.0, cacheStats.getMissRate(), Double.MIN_VALUE);
+ assertEquals(0, cacheStats.getLoadCount());
+ assertEquals(0, cacheStats.getLoadSuccessCount());
+ assertEquals(0, cacheStats.getLoadExceptionCount());
+ assertEquals(0, cacheStats.getLoadExceptionRate(), Double.MIN_VALUE);
+ assertEquals(0, cacheStats.getTotalLoadTime());
+ assertEquals(0, cacheStats.getAverageLoadPenalty(), Double.MIN_VALUE);
+ assertEquals(0, cacheStats.getEvictionCount());
+ assertEquals(KEYS, cacheStats.getElementCount());
+ assertEquals(-1, cacheStats.getMaxTotalWeight());
+ assertEquals(KEYS, cacheStats.estimateCurrentWeight());
+ }
+}