1 /*
   2  * Copyright (c) 1997, 2007, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.org.glassfish.external.statistics.impl;
  27 import com.sun.org.glassfish.external.statistics.TimeStatistic;
  28 import java.util.concurrent.atomic.AtomicLong;
  29 import java.util.Map;
  30 import java.lang.reflect.*;
  31 
  32 /**
  33  * @author Sreenivas Munnangi
  34  */
  35 public final class TimeStatisticImpl extends StatisticImpl
  36     implements TimeStatistic, InvocationHandler {
  37 
  38     private long count = 0L;
  39     private long maxTime = 0L;
  40     private long minTime = 0L;
  41     private long totTime = 0L;
  42     private final long initCount;
  43     private final long initMaxTime;
  44     private final long initMinTime;
  45     private final long initTotTime;
  46 
  47     private final TimeStatistic ts =
  48             (TimeStatistic) Proxy.newProxyInstance(
  49             TimeStatistic.class.getClassLoader(),
  50             new Class[] { TimeStatistic.class },
  51             this);
  52 
  53     public synchronized final String toString() {
  54         return super.toString() + NEWLINE +
  55             "Count: " + getCount() + NEWLINE +
  56             "MinTime: " + getMinTime() + NEWLINE +
  57             "MaxTime: " + getMaxTime() + NEWLINE +
  58             "TotalTime: " + getTotalTime();
  59     }
  60 
  61     public TimeStatisticImpl(long counter, long maximumTime, long minimumTime,
  62                              long totalTime, String name, String unit,
  63                              String desc, long startTime, long sampleTime) {
  64         super(name, unit, desc, startTime, sampleTime);
  65         count = counter;
  66         initCount = counter;
  67         maxTime = maximumTime;
  68         initMaxTime = maximumTime;
  69         minTime = minimumTime;
  70         initMinTime = minimumTime;
  71         totTime = totalTime;
  72         initTotTime = totalTime;
  73     }
  74 
  75     public synchronized TimeStatistic getStatistic() {
  76         return ts;
  77     }
  78 
  79     public synchronized Map getStaticAsMap() {
  80         Map m = super.getStaticAsMap();
  81         m.put("count", getCount());
  82         m.put("maxtime", getMaxTime());
  83         m.put("mintime", getMinTime());
  84         m.put("totaltime", getTotalTime());
  85         return m;
  86     }
  87 
  88      public synchronized void incrementCount(long current) {
  89         if (count == 0) {
  90             totTime = current;
  91             maxTime = current;
  92             minTime = current;
  93         } else {
  94             totTime = totTime + current;
  95             maxTime = (current >= maxTime ? current : maxTime);
  96             minTime = (current >= minTime ? minTime : current);
  97         }
  98         count++;
  99         sampleTime = System.currentTimeMillis();
 100      }
 101 
 102     /**
 103      * Returns the number of times an operation was invoked
 104      */
 105     public synchronized long getCount() {
 106         return count;
 107     }
 108 
 109     /**
 110      * Returns the maximum amount of time that it took for one invocation of an
 111      * operation, since measurement started.
 112      */
 113     public synchronized long getMaxTime() {
 114         return maxTime;
 115     }
 116 
 117     /**
 118      * Returns the minimum amount of time that it took for one invocation of an
 119      * operation, since measurement started.
 120      */
 121     public synchronized long getMinTime() {
 122         return minTime;
 123     }
 124 
 125     /**
 126      * Returns the amount of time that it took for all invocations,
 127      * since measurement started.
 128      */
 129     public synchronized long getTotalTime() {
 130         return totTime;
 131     }
 132 
 133     @Override
 134     public synchronized void reset() {
 135         super.reset();
 136         count = initCount;
 137         maxTime = initMaxTime;
 138         minTime = initMinTime;
 139         totTime = initTotTime;
 140         sampleTime = -1L;
 141     }
 142 
 143     // todo: equals implementation
 144     public Object invoke(Object proxy, Method m, Object[] args) throws Throwable {
 145         Object result;
 146         try {
 147             result = m.invoke(this, args);
 148         } catch (InvocationTargetException e) {
 149             throw e.getTargetException();
 150         } catch (Exception e) {
 151             throw new RuntimeException("unexpected invocation exception: " +
 152                        e.getMessage());
 153         } finally {
 154         }
 155         return result;
 156     }
 157 }