aoqi@0: /* aoqi@0: * Copyright (c) 2009, 2013, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. Oracle designates this aoqi@0: * particular file as subject to the "Classpath" exception as provided aoqi@0: * by Oracle in the LICENSE file that accompanied this code. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: aoqi@0: aoqi@0: package com.sun.org.glassfish.external.statistics.impl; aoqi@0: aoqi@0: import com.sun.org.glassfish.external.statistics.TimeStatistic; aoqi@0: import java.util.Map; aoqi@0: import java.lang.reflect.*; aoqi@0: aoqi@0: /** aoqi@0: * @author Sreenivas Munnangi aoqi@0: */ aoqi@0: public final class TimeStatisticImpl extends StatisticImpl aoqi@0: implements TimeStatistic, InvocationHandler { aoqi@0: aoqi@0: private long count = 0L; aoqi@0: private long maxTime = 0L; aoqi@0: private long minTime = 0L; aoqi@0: private long totTime = 0L; aoqi@0: private final long initCount; aoqi@0: private final long initMaxTime; aoqi@0: private final long initMinTime; aoqi@0: private final long initTotTime; aoqi@0: aoqi@0: private final TimeStatistic ts = aoqi@0: (TimeStatistic) Proxy.newProxyInstance( aoqi@0: TimeStatistic.class.getClassLoader(), aoqi@0: new Class[] { TimeStatistic.class }, aoqi@0: this); aoqi@0: aoqi@0: public synchronized final String toString() { aoqi@0: return super.toString() + NEWLINE + aoqi@0: "Count: " + getCount() + NEWLINE + aoqi@0: "MinTime: " + getMinTime() + NEWLINE + aoqi@0: "MaxTime: " + getMaxTime() + NEWLINE + aoqi@0: "TotalTime: " + getTotalTime(); aoqi@0: } aoqi@0: aoqi@0: public TimeStatisticImpl(long counter, long maximumTime, long minimumTime, aoqi@0: long totalTime, String name, String unit, aoqi@0: String desc, long startTime, long sampleTime) { aoqi@0: super(name, unit, desc, startTime, sampleTime); aoqi@0: count = counter; aoqi@0: initCount = counter; aoqi@0: maxTime = maximumTime; aoqi@0: initMaxTime = maximumTime; aoqi@0: minTime = minimumTime; aoqi@0: initMinTime = minimumTime; aoqi@0: totTime = totalTime; aoqi@0: initTotTime = totalTime; aoqi@0: } aoqi@0: aoqi@0: public synchronized TimeStatistic getStatistic() { aoqi@0: return ts; aoqi@0: } aoqi@0: aoqi@0: public synchronized Map getStaticAsMap() { aoqi@0: Map m = super.getStaticAsMap(); aoqi@0: m.put("count", getCount()); aoqi@0: m.put("maxtime", getMaxTime()); aoqi@0: m.put("mintime", getMinTime()); aoqi@0: m.put("totaltime", getTotalTime()); aoqi@0: return m; aoqi@0: } aoqi@0: aoqi@0: public synchronized void incrementCount(long current) { aoqi@0: if (count == 0) { aoqi@0: totTime = current; aoqi@0: maxTime = current; aoqi@0: minTime = current; aoqi@0: } else { aoqi@0: totTime = totTime + current; aoqi@0: maxTime = (current >= maxTime ? current : maxTime); aoqi@0: minTime = (current >= minTime ? minTime : current); aoqi@0: } aoqi@0: count++; aoqi@0: sampleTime = System.currentTimeMillis(); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Returns the number of times an operation was invoked aoqi@0: */ aoqi@0: public synchronized long getCount() { aoqi@0: return count; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Returns the maximum amount of time that it took for one invocation of an aoqi@0: * operation, since measurement started. aoqi@0: */ aoqi@0: public synchronized long getMaxTime() { aoqi@0: return maxTime; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Returns the minimum amount of time that it took for one invocation of an aoqi@0: * operation, since measurement started. aoqi@0: */ aoqi@0: public synchronized long getMinTime() { aoqi@0: return minTime; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Returns the amount of time that it took for all invocations, aoqi@0: * since measurement started. aoqi@0: */ aoqi@0: public synchronized long getTotalTime() { aoqi@0: return totTime; aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: public synchronized void reset() { aoqi@0: super.reset(); aoqi@0: count = initCount; aoqi@0: maxTime = initMaxTime; aoqi@0: minTime = initMinTime; aoqi@0: totTime = initTotTime; aoqi@0: sampleTime = -1L; aoqi@0: } aoqi@0: aoqi@0: // todo: equals implementation aoqi@0: public Object invoke(Object proxy, Method m, Object[] args) throws Throwable { aoqi@0: checkMethod(m); aoqi@0: aoqi@0: Object result; aoqi@0: try { aoqi@0: result = m.invoke(this, args); aoqi@0: } catch (InvocationTargetException e) { aoqi@0: throw e.getTargetException(); aoqi@0: } catch (Exception e) { aoqi@0: throw new RuntimeException("unexpected invocation exception: " + aoqi@0: e.getMessage()); aoqi@0: } aoqi@0: return result; aoqi@0: } aoqi@0: }