openjpa-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From curti...@apache.org
Subject svn commit: r910667 - in /openjpa/trunk: openjpa-kernel/src/main/java/org/apache/openjpa/datacache/ openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/ openjpa-project/src/doc/manual/
Date Tue, 16 Feb 2010 20:00:46 GMT
Author: curtisr7
Date: Tue Feb 16 20:00:38 2010
New Revision: 910667

URL: http://svn.apache.org/viewvc?rev=910667&view=rev
Log:
OPENJPA-1515: Make CacheStatistics configurable.

Modified:
    openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/AbstractDataCache.java
    openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/CacheStatistics.java
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/TestStatistics.java
    openjpa/trunk/openjpa-project/src/doc/manual/ref_guide_caching.xml

Modified: openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/AbstractDataCache.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/AbstractDataCache.java?rev=910667&r1=910666&r2=910667&view=diff
==============================================================================
--- openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/AbstractDataCache.java
(original)
+++ openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/AbstractDataCache.java
Tue Feb 16 20:00:38 2010
@@ -19,12 +19,10 @@
 package org.apache.openjpa.datacache;
 
 import java.util.ArrayList;
-import java.util.Arrays;
 import java.util.BitSet;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.HashMap;
-import java.util.HashSet;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
@@ -40,8 +38,6 @@
 import org.apache.openjpa.lib.util.Localizer;
 import org.apache.openjpa.lib.util.concurrent.AbstractConcurrentEventManager;
 
-import serp.util.Strings;
-
 /**
  * Abstract {@link DataCache} implementation that provides various
  * statistics, logging, and timeout functionality common across cache
@@ -82,6 +78,14 @@
     public void setName(String name) {
         _name = name;
     }
+    public void setEnableStatistics(boolean enable){
+        if(enable == true){
+            stats.enable();
+        }
+    }
+    public void getEnableStatistics(){
+        stats.isEnabled();
+    }
 
     public String getEvictionSchedule() {
         return _schedule;
@@ -131,7 +135,9 @@
 
     public boolean contains(Object key) {
         DataCachePCData o = getInternal(key);
-        stats.newGet(o == null ? null : o.getType(), o != null);
+        if (stats.isEnabled()) {
+            stats.newGet(o == null ? null : o.getType(), o != null);
+        }
         if (o != null && o.isTimedOut()) {
             o = null;
             removeInternal(key);
@@ -167,7 +173,9 @@
             else
                 log.trace(s_loc.get("cache-hit", key));
         }
-        stats.newGet((o == null) ? null : o.getType(), o != null);
+        if (stats.isEnabled()) {
+            stats.newGet((o == null) ? null : o.getType(), o != null);
+        }
         return o;
     }
 
@@ -183,7 +191,9 @@
     }
 
     public DataCachePCData put(DataCachePCData data) {
-        stats.newPut(data.getType());
+        if (stats.isEnabled()) {
+            stats.newPut(data.getType());
+        }
         DataCachePCData o = putInternal(data.getId(), data);
         if (log.isTraceEnabled())
             log.trace(s_loc.get("cache-put", data.getId()));
@@ -192,7 +202,9 @@
 
     public void update(DataCachePCData data) {
         if (recacheUpdates()) {
-            stats.newPut(data.getType());
+            if (stats.isEnabled()) {
+                stats.newPut(data.getType());
+            }
             putInternal(data.getId(), data);
         }
     }
@@ -381,7 +393,9 @@
      */
     protected void putAllInternal(Collection<DataCachePCData> pcs) {
         for (DataCachePCData pc : pcs) {
-            stats.newPut(pc.getType());
+            if (stats.isEnabled()) {
+                stats.newPut(pc.getType());
+            }
             putInternal(pc.getId(), pc);
         }
     }

Modified: openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/CacheStatistics.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/CacheStatistics.java?rev=910667&r1=910666&r2=910667&view=diff
==============================================================================
--- openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/CacheStatistics.java
(original)
+++ openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/CacheStatistics.java
Tue Feb 16 20:00:38 2010
@@ -119,6 +119,11 @@
 	public void reset();
 	
 	/**
+	 * Returns whether or not statistics will be collected.
+	 */
+	public boolean isEnabled();
+	
+	/**
 	 * A default implementation.
 	 *
 	 */
@@ -129,7 +134,8 @@
         private Map<Class, long[]> astats = new HashMap<Class, long[]>();
 		private Date start = new Date();
 		private Date since = new Date();
-
+		private boolean enabled = false;
+		
 		private static final int READ  = 0;
 		private static final int HIT   = 1;
 		private static final int WRITE = 2;
@@ -201,6 +207,15 @@
 			since = new Date();
 		}
 
+		public boolean isEnabled() {
+		    return enabled;
+		}
+		void enable(){
+		    enabled = true;
+		}
+	      void disable() {
+            enabled = false;
+        }
 		void newGet(Class cls, boolean hit) {
 			cls = (cls == null) ? Object.class : cls;
 			addSample(cls, READ);

Modified: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/TestStatistics.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/TestStatistics.java?rev=910667&r1=910666&r2=910667&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/TestStatistics.java
(original)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/datacache/TestStatistics.java
Tue Feb 16 20:00:38 2010
@@ -18,6 +18,8 @@
  */
 package org.apache.openjpa.persistence.datacache;
 
+import java.util.Arrays;
+
 import javax.persistence.EntityManager;
 
 import org.apache.openjpa.datacache.CacheStatistics;
@@ -40,9 +42,8 @@
     private StoreCache cache;
     CacheStatistics stats;
     public void setUp() {
-        
         super.setUp(CLEAR_TABLES, CachedPerson.class,
-                "openjpa.DataCache", "true",
+                "openjpa.DataCache", "true(EnableStatistics=true)",
                 "openjpa.QueryCache", "true",
                 "openjpa.RemoteCommitProvider", "sjvm");
         cache = emf.getStoreCache();
@@ -51,36 +52,57 @@
         assertNotNull(stats);
         em = emf.createEntityManager();
         
-        if (person == null) {
-            person = createData();
-        }
+        person = createData();
         stats.reset();
         em.clear();
     }
     
-    public CachedPerson createData() {
-        em.getTransaction().begin();
-        CachedPerson p = new CachedPerson();
-        p.setId((int)System.currentTimeMillis());
-        em.persist(p);
-        em.getTransaction().commit();
-        return p;
+    /**
+     * Test that the CacheStatistics is disabled by default.
+     */
+    public void testDefaultSettings() {
+        Object[] props = {"openjpa.DataCache", "true", "openjpa.RemoteCommitProvider", "sjvm"};
+        OpenJPAEntityManagerFactory emf1 = createNamedEMF("second-persistence-unit", props);
+        
+        assertFalse(emf1.getStoreCache().getStatistics().isEnabled());
     }
     
     /**
      * Finding an entity from a clean should hit the L2 cache.
      */
     public void testFind() {
+        assertTrue(cache.getStatistics().isEnabled());
         Object pid = person.getId();
-        int N = 0;
-        for (int i = 0; i < N; i++) {
-            assertCached(person, pid, !L1Cached, L2Cached);
-            long[] before = snapshot();
-            CachedPerson p = em.find(CachedPerson.class, pid);
-            long[] after = snapshot();
-            assertDelta(before, after, 1, 1, 0); //READ:1 HIT:1, WRITE:0
-            assertCached(p, pid, L1Cached, L2Cached);
-        }
+        assertCached(person, pid, !L1Cached, L2Cached);
+        
+        long[] before = snapshot();
+        CachedPerson p = em.find(CachedPerson.class, pid);
+        long[] after = snapshot();
+
+        assertDelta(before, after, 1, 1, 0); // READ:1 HIT:1, WRITE:0
+        assertCached(p, pid, L1Cached, L2Cached);
+
+    }
+    
+    public void testMultipleUnits() {
+        String[] props = {"openjpa.DataCache", "true", "openjpa.RemoteCommitProvider", "sjvm"};
+        OpenJPAEntityManagerFactory emf1 = createNamedEMF("test", props);
+        OpenJPAEntityManagerFactory emf2 = createNamedEMF("empty-pu", props);
+        assertNotSame(emf1, emf2);
+        assertNotSame(emf1.getStoreCache(), emf2.getStoreCache());
+        assertNotSame(emf1.getStoreCache().getStatistics(), emf2.getStoreCache().getStatistics());
+        assertNotSame(((StoreCacheImpl)emf1.getStoreCache()).getDelegate(), 
+                ((StoreCacheImpl)emf2.getStoreCache()).getDelegate());
+        
+    }
+    
+    CachedPerson createData() {
+        em.getTransaction().begin();
+        CachedPerson p = new CachedPerson();
+        p.setId((int)System.currentTimeMillis());
+        em.persist(p);
+        em.getTransaction().commit();
+        return p;
     }
     
     /**
@@ -121,16 +143,4 @@
         System.err.println(msg + stats + " H:" + stats.getHitCount() + " R:" + stats.getReadCount()
+ " W:" + 
                 stats.getWriteCount());
     }
-    
-    public void testMultipleUnits() {
-        String[] props = {"openjpa.DataCache", "true", "openjpa.RemoteCommitProvider", "sjvm"};
-        OpenJPAEntityManagerFactory emf1 = createNamedEMF("test", props);
-        OpenJPAEntityManagerFactory emf2 = createNamedEMF("empty-pu", props);
-        assertNotSame(emf1, emf2);
-        assertNotSame(emf1.getStoreCache(), emf2.getStoreCache());
-        assertNotSame(emf1.getStoreCache().getStatistics(), emf2.getStoreCache().getStatistics());
-        assertNotSame(((StoreCacheImpl)emf1.getStoreCache()).getDelegate(), 
-                ((StoreCacheImpl)emf2.getStoreCache()).getDelegate());
-        
-    }
 }

Modified: openjpa/trunk/openjpa-project/src/doc/manual/ref_guide_caching.xml
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-project/src/doc/manual/ref_guide_caching.xml?rev=910667&r1=910666&r2=910667&view=diff
==============================================================================
--- openjpa/trunk/openjpa-project/src/doc/manual/ref_guide_caching.xml (original)
+++ openjpa/trunk/openjpa-project/src/doc/manual/ref_guide_caching.xml Tue Feb 16 20:00:38
2010
@@ -550,7 +550,17 @@
 data cache is available via 
 <ulink url="../javadoc/org/apache/openjpa/datacache/CacheStatistics.html">
 <classname>org.apache.openjpa.datacache.CacheStatistics</classname></ulink>

-interface. You can access this statistics via StoreCache 
+interface. The collection of cache statistics is disabled by default and needs to be enabled
on a per cache basis. By default
+all counts returned from the CacheStatistics interface will return 0.
+        <example id="ref_guide_cache_enablestats">
+                <title>
+                    Configuring CacheStatistics
+                </title>
+                <programlisting>
+&lt;property name="openjpa.DataCache" value="true(EnableStatistics=true)"/&gt;
+</programlisting>
+        </example>
+Once cache statistics are enabled you can access them via StoreCache 
 <programlisting>
 import org.apache.openjpa.datacache.CacheStatistics;
 ...
@@ -587,6 +597,9 @@
     
     // Resets the statistics. 
     public void reset();
+    
+    // Returns whether or not statistics will be collected.
+    public boolean isEnabled();
 }
 </programlisting>
 Collecting per-class statistics depends on determining the runtime type of a



Mime
View raw message