openjpa-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ppod...@apache.org
Subject svn commit: r694135 - in /openjpa/branches/sql-cache: openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/sqlcache/ openjpa-persistence/src/main/java/org/apache/openjpa/persistence/
Date Thu, 11 Sep 2008 05:17:03 GMT
Author: ppoddar
Date: Wed Sep 10 22:17:03 2008
New Revision: 694135

URL: http://svn.apache.org/viewvc?rev=694135&view=rev
Log:
Add query execution statistics

Added:
    openjpa/branches/sql-cache/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/QueryStatistics.java
Modified:
    openjpa/branches/sql-cache/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/sqlcache/TestPreparedQueryCache.java
    openjpa/branches/sql-cache/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PreparedQueryCache.java
    openjpa/branches/sql-cache/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/QueryImpl.java

Modified: openjpa/branches/sql-cache/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/sqlcache/TestPreparedQueryCache.java
URL: http://svn.apache.org/viewvc/openjpa/branches/sql-cache/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/sqlcache/TestPreparedQueryCache.java?rev=694135&r1=694134&r2=694135&view=diff
==============================================================================
--- openjpa/branches/sql-cache/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/sqlcache/TestPreparedQueryCache.java
(original)
+++ openjpa/branches/sql-cache/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/sqlcache/TestPreparedQueryCache.java
Wed Sep 10 22:17:03 2008
@@ -218,6 +218,23 @@
 		assertLanguage(q3, QueryLanguages.LANG_PREPARED_SQL);
 	}
 
+	public void testQueryStatistics() {
+		String jpql1 = "select p from Company p";
+		String jpql2 = "select p from Company p where p.name = 'PObject'";
+		OpenJPAEntityManager em = emf.createEntityManager();
+		OpenJPAQuery q1 = em.createQuery(jpql1);
+		OpenJPAQuery q2 = em.createQuery(jpql2);
+		int N1 = 5;
+		int N2 = 8;
+		for (int i = 0; i < N1; i++)
+			em.createQuery(q1).getResultList();
+		for (int i = 0; i < N2; i++)
+			em.createQuery(q2).getResultList();
+		
+		assertEquals(N1, getCache().getStatistics().getExecutionCount(jpql1));
+		assertEquals(N2, getCache().getStatistics().getExecutionCount(jpql2));
+		assertEquals(N1+N2, getCache().getStatistics().getExecutionCount());
+	}
 
 	public void testQueryWithNoParameter() {
 		compare("select p from Company p");

Modified: openjpa/branches/sql-cache/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PreparedQueryCache.java
URL: http://svn.apache.org/viewvc/openjpa/branches/sql-cache/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PreparedQueryCache.java?rev=694135&r1=694134&r2=694135&view=diff
==============================================================================
--- openjpa/branches/sql-cache/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PreparedQueryCache.java
(original)
+++ openjpa/branches/sql-cache/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/PreparedQueryCache.java
Wed Sep 10 22:17:03 2008
@@ -55,12 +55,14 @@
 
 	private final Map<String, PreparedQuery> _delegate;
 	private final Set<String> _uncachables;
+	private final QueryStatistics _stats;
 	private List<String> _excludes;
 	private ReentrantLock _lock = new ReentrantLock();
 
 	public PreparedQueryCache() {
 		_delegate = new HashMap<String, PreparedQuery>();
 		_uncachables = new HashSet<String>();
+		_stats = new QueryStatistics.Default();
 	}
 	
 	/**
@@ -174,6 +176,10 @@
 		}
 	}
 	
+	public QueryStatistics getStatistics() {
+		return _stats;
+	}
+	
     void lock() {
         if (_lock != null)
             _lock.lock();

Modified: openjpa/branches/sql-cache/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/QueryImpl.java
URL: http://svn.apache.org/viewvc/openjpa/branches/sql-cache/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/QueryImpl.java?rev=694135&r1=694134&r2=694135&view=diff
==============================================================================
--- openjpa/branches/sql-cache/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/QueryImpl.java
(original)
+++ openjpa/branches/sql-cache/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/QueryImpl.java
Wed Sep 10 22:17:03 2008
@@ -248,6 +248,8 @@
 		PreparedQuery cachedQuery = cache();
 		boolean usingCachedQuery = (cachedQuery != null);
 		validate(_query.getParameterTypes(), !usingCachedQuery);
+		recordStatistics(usingCachedQuery ? cachedQuery.getIdentifier() 
+			: _query.getQueryString());
 		Object result = _query.execute(getParameterMap(usingCachedQuery));
 		return result;
 	}
@@ -635,6 +637,14 @@
 		_query = new DelegatingQuery(newQuery, 
 				broker.getInstanceExceptionTranslator());
 	}
+	
+	private void recordStatistics(String query) {
+		PreparedQueryCache cache = _em.getConfiguration()
+			.getPreparedQueryCacheInstance();
+		if (cache == null)
+			return;
+		cache.getStatistics().recordExecution(query);
+	}
 
 	public int hashCode() {
 		return _query.hashCode();

Added: openjpa/branches/sql-cache/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/QueryStatistics.java
URL: http://svn.apache.org/viewvc/openjpa/branches/sql-cache/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/QueryStatistics.java?rev=694135&view=auto
==============================================================================
--- openjpa/branches/sql-cache/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/QueryStatistics.java
(added)
+++ openjpa/branches/sql-cache/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/QueryStatistics.java
Wed Sep 10 22:17:03 2008
@@ -0,0 +1,145 @@
+/*
+ * 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.openjpa.persistence;
+
+import java.io.Serializable;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * Records query execution statistics.
+ * 
+ * @since 1.3.0
+ * 
+ * @author Pinaki Poddar
+ * 
+ */
+public interface QueryStatistics extends Serializable {
+	
+	/**
+	 * Record that the given query has been executed.
+	 */
+	void recordExecution(String query);
+		
+	/**
+	 * Gets number of total query execution since last reset.
+	 */
+	public long getExecutionCount();
+
+	/**
+	 * Gets number of total query execution since start.
+	 */
+	public long getTotalExecutionCount();
+
+	/**
+	 * Gets number of executions for the given query since last reset.
+	 */
+	public long getExecutionCount(String query);
+
+	/**
+	 * Gets number of executions for the given query since start.
+	 */
+	public long getTotalExecutionCount(String query);
+
+	/**
+	 * Gets the time of last reset.
+	 */
+	public Date since();
+
+	/**
+	 * Gets the time of start.
+	 */
+	public Date start();
+
+	/**
+	 * Clears all accumulated statistics.
+	 */
+	public void reset();
+	
+	/**
+	 * A default implementation.
+	 *
+	 */
+	public static class Default implements QueryStatistics {
+		private long[] astat = new long[1];
+		private long[] stat  = new long[1];
+		private Map<String, long[]> stats  = new HashMap<String, long[]>();
+		private Map<String, long[]> astats = new HashMap<String, long[]>();
+		private Date start = new Date();
+		private Date since = new Date();
+
+		private static final int READ  = 0;
+
+		public long getExecutionCount() {
+			return stat[READ];
+		}
+
+		public long getTotalExecutionCount() {
+			return astat[READ];
+		}
+
+		public long getExecutionCount(String query) {
+			return getCount(stats, query, READ);
+		}
+
+		public long getTotalExecutionCount(String query) {
+			return getCount(astats, query, READ);
+		}
+
+		private long getCount(Map<String, long[]> target, String query, int index) {
+			long[] row = target.get(query);
+			return (row == null) ? 0 : row[index];
+		}
+
+		public Date since() {
+			return since;
+		}
+
+		public Date start() {
+			return start;
+		}
+
+		public void reset() {
+			stat = new long[1];
+			stats.clear();
+			since = new Date();
+		}
+
+		private void addSample(String query, int index) {
+			stat[index]++;
+			astat[index]++;
+			addSample(stats, query, index);
+			addSample(astats, query, index);
+		}
+		
+		private void addSample(Map<String, long[]> target, String query, int index) {
+			long[] row = target.get(query);
+			if (row == null) {
+				row = new long[1];
+			}
+			row[index]++;
+			target.put(query, row);
+		}
+		
+		public void recordExecution(String query) {
+			addSample(query, READ);
+		}
+	}
+}



Mime
View raw message