001 //$HeadURL: svn+ssh://jwilden@svn.wald.intevation.org/deegree/base/branches/2.5_testing/src/org/deegree/framework/util/ProfilerInterceptor.java $
002 // $Id: ProfilerInterceptor.java 18195 2009-06-18 15:55:39Z mschneider $
003 /*----------------------------------------------------------------------------
004 This file is part of deegree, http://deegree.org/
005 Copyright (C) 2001-2009 by:
006 Department of Geography, University of Bonn
007 and
008 lat/lon GmbH
009
010 This library is free software; you can redistribute it and/or modify it under
011 the terms of the GNU Lesser General Public License as published by the Free
012 Software Foundation; either version 2.1 of the License, or (at your option)
013 any later version.
014 This library is distributed in the hope that it will be useful, but WITHOUT
015 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
016 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
017 details.
018 You should have received a copy of the GNU Lesser General Public License
019 along with this library; if not, write to the Free Software Foundation, Inc.,
020 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
021
022 Contact information:
023
024 lat/lon GmbH
025 Aennchenstr. 19, 53177 Bonn
026 Germany
027 http://lat-lon.de/
028
029 Department of Geography, University of Bonn
030 Prof. Dr. Klaus Greve
031 Postfach 1147, 53001 Bonn
032 Germany
033 http://www.geographie.uni-bonn.de/deegree/
034
035 e-mail: info@deegree.org
036 ----------------------------------------------------------------------------*/
037
038 package org.deegree.framework.util;
039
040 import java.lang.reflect.InvocationTargetException;
041 import java.lang.reflect.Method;
042 import java.text.DecimalFormat;
043 import java.text.NumberFormat;
044 import java.util.HashMap;
045
046 /**
047 * Interceptor to profile the application.
048 *
049 * @author <a href="mailto:tfr@users.sourceforge.net">Torsten Friebe </A>
050 *
051 * @author last edited by: $Author: mschneider $
052 *
053 * @version 2.0, $Revision: 18195 $, $Date: 2009-06-18 17:55:39 +0200 (Do, 18 Jun 2009) $
054 *
055 * @see <a href="http://www.dofactory.com/patterns/PatternChain.aspx">Chain of Responsibility Design Pattern </a>
056 *
057 * @since 2.0
058 */
059 public class ProfilerInterceptor extends Interceptor {
060
061 static {
062 Runtime.getRuntime().addShutdownHook( new Thread( new Runnable() {
063
064 public void run() {
065 printStats();
066 }
067 } ) );
068 }
069
070 /**
071 * data to be used
072 */
073 protected static HashMap<String, ProfileEntry> data = new HashMap<String, ProfileEntry>();
074
075 /**
076 *
077 * @param nextInterceptor
078 */
079 public ProfilerInterceptor( Interceptor nextInterceptor ) {
080 this.nextInterceptor = nextInterceptor;
081 }
082
083 /**
084 *
085 *
086 */
087 protected static void printStats() {
088 NumberFormat zweiDezimalen = new DecimalFormat( "### ###.##" );
089
090 for ( String m : data.keySet() ) {
091 ProfileEntry entry = data.get( m );
092 LOG.logInfo( entry.invocations + " calls " + entry.time + " ms " + " avg "
093 + zweiDezimalen.format( entry.getAverage() ) + m );
094 }
095 }
096
097 /**
098 *
099 * @param m
100 * @return named ProfileEntry
101 */
102 protected static ProfileEntry getEntry( String m ) {
103 if ( data.containsKey( m ) ) {
104 return data.get( m );
105 }
106 ProfileEntry entry = new ProfileEntry();
107 data.put( m, entry );
108 return entry;
109 }
110
111 /**
112 * @param method
113 * @param params
114 * @return -
115 */
116 @Override
117 protected Object handleInvocation( Method method, Object[] params )
118 throws IllegalAccessException, InvocationTargetException {
119 long start = System.currentTimeMillis();
120 Object result = nextInterceptor.handleInvocation( method, params );
121 long duration = System.currentTimeMillis() - start;
122 ProfileEntry entry = getEntry( getTarget().getClass().getName() + "." + method.getName() );
123 entry.time += duration;
124 entry.invocations++;
125 return result;
126 }
127
128 /**
129 * <code>ProfileEntry</code> simple wrapper to be used ad a profile entry
130 *
131 * @author last edited by: $Author: mschneider $
132 *
133 * @version $Revision: 18195 $, $Date: 2009-06-18 17:55:39 +0200 (Do, 18 Jun 2009) $
134 */
135 static class ProfileEntry {
136
137 /**
138 * of the profile entry
139 */
140 long time;
141
142 /**
143 * how many invocations
144 */
145 long invocations;
146
147 /**
148 * @return the average invocations during the last time
149 */
150 public double getAverage() {
151 return (double) time / invocations;
152 }
153
154 }
155 }