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