Coverage Report - net.admin4j.jdbc.driver.SqlStatementTimerRegistry
 
Classes in this File Line Coverage Branch Coverage Complexity
SqlStatementTimerRegistry
85%
35/41
100%
10/10
1.9
SqlStatementTimerRegistry$1
N/A
N/A
1.9
SqlStatementTimerRegistry$TaskTimerComparable
100%
8/8
100%
4/4
1.9
 
 1  
 /*
 2  
  * This software is licensed under the Apache License, Version 2.0
 3  
  * (the "License") agreement; you may not use this file except in compliance with
 4  
  * the License.  You may obtain a copy of the License at
 5  
  * 
 6  
  *      http://www.apache.org/licenses/LICENSE-2.0
 7  
  * 
 8  
  * Unless required by applicable law or agreed to in writing, software
 9  
  * distributed under the License is distributed on an "AS IS" BASIS,
 10  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 11  
  * See the License for the specific language governing permissions and
 12  
  * limitations under the License.
 13  
  */
 14  
 package net.admin4j.jdbc.driver;
 15  
 
 16  
 import java.util.ArrayList;
 17  
 import java.util.Comparator;
 18  
 import java.util.HashMap;
 19  
 import java.util.HashSet;
 20  
 import java.util.Map;
 21  
 import java.util.Set;
 22  
 import java.util.TreeSet;
 23  
 import java.util.concurrent.ConcurrentHashMap;
 24  
 
 25  
 import net.admin4j.config.Admin4JConfiguration;
 26  
 import net.admin4j.deps.commons.lang3.Validate;
 27  
 import net.admin4j.timer.DataMeasure;
 28  
 import net.admin4j.timer.TaskTimer;
 29  
 import net.admin4j.vo.DataMeasurementSummaryVO;
 30  
 
 31  
 import org.slf4j.Logger;
 32  
 import org.slf4j.LoggerFactory;
 33  
 
 34  
 /**
 35  
  * Tracks performance metrics for SQL Statements
 36  
  * @author D. Ashmore
 37  
  *
 38  
  */
 39  228
 class SqlStatementTimerRegistry {
 40  
     
 41  12
     private static final SqlStatementTimerRegistry singleton = new SqlStatementTimerRegistry();
 42  12
     private Map<String, SqlTaskTimer> registeredDataMeasures
 43  
         = new ConcurrentHashMap<String, SqlTaskTimer>();
 44  
     
 45  12
     public static final Integer DEFAULT_NBR_SQL_STATEMENTS = 50; 
 46  12
     private static Integer nbrRetainedSqlStatements = DEFAULT_NBR_SQL_STATEMENTS;
 47  
     
 48  12
     public static final Long DEFAULT_SQL_RETENTION_TIME_IN_MILLIS = 60L * 60L * 6L * 1000L; // 6 hrs
 49  12
     private static Long sqlRetentionTimeInMillis = DEFAULT_SQL_RETENTION_TIME_IN_MILLIS;
 50  
     
 51  
     static {
 52  12
         if (Admin4JConfiguration.getSqlNbrRetainedSqlStatements() != null) {
 53  0
             nbrRetainedSqlStatements = Admin4JConfiguration.getSqlNbrRetainedSqlStatements();
 54  
         }
 55  12
         if (Admin4JConfiguration.getSqlRetentionTimeInMillis() != null) {
 56  0
             sqlRetentionTimeInMillis = Admin4JConfiguration.getSqlRetentionTimeInMillis();
 57  
         }
 58  12
     }
 59  
     
 60  
     public static void register(SqlTaskTimer timer) {
 61  36
         singleton.registeredDataMeasures.put(timer.getLabel(), timer);
 62  36
     }
 63  
     
 64  
     public static SqlTaskTimer findTaskTimer(String label) {
 65  
         Validate.notEmpty(label, "Null or blank label not allowed.");
 66  180138
         return singleton.registeredDataMeasures.get(label);
 67  
     }
 68  
     
 69  
     public static void delete(String label) {
 70  0
         singleton.registeredDataMeasures.remove(label);
 71  0
     }
 72  
     
 73  
     public static Map<String,Set<DataMeasurementSummaryVO>> getDataSummaryMap() {
 74  18
         TreeSet<SqlTaskTimer> set = new TreeSet<SqlTaskTimer>(new TaskTimerComparable());
 75  18
         set.addAll(singleton.registeredDataMeasures.values());
 76  18
         HashSet<SqlTaskTimer> topSet = new HashSet<SqlTaskTimer>();
 77  18
         for (SqlTaskTimer timer: set) {
 78  70
             if (topSet.size() < nbrRetainedSqlStatements && extractDataMeasure(timer).getLastObservationTime() > createEarliestTime()) {
 79  49
                 topSet.add(timer);
 80  
             }
 81  
         }
 82  18
         Map<String,Set<DataMeasurementSummaryVO>> summaryMap = new HashMap<String,Set<DataMeasurementSummaryVO>>();
 83  
         
 84  
         Set<DataMeasurementSummaryVO> summarySet;
 85  
         DataMeasurementSummaryVO summary;
 86  18
         for (SqlTaskTimer timer :topSet) {
 87  49
             summarySet = new HashSet<DataMeasurementSummaryVO>();
 88  49
             summaryMap.put(timer.getDriverContext() + "-" + timer.getLabel(), summarySet);
 89  49
             for (DataMeasure measure: timer.getDataMeasures()) {
 90  49
                 summary = measure.getDataMeasurementSummary();
 91  49
                 summary.setLabel(timer.getLabel());
 92  49
                 summarySet.add(summary);
 93  
             }
 94  
         }
 95  18
         return summaryMap;
 96  
     }
 97  
     
 98  
     private static Long createEarliestTime() {
 99  64
         return System.currentTimeMillis() - sqlRetentionTimeInMillis;
 100  
     }
 101  
     
 102  
     protected static void clearAll() {
 103  0
         singleton.registeredDataMeasures.clear();
 104  0
     }
 105  
     
 106  144
     private static class TaskTimerComparable implements Comparator<TaskTimer> {
 107  
 
 108  
         /* (non-Javadoc)
 109  
          * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
 110  
          */
 111  
         public int compare(TaskTimer timer1, TaskTimer timer2) {
 112  108
             SqlStatementSummaryDataMeasure measure1 = extractDataMeasure(timer1);
 113  108
             SqlStatementSummaryDataMeasure measure2 = extractDataMeasure(timer2);
 114  
             
 115  
             // Intentionally reverse order.
 116  108
             if (measure1.getTotal() < measure2.getTotal()) {
 117  46
                 return 1;
 118  
             }
 119  62
             else if (measure1.getTotal() > measure2.getTotal()) {
 120  12
                 return -1;
 121  
             }
 122  
             else {
 123  50
                 return -1 * timer1.getLabel().compareTo(timer2.getLabel());
 124  
             }
 125  
         }
 126  
         
 127  
     }
 128  
     
 129  
     @SuppressWarnings({ "unchecked", "rawtypes" })
 130  
     private static SqlStatementSummaryDataMeasure extractDataMeasure(
 131  
             TaskTimer timer1) {
 132  280
         return (SqlStatementSummaryDataMeasure)new ArrayList(timer1.getDataMeasures()).get(0);
 133  
     }
 134  
 
 135  
     protected static void setNbrRetainedSqlStatements(Integer nbrSqlStatements) {
 136  3
         SqlStatementTimerRegistry.nbrRetainedSqlStatements = nbrSqlStatements;
 137  3
     }
 138  
 
 139  
     protected static void setSqlRetentionTimeInMillis(Long sqlRetentionTimeInMillis) {
 140  3
         SqlStatementTimerRegistry.sqlRetentionTimeInMillis = sqlRetentionTimeInMillis;
 141  3
     }
 142  
     
 143  
 }