|
| 1 | +/* |
| 2 | +* author: gvenzl |
| 3 | +* created: 23 Dec 2016 |
| 4 | +*/ |
| 5 | + |
| 6 | +package com.gvenzl.stats; |
| 7 | + |
| 8 | +import java.sql.Connection; |
| 9 | +import java.sql.PreparedStatement; |
| 10 | +import java.sql.ResultSet; |
| 11 | +import java.sql.SQLException; |
| 12 | +import java.util.ArrayList; |
| 13 | +import java.util.HashMap; |
| 14 | +import java.util.Map.Entry; |
| 15 | + |
| 16 | +import com.gvenzl.OutOfSequenceException; |
| 17 | + |
| 18 | +/** |
| 19 | + * Provides statistic for the database connection. |
| 20 | + * @author gvenzl |
| 21 | + * |
| 22 | + */ |
| 23 | +public class ConnectionStats { |
| 24 | + |
| 25 | + private static final int INIT_CAPACITY = 1000; |
| 26 | + private Connection myConn; |
| 27 | + private HashMap<String, Long> beginStats; |
| 28 | + private HashMap<String, Long> endStats; |
| 29 | + |
| 30 | + /** |
| 31 | + * Creates a new ConnectionStats object. |
| 32 | + * @param conn The connection used to retrieve the statistics |
| 33 | + * @throws SQLException Any SQL error during the instantiation |
| 34 | + */ |
| 35 | + public ConnectionStats(Connection conn) throws SQLException { |
| 36 | + |
| 37 | + if (null == conn || conn.isClosed()) { |
| 38 | + throw new SQLException ("No connection to the database"); |
| 39 | + } |
| 40 | + myConn = conn; |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Creates a new snapshot for the current connection. |
| 45 | + * @throws SQLException Any SQL error during this operation |
| 46 | + */ |
| 47 | + public void createSnapshot() throws SQLException { |
| 48 | + HashMap<String, Long> results = getCurrentStats(); |
| 49 | + |
| 50 | + if (null == beginStats) { |
| 51 | + beginStats = results; |
| 52 | + } |
| 53 | + else { |
| 54 | + endStats = results; |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Returns the current statistics for the database connection. |
| 60 | + * @return A HashMap of the current statistics |
| 61 | + * @throws SQLException Any SQL error during this operation |
| 62 | + */ |
| 63 | + public HashMap<String, Long> getCurrentStats() throws SQLException { |
| 64 | + HashMap<String, Long> results = new HashMap<String, Long>(INIT_CAPACITY); |
| 65 | + |
| 66 | + PreparedStatement stmt = myConn.prepareStatement( |
| 67 | + "SELECT n.name, s.value FROM v$mystat s, v$statname n" |
| 68 | + + " WHERE s.statistic# = n.statistic#" |
| 69 | + + " ORDER BY n.name"); |
| 70 | + ResultSet rslt = stmt.executeQuery(); |
| 71 | + while (rslt.next()) { |
| 72 | + results.put(rslt.getString(1), rslt.getLong(2)); |
| 73 | + } |
| 74 | + stmt.close(); |
| 75 | + |
| 76 | + return results; |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Creates a new snapshot for the database connection with a given session id. |
| 81 | + * @param sessionId The session id (SID) for which to take the snapshot |
| 82 | + * @throws SQLException Any SQL error during this operation |
| 83 | + */ |
| 84 | + public void createSnapshot(int sessionId) throws SQLException { |
| 85 | + HashMap<String, Long> results = getCurrentStats(sessionId); |
| 86 | + |
| 87 | + if (null == beginStats) { |
| 88 | + beginStats = results; |
| 89 | + } |
| 90 | + else { |
| 91 | + endStats = results; |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Returns the current statistics for the database connection with the given session id. |
| 97 | + * @param sessionId The session id (SID) for which to retrieve the statistics |
| 98 | + * @return A HashMap of the current statistics |
| 99 | + * @throws SQLException Any SQL error during this operation |
| 100 | + */ |
| 101 | + public HashMap<String, Long> getCurrentStats(int sessionId) throws SQLException { |
| 102 | + HashMap<String, Long> results = new HashMap<String, Long>(INIT_CAPACITY); |
| 103 | + |
| 104 | + PreparedStatement stmt = myConn.prepareStatement( |
| 105 | + "SELECT n.name, s.value FROM v$sesstat s, v$statname n" |
| 106 | + + " WHERE s.sid = ? AND s.statistic# = n.statistic#" |
| 107 | + + " ORDER BY n.name"); |
| 108 | + stmt.setInt(1, sessionId); |
| 109 | + ResultSet rslt = stmt.executeQuery(); |
| 110 | + while (rslt.next()) { |
| 111 | + results.put(rslt.getString(1), rslt.getLong(2)); |
| 112 | + } |
| 113 | + stmt.close(); |
| 114 | + |
| 115 | + return results; |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * Returns a delta of the statistics of the connection. Only statistics with a delta are returned. |
| 120 | + * @return A HashMap containing all statistics with deltas |
| 121 | + * @throws OutOfSequenceException If the begin or end snapshot is out of sequence |
| 122 | + */ |
| 123 | + public HashMap<String, Long> getDelta() throws OutOfSequenceException { |
| 124 | + if (null == beginStats) { |
| 125 | + throw new OutOfSequenceException("No begin snapshot available, create begin and end snapshots first!"); |
| 126 | + } |
| 127 | + |
| 128 | + if (null == endStats) { |
| 129 | + throw new OutOfSequenceException("No end snapshot available, create begin snapshot first!"); |
| 130 | + } |
| 131 | + |
| 132 | + HashMap<String, Long> delta = new HashMap<String, Long>(); |
| 133 | + |
| 134 | + for (Entry<String, Long> entry : beginStats.entrySet()) { |
| 135 | + String beginKey = entry.getKey(); |
| 136 | + Long beginValue = entry.getValue(); |
| 137 | + Long endValue = endStats.get(beginKey); |
| 138 | + |
| 139 | + // Only store delats |
| 140 | + long deltaLong = endValue.longValue()-beginValue.longValue(); |
| 141 | + if (deltaLong != 0) { |
| 142 | + delta.put(beginKey, deltaLong); |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + beginStats = null; |
| 147 | + endStats = null; |
| 148 | + |
| 149 | + return delta; |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * Returns all statistic names of the database. |
| 154 | + * @return A list of all statistic names |
| 155 | + * @throws SQLException Any SQL error during this operation |
| 156 | + */ |
| 157 | + public ArrayList<String> getAllStatisticNames() throws SQLException { |
| 158 | + ArrayList<String> stats = new ArrayList<String>(INIT_CAPACITY); |
| 159 | + |
| 160 | + PreparedStatement stmt = myConn.prepareStatement( |
| 161 | + "SELECT n.name FROM v$statname n ORDER BY n.name"); |
| 162 | + ResultSet rslt = stmt.executeQuery(); |
| 163 | + while (rslt.next()) { |
| 164 | + stats.add(rslt.getString(1)); |
| 165 | + } |
| 166 | + return stats; |
| 167 | + } |
| 168 | +} |
0 commit comments