mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-10 15:35:49 +01:00
implement cache statistics
This commit is contained in:
@@ -118,4 +118,15 @@ public interface Cache<K, V>
|
||||
* @return The cached element with the specified key or null
|
||||
*/
|
||||
public V get(K key);
|
||||
|
||||
/**
|
||||
* Returns performance statistics of the cache or null if the cache does not
|
||||
* support statistics. The returned statistic is a snapshot of the current
|
||||
* performance.
|
||||
*
|
||||
* @return performance statistics or null
|
||||
*
|
||||
* @since 2.0.0
|
||||
*/
|
||||
public CacheStatistics getStatistics();
|
||||
}
|
||||
|
||||
209
scm-core/src/main/java/sonia/scm/cache/CacheStatistics.java
vendored
Normal file
209
scm-core/src/main/java/sonia/scm/cache/CacheStatistics.java
vendored
Normal file
@@ -0,0 +1,209 @@
|
||||
/**
|
||||
* Copyright (c) 2010, Sebastian Sdorra All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer. 2. Redistributions in
|
||||
* binary form must reproduce the above copyright notice, this list of
|
||||
* conditions and the following disclaimer in the documentation and/or other
|
||||
* materials provided with the distribution. 3. Neither the name of SCM-Manager;
|
||||
* nor the names of its contributors may be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR
|
||||
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* http://bitbucket.org/sdorra/scm-manager
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
|
||||
package sonia.scm.cache;
|
||||
|
||||
//~--- non-JDK imports --------------------------------------------------------
|
||||
|
||||
import com.google.common.base.Objects;
|
||||
|
||||
/**
|
||||
* Statistics about the performance of a {@link Cache}.
|
||||
* Instances of this class are immutable.
|
||||
*
|
||||
* @author Sebastian Sdorra
|
||||
* @since 2.0.0
|
||||
*/
|
||||
public final class CacheStatistics
|
||||
{
|
||||
|
||||
/**
|
||||
* Constructs a new performance statistic for a {@link Cache}.
|
||||
*
|
||||
*
|
||||
* @param name name of the cache
|
||||
* @param hitCount hit count
|
||||
* @param missCount miss count
|
||||
*/
|
||||
public CacheStatistics(String name, long hitCount, long missCount)
|
||||
{
|
||||
this.name = name;
|
||||
this.hitCount = hitCount;
|
||||
this.missCount = missCount;
|
||||
}
|
||||
|
||||
//~--- methods --------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public boolean equals(Object obj)
|
||||
{
|
||||
if (obj == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (getClass() != obj.getClass())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
final CacheStatistics other = (CacheStatistics) obj;
|
||||
|
||||
return Objects.equal(name, other.name)
|
||||
&& Objects.equal(hitCount, other.hitCount)
|
||||
&& Objects.equal(missCount, other.missCount);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public int hashCode()
|
||||
{
|
||||
return Objects.hashCode(name, hitCount, missCount);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
//J-
|
||||
return Objects.toStringHelper(this)
|
||||
.add("name", name)
|
||||
.add("hitCount", hitCount)
|
||||
.add("missCount", missCount)
|
||||
.toString();
|
||||
//J+
|
||||
}
|
||||
|
||||
//~--- get methods ----------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Returns number of times requested elements were found in the cache.
|
||||
*
|
||||
*
|
||||
* @return number of cache hits
|
||||
*/
|
||||
public long getHitCount()
|
||||
{
|
||||
return hitCount;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the ratio of cache requests which were hits.
|
||||
*
|
||||
*
|
||||
* @return ratio of cache hits
|
||||
*/
|
||||
public double getHitRate()
|
||||
{
|
||||
return ratio(hitCount);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns number of times a requested element was not found in the cache.
|
||||
*
|
||||
*
|
||||
* @return number of cache misses
|
||||
*/
|
||||
public long getMissCount()
|
||||
{
|
||||
return missCount;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the ratio of cache requests which were misses.
|
||||
*
|
||||
*
|
||||
* @return ratio of cache misses
|
||||
*/
|
||||
public double getMissRate()
|
||||
{
|
||||
return ratio(missCount);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns name of the cache.
|
||||
*
|
||||
*
|
||||
* @return name of the cache
|
||||
*/
|
||||
public String getName()
|
||||
{
|
||||
return name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the total number of requests, this includes hits and misses.
|
||||
*
|
||||
*
|
||||
* @return numer of requests
|
||||
*/
|
||||
public long getRequestCount()
|
||||
{
|
||||
return hitCount + missCount;
|
||||
}
|
||||
|
||||
//~--- methods --------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Calculates the ratio of a counter.
|
||||
*
|
||||
*
|
||||
* @param counter counter
|
||||
*
|
||||
* @return rate of counter
|
||||
*/
|
||||
private double ratio(long counter)
|
||||
{
|
||||
long requestCount = getRequestCount();
|
||||
|
||||
return (requestCount == 0)
|
||||
? 1.0
|
||||
: (double) counter / requestCount;
|
||||
}
|
||||
|
||||
//~--- fields ---------------------------------------------------------------
|
||||
|
||||
/** hit count */
|
||||
private final long hitCount;
|
||||
|
||||
/** miss count */
|
||||
private final long missCount;
|
||||
|
||||
/** name of cache */
|
||||
private final String name;
|
||||
}
|
||||
73
scm-core/src/test/java/sonia/scm/cache/CacheStatisticsTest.java
vendored
Normal file
73
scm-core/src/test/java/sonia/scm/cache/CacheStatisticsTest.java
vendored
Normal file
@@ -0,0 +1,73 @@
|
||||
/**
|
||||
* Copyright (c) 2010, Sebastian Sdorra All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer. 2. Redistributions in
|
||||
* binary form must reproduce the above copyright notice, this list of
|
||||
* conditions and the following disclaimer in the documentation and/or other
|
||||
* materials provided with the distribution. 3. Neither the name of SCM-Manager;
|
||||
* nor the names of its contributors may be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR
|
||||
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* http://bitbucket.org/sdorra/scm-manager
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
|
||||
package sonia.scm.cache;
|
||||
|
||||
//~--- non-JDK imports --------------------------------------------------------
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Sebastian Sdorra
|
||||
*/
|
||||
public class CacheStatisticsTest
|
||||
{
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*/
|
||||
@Test
|
||||
public void testCounters()
|
||||
{
|
||||
CacheStatistics stats = new CacheStatistics("", 12, 3);
|
||||
|
||||
assertEquals(12, stats.getHitCount());
|
||||
assertEquals(3, stats.getMissCount());
|
||||
assertEquals(15, stats.getRequestCount());
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*/
|
||||
@Test
|
||||
public void testRates()
|
||||
{
|
||||
CacheStatistics stats = new CacheStatistics("", 12, 3);
|
||||
|
||||
assertEquals(0.8d, stats.getHitRate(), 0.0);
|
||||
assertEquals(0.2d, stats.getMissRate(), 0.0);
|
||||
}
|
||||
}
|
||||
@@ -47,6 +47,7 @@ import org.slf4j.LoggerFactory;
|
||||
import java.util.Collection;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -111,6 +112,10 @@ public class GuavaCache<K, V>
|
||||
{
|
||||
this.copyStrategy = copyStrategy;
|
||||
}
|
||||
else
|
||||
{
|
||||
this.copyStrategy = CopyStrategy.NONE;
|
||||
}
|
||||
}
|
||||
|
||||
//~--- methods --------------------------------------------------------------
|
||||
@@ -266,19 +271,42 @@ public class GuavaCache<K, V>
|
||||
if (value != null)
|
||||
{
|
||||
value = copyStrategy.copyOnRead(value);
|
||||
hitCount.incrementAndGet();
|
||||
}
|
||||
else
|
||||
{
|
||||
missCount.incrementAndGet();
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public CacheStatistics getStatistics()
|
||||
{
|
||||
return new CacheStatistics(name, hitCount.get(), missCount.get());
|
||||
}
|
||||
|
||||
//~--- fields ---------------------------------------------------------------
|
||||
|
||||
/** Field description */
|
||||
private com.google.common.cache.Cache<K, V> cache;
|
||||
private final com.google.common.cache.Cache<K, V> cache;
|
||||
|
||||
/** Field description */
|
||||
private CopyStrategy copyStrategy = CopyStrategy.NONE;
|
||||
private final CopyStrategy copyStrategy;
|
||||
|
||||
/** Field description */
|
||||
private String name;
|
||||
private final AtomicLong hitCount = new AtomicLong();
|
||||
|
||||
/** Field description */
|
||||
private final AtomicLong missCount = new AtomicLong();
|
||||
|
||||
/** Field description */
|
||||
private final String name;
|
||||
}
|
||||
|
||||
@@ -93,7 +93,7 @@
|
||||
expireAfterWrite="5400"
|
||||
/>
|
||||
|
||||
<!-- new repository api -->
|
||||
<!-- repository api -->
|
||||
|
||||
<!--
|
||||
Changeset cache
|
||||
|
||||
@@ -46,6 +46,7 @@ import sonia.scm.util.IOUtil;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import org.junit.Assume;
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -181,6 +182,30 @@ public abstract class CacheTestBase
|
||||
assertNotNull(cache.get("a-2"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCacheStatistics(){
|
||||
CacheStatistics stats = cache.getStatistics();
|
||||
// skip test if implementation does not support stats
|
||||
Assume.assumeTrue( stats != null );
|
||||
assertEquals("test", stats.getName());
|
||||
assertEquals(0l, stats.getHitCount());
|
||||
assertEquals(0l, stats.getMissCount());
|
||||
cache.put("test-1", "test123");
|
||||
cache.put("test-2", "test456");
|
||||
cache.get("test-1");
|
||||
cache.get("test-1");
|
||||
cache.get("test-1");
|
||||
cache.get("test-3");
|
||||
// check that stats have not changed
|
||||
assertEquals(0l, stats.getHitCount());
|
||||
assertEquals(0l, stats.getMissCount());
|
||||
stats = cache.getStatistics();
|
||||
assertEquals(3l, stats.getHitCount());
|
||||
assertEquals(1l, stats.getMissCount());
|
||||
assertEquals(0.75d, stats.getHitRate(), 0.0d);
|
||||
assertEquals(0.25d, stats.getMissRate(), 0.0d);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user