src/share/jaxws_classes/com/sun/org/glassfish/external/statistics/impl/TimeStatisticImpl.java

Thu, 31 Aug 2017 15:18:52 +0800

author
aoqi
date
Thu, 31 Aug 2017 15:18:52 +0800
changeset 637
9c07ef4934dd
parent 397
b99d7e355d4b
parent 0
373ffda63c9a
permissions
-rw-r--r--

merge

     1 /*
     2  * Copyright (c) 2009, 2013, 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  */
    28 package com.sun.org.glassfish.external.statistics.impl;
    30 import com.sun.org.glassfish.external.statistics.TimeStatistic;
    31 import java.util.Map;
    32 import java.lang.reflect.*;
    34 /**
    35  * @author Sreenivas Munnangi
    36  */
    37 public final class TimeStatisticImpl extends StatisticImpl
    38     implements TimeStatistic, InvocationHandler {
    40     private long count = 0L;
    41     private long maxTime = 0L;
    42     private long minTime = 0L;
    43     private long totTime = 0L;
    44     private final long initCount;
    45     private final long initMaxTime;
    46     private final long initMinTime;
    47     private final long initTotTime;
    49     private final TimeStatistic ts =
    50             (TimeStatistic) Proxy.newProxyInstance(
    51             TimeStatistic.class.getClassLoader(),
    52             new Class[] { TimeStatistic.class },
    53             this);
    55     public synchronized final String toString() {
    56         return super.toString() + NEWLINE +
    57             "Count: " + getCount() + NEWLINE +
    58             "MinTime: " + getMinTime() + NEWLINE +
    59             "MaxTime: " + getMaxTime() + NEWLINE +
    60             "TotalTime: " + getTotalTime();
    61     }
    63     public TimeStatisticImpl(long counter, long maximumTime, long minimumTime,
    64                              long totalTime, String name, String unit,
    65                              String desc, long startTime, long sampleTime) {
    66         super(name, unit, desc, startTime, sampleTime);
    67         count = counter;
    68         initCount = counter;
    69         maxTime = maximumTime;
    70         initMaxTime = maximumTime;
    71         minTime = minimumTime;
    72         initMinTime = minimumTime;
    73         totTime = totalTime;
    74         initTotTime = totalTime;
    75     }
    77     public synchronized TimeStatistic getStatistic() {
    78         return ts;
    79     }
    81     public synchronized Map getStaticAsMap() {
    82         Map m = super.getStaticAsMap();
    83         m.put("count", getCount());
    84         m.put("maxtime", getMaxTime());
    85         m.put("mintime", getMinTime());
    86         m.put("totaltime", getTotalTime());
    87         return m;
    88     }
    90      public synchronized void incrementCount(long current) {
    91         if (count == 0) {
    92             totTime = current;
    93             maxTime = current;
    94             minTime = current;
    95         } else {
    96             totTime = totTime + current;
    97             maxTime = (current >= maxTime ? current : maxTime);
    98             minTime = (current >= minTime ? minTime : current);
    99         }
   100         count++;
   101         sampleTime = System.currentTimeMillis();
   102      }
   104     /**
   105      * Returns the number of times an operation was invoked
   106      */
   107     public synchronized long getCount() {
   108         return count;
   109     }
   111     /**
   112      * Returns the maximum amount of time that it took for one invocation of an
   113      * operation, since measurement started.
   114      */
   115     public synchronized long getMaxTime() {
   116         return maxTime;
   117     }
   119     /**
   120      * Returns the minimum amount of time that it took for one invocation of an
   121      * operation, since measurement started.
   122      */
   123     public synchronized long getMinTime() {
   124         return minTime;
   125     }
   127     /**
   128      * Returns the amount of time that it took for all invocations,
   129      * since measurement started.
   130      */
   131     public synchronized long getTotalTime() {
   132         return totTime;
   133     }
   135     @Override
   136     public synchronized void reset() {
   137         super.reset();
   138         count = initCount;
   139         maxTime = initMaxTime;
   140         minTime = initMinTime;
   141         totTime = initTotTime;
   142         sampleTime = -1L;
   143     }
   145     // todo: equals implementation
   146     public Object invoke(Object proxy, Method m, Object[] args) throws Throwable {
   147         checkMethod(m);
   149         Object result;
   150         try {
   151             result = m.invoke(this, args);
   152         } catch (InvocationTargetException e) {
   153             throw e.getTargetException();
   154         } catch (Exception e) {
   155             throw new RuntimeException("unexpected invocation exception: " +
   156                        e.getMessage());
   157         }
   158         return result;
   159     }
   160 }

mercurial