Index: org/jboss/varia/stats/AbstractStatisticalItem.java =================================================================== RCS file: /cvsroot/jboss/contrib/varia/src/main/org/jboss/varia/stats/AbstractStatisticalItem.java,v retrieving revision 1.2.4.1 diff -u -r1.2.4.1 AbstractStatisticalItem.java --- org/jboss/varia/stats/AbstractStatisticalItem.java 29 Oct 2005 05:06:46 -0000 1.2.4.1 +++ org/jboss/varia/stats/AbstractStatisticalItem.java 26 Dec 2005 14:43:20 -0000 @@ -21,7 +21,6 @@ */ package org.jboss.varia.stats; - /** * @author Alexey Loubyansky * @version $Revision: 1.2.4.1 $ @@ -35,6 +34,10 @@ private int minCountPerTx = Integer.MAX_VALUE; private int maxCountPerTx; private int mergedItemsTotal = 1; + protected long executionTime = 0; + protected long minExecutionTime = Long.MAX_VALUE; + protected long maxExecutionTime = 0; + protected long totalExecutionTime = 0; public AbstractStatisticalItem(String name) { @@ -65,6 +68,21 @@ { return count; } + + public long getMinExecutionTime() + { + return minExecutionTime == Long.MAX_VALUE ? executionTime : minExecutionTime; + } + + public long getMaxExecutionTime() + { + return maxExecutionTime == 0 ? executionTime : maxExecutionTime; + } + + public long getExecutionTime() + { + return executionTime; + } public void add(StatisticalItem item) { @@ -75,6 +93,20 @@ } this.count += item.getCount(); + + long newTime = item.getExecutionTime(); + if (newTime > maxExecutionTime) + { + maxExecutionTime = newTime; + } + + if (newTime < minExecutionTime) + { + minExecutionTime = newTime; + } + + totalExecutionTime += newTime; + } public void merge(StatisticalItem item) @@ -105,4 +137,13 @@ { return mergedItemsTotal; } + + public long getMeanExecutionTime() + { + return totalExecutionTime / mergedItemsTotal; + } + + public long getTotalExecutionTime() { + return totalExecutionTime; + } } Index: org/jboss/varia/stats/DataSourceInterceptor.java =================================================================== RCS file: /cvsroot/jboss/contrib/varia/src/main/org/jboss/varia/stats/DataSourceInterceptor.java,v retrieving revision 1.1.4.3 diff -u -r1.1.4.3 DataSourceInterceptor.java --- org/jboss/varia/stats/DataSourceInterceptor.java 29 Oct 2005 05:06:46 -0000 1.1.4.3 +++ org/jboss/varia/stats/DataSourceInterceptor.java 26 Dec 2005 14:43:20 -0000 @@ -365,24 +365,24 @@ public PreparedStatement prepareStatement(String sql) throws SQLException { - logSql(sql); - return new PreparedStatementInterceptor(this, target.prepareStatement(sql)); + //logSql(sql); + return new PreparedStatementInterceptor(this, target.prepareStatement(sql), sql); } public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws SQLException { - logSql(sql); - return new PreparedStatementInterceptor(this, target.prepareStatement(sql, autoGeneratedKeys)); + //logSql(sql); + return new PreparedStatementInterceptor(this, target.prepareStatement(sql, autoGeneratedKeys), sql); } public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency) throws SQLException { - logSql(sql); + //logSql(sql); return new PreparedStatementInterceptor(this, - target.prepareStatement(sql, resultSetType, resultSetConcurrency)); + target.prepareStatement(sql, resultSetType, resultSetConcurrency), sql); } @@ -391,17 +391,17 @@ int resultSetConcurrency, int resultSetHoldability) throws SQLException { - logSql(sql); + //logSql(sql); return new PreparedStatementInterceptor(this, - target.prepareStatement(sql, resultSetType, resultSetConcurrency, resultSetHoldability)); + target.prepareStatement(sql, resultSetType, resultSetConcurrency, resultSetHoldability), sql); } public PreparedStatement prepareStatement(String sql, int columnIndexes[]) throws SQLException { - logSql(sql); - return new PreparedStatementInterceptor(this, target.prepareStatement(sql, columnIndexes)); + //logSql(sql); + return new PreparedStatementInterceptor(this, target.prepareStatement(sql, columnIndexes), sql); } @@ -415,8 +415,8 @@ public PreparedStatement prepareStatement(String sql, String columnNames[]) throws SQLException { - logSql(sql); - return new PreparedStatementInterceptor(this, target.prepareStatement(sql, columnNames)); + //logSql(sql); + return new PreparedStatementInterceptor(this, target.prepareStatement(sql, columnNames), sql); } } @@ -549,8 +549,12 @@ public int executeUpdate(String sql) throws SQLException { - logSql(sql); - return target.executeUpdate(sql); + long begin = System.currentTimeMillis(); + int ret = target.executeUpdate(sql); + long end = System.currentTimeMillis(); + logSql(sql, begin, end); + + return ret; } public void addBatch(String sql) throws SQLException @@ -566,39 +570,58 @@ public boolean execute(String sql) throws SQLException { - logSql(sql); - return target.execute(sql); + long begin = System.currentTimeMillis(); + boolean ret = target.execute(sql); + long end = System.currentTimeMillis(); + + logSql(sql, begin, end); + + return ret; } public int executeUpdate(String sql, int autoGeneratedKeys) throws SQLException { - logSql(sql); - return target.executeUpdate(sql, autoGeneratedKeys); + long begin = System.currentTimeMillis(); + int ret = target.executeUpdate(sql, autoGeneratedKeys); + long end = System.currentTimeMillis(); + + logSql(sql, begin, end); + + return ret; } public boolean execute(String sql, int autoGeneratedKeys) throws SQLException { - logSql(sql); - return target.execute(sql, autoGeneratedKeys); + long begin = System.currentTimeMillis(); + boolean ret = target.execute(sql, autoGeneratedKeys); + long end = System.currentTimeMillis(); + logSql(sql, begin, end); + return ret; } public int executeUpdate(String sql, int columnIndexes[]) throws SQLException { - logSql(sql); - return target.executeUpdate(sql, columnIndexes); + long begin = System.currentTimeMillis(); + int ret = target.executeUpdate(sql, columnIndexes); + long end = System.currentTimeMillis(); + logSql(sql, begin, end); + return ret; } public boolean execute(String sql, int columnIndexes[]) throws SQLException { - logSql(sql); - return target.execute(sql, columnIndexes); + long begin = System.currentTimeMillis(); + boolean ret = target.execute(sql, columnIndexes); + long end = System.currentTimeMillis(); + logSql(sql, begin, end); + return ret; } @@ -627,23 +650,32 @@ public int executeUpdate(String sql, String columnNames[]) throws SQLException { - logSql(sql); - return target.executeUpdate(sql, columnNames); + long begin = System.currentTimeMillis(); + int ret = target.executeUpdate(sql, columnNames); + long end = System.currentTimeMillis(); + logSql(sql, begin, end); + return ret; } public boolean execute(String sql, String columnNames[]) throws SQLException { - logSql(sql); - return target.execute(sql, columnNames); + long begin = System.currentTimeMillis(); + boolean ret = target.execute(sql, columnNames); + long end = System.currentTimeMillis(); + logSql(sql, begin, end); + return ret; } public ResultSet executeQuery(String sql) throws SQLException { - logSql(sql); - return target.executeQuery(sql); + long begin = System.currentTimeMillis(); + ResultSet ret = target.executeQuery(sql); + long end = System.currentTimeMillis(); + logSql(sql, begin, end); + return ret; } } @@ -652,16 +684,23 @@ implements PreparedStatement { private final PreparedStatement target; + private final String sql; - public PreparedStatementInterceptor(Connection con, PreparedStatement target) + public PreparedStatementInterceptor(Connection con, PreparedStatement target, String sql) { - super(con, target); - this.target = target; + super(con, target); + this.target = target; + this.sql = sql; } - + public int executeUpdate() throws SQLException { - return target.executeUpdate(); + long begin = System.currentTimeMillis(); + int ret = target.executeUpdate(); + long end = System.currentTimeMillis(); + + logSql(sql, begin, end); + return ret; } public void addBatch() throws SQLException @@ -676,7 +715,12 @@ public boolean execute() throws SQLException { - return target.execute(); + long begin = System.currentTimeMillis(); + boolean ret = target.execute(); + long end = System.currentTimeMillis(); + + logSql(sql, begin, end); + return ret; } public void setByte(int parameterIndex, byte x) throws SQLException @@ -815,7 +859,12 @@ public ResultSet executeQuery() throws SQLException { - return target.executeQuery(); + long begin = System.currentTimeMillis(); + ResultSet ret = target.executeQuery(); + long end = System.currentTimeMillis(); + + logSql(sql, begin, end); + return ret; } public ResultSetMetaData getMetaData() throws SQLException @@ -865,6 +914,25 @@ log.error("Failed to add invocation.", e); } } + + + private void logSql(String sql, long begin, long end) + { + try + { + if (log.isTraceEnabled()) { + log.trace("SQL:" + sql + ", " + (end - begin) + "ms"); + } + StatisticalItem item = new TxReport.SqlStats(sql, begin, end); + server.invoke(statsCollector, "addStatisticalItem", + new Object[]{item}, + new String[]{StatisticalItem.class.getName()}); + } + catch(Exception e) + { + log.error("Failed to add invocation.", e); + } + } private void bind() throws NamingException @@ -912,6 +980,7 @@ try { ic = new InitialContext(); + log.debug("target updating to " + targetName); target = (DataSource) ic.lookup(targetName); log.debug("target updated to " + targetName); } Index: org/jboss/varia/stats/StatisticalItem.java =================================================================== RCS file: /cvsroot/jboss/contrib/varia/src/main/org/jboss/varia/stats/StatisticalItem.java,v retrieving revision 1.2.4.1 diff -u -r1.2.4.1 StatisticalItem.java --- org/jboss/varia/stats/StatisticalItem.java 29 Oct 2005 05:06:46 -0000 1.2.4.1 +++ org/jboss/varia/stats/StatisticalItem.java 26 Dec 2005 14:43:20 -0000 @@ -49,4 +49,14 @@ public void mergeNull(); public int getMergedItemsTotal(); + + public long getMinExecutionTime(); + + public long getMaxExecutionTime(); + + public long getExecutionTime(); + + public long getMeanExecutionTime(); + + public long getTotalExecutionTime(); } Index: org/jboss/varia/stats/TxReport.java =================================================================== RCS file: /cvsroot/jboss/contrib/varia/src/main/org/jboss/varia/stats/TxReport.java,v retrieving revision 1.2.4.2 diff -u -r1.2.4.2 TxReport.java --- org/jboss/varia/stats/TxReport.java 29 Oct 2005 05:06:46 -0000 1.2.4.2 +++ org/jboss/varia/stats/TxReport.java 26 Dec 2005 14:43:21 -0000 @@ -149,11 +149,19 @@ public static class SqlStats extends AbstractStatisticalItem { public static final String NAME = "SQL Statistics Per Transaction"; - + public SqlStats(String sql) { super(NAME); value = sql; } + + public SqlStats(String sql, long begin, long end) + { + super(NAME); + value=sql; + executionTime = totalExecutionTime = end - begin; + maxExecutionTime = minExecutionTime = executionTime; + } } } Index: org/jboss/varia/stats/report/GeneralReportGenerator.java =================================================================== RCS file: /cvsroot/jboss/contrib/varia/src/main/org/jboss/varia/stats/report/GeneralReportGenerator.java,v retrieving revision 1.2.4.1 diff -u -r1.2.4.1 GeneralReportGenerator.java --- org/jboss/varia/stats/report/GeneralReportGenerator.java 29 Oct 2005 05:06:46 -0000 1.2.4.1 +++ org/jboss/varia/stats/report/GeneralReportGenerator.java 26 Dec 2005 14:43:21 -0000 @@ -102,8 +102,17 @@ if(itemMap != null && !itemMap.isEmpty()) { buf.append("") - .append(""); + .append(""); + if (itemName.equals(TxReport.SqlStats.NAME)) { + buf.append(""); + } + buf.append(""); + + int sumCount = 0; + long sumTotalMs = 0; + long sumAvgMs = 0; + for(Iterator itemIter = itemMap.values().iterator(); itemIter.hasNext();) { StatisticalItem item = (StatisticalItem) itemIter.next(); @@ -117,8 +126,49 @@ .append(item.getMinCountPerTx()) .append(""); + + sumCount += item.getCount(); + + if (itemName.equals(TxReport.SqlStats.NAME)) { + buf.append(""); + + sumTotalMs += item.getTotalExecutionTime(); + sumAvgMs += item.getMeanExecutionTime(); + } + + buf.append(""); } + + if (itemName.equals(TxReport.SqlStats.NAME)) { + + // total + buf.append(""); + } buf.append("
item%avgminmax
item%avgminmaxtotalavg(ms)min(ms)max(ms)total(ms)
") .append(item.getMaxCountPerTx()) + .append("") + .append(item.getCount()) .append("") + .append(item.getMeanExecutionTime()) + .append("") + .append(item.getMinExecutionTime()) + .append("") + .append(item.getMaxExecutionTime()) + .append("") + .append(item.getTotalExecutionTime()) + .append("
") // item + .append("Total") + .append("") //% + .append("") //avg + .append("") //min + .append("") //max + .append("") //total + .append(sumCount) //min + .append("") //avg(ms) + .append(sumAvgMs) + .append("") //min(ms) + .append("") //max(ms) + .append("") //total(ms) + .append(sumTotalMs) + .append("
"); } @@ -138,6 +188,10 @@ .append("
  • avg - the average number of times the item took place in the given transaction
  • ") .append("
  • min - the minimum number of times the item took place in the given transaction
  • ") .append("
  • max - the maximum number of times the item took place in the given transaction
  • ") + .append("
  • avg(ms) - the average execution time(ms) of the item took place in the given transaction
  • ") + .append("
  • min(ms) - the minimum execution time(ms) of the item took place in the given transaction
  • ") + .append("
  • max(ms) - the maximum execution time(ms) of the item took place in the given transaction
  • ") + .append("
  • total(ms) - the total execution time(ms) of the item took place in the given transaction
  • ") .append(""); } }