Author: trustin
Date: Fri Jul 27 10:31:13 2007
New Revision: 560327
URL: http://svn.apache.org/viewvc?view=rev&rev=560327
Log:
Reformatted ProfileTimerFilter
Modified:
mina/trunk/core/src/main/java/org/apache/mina/filter/statistic/ProfilerTimerFilter.java
Modified:
mina/trunk/core/src/main/java/org/apache/mina/filter/statistic/ProfilerTimerFilter.java
URL:
http://svn.apache.org/viewvc/mina/trunk/core/src/main/java/org/apache/mina/filter/statistic/ProfilerTimerFilter.java?view=diff&rev=560327&r1=560326&r2=560327
==============================================================================
---
mina/trunk/core/src/main/java/org/apache/mina/filter/statistic/ProfilerTimerFilter.java
(original)
+++
mina/trunk/core/src/main/java/org/apache/mina/filter/statistic/ProfilerTimerFilter.java
Fri Jul 27 10:31:13 2007
@@ -19,7 +19,6 @@
*/
package org.apache.mina.filter.statistic;
-
import java.util.EnumSet;
import java.util.HashMap;
import java.util.concurrent.TimeUnit;
@@ -31,7 +30,6 @@
import org.apache.mina.common.IoSession;
import org.apache.mina.common.WriteRequest;
-
/**
* This class will measure, the time it takes for a
* method in the [EMAIL PROTECTED] IoFilterAdapter} class to execute. The
basic
@@ -48,26 +46,25 @@
* @author <a href="mailto:[EMAIL PROTECTED]">Apache Directory Project</a>
* @version $Rev$, $Date$
*/
-public class ProfilerTimerFilter extends IoFilterAdapter
-{
+public class ProfilerTimerFilter extends IoFilterAdapter {
private EnumSet<IoEventType> eventsToProfile;
+
private ProfilerTimerUnit timeUnit;
- private HashMap<IoEventType,TimerWorker> timerManager;
-
+ private HashMap<IoEventType, TimerWorker> timerManager;
+
/**
* Creates a new instance of ProfilerFilter. This is the
* default constructor and will print out timings for
* messageReceived and messageSent and the time increment
* will be in milliseconds.
*/
- public ProfilerTimerFilter()
- {
- this( EnumSet.of( IoEventType.MESSAGE_RECEIVED,
IoEventType.MESSAGE_SENT ),
- TimeUnit.MILLISECONDS );
+ public ProfilerTimerFilter() {
+ this(
+ EnumSet.of(IoEventType.MESSAGE_RECEIVED,
+ IoEventType.MESSAGE_SENT), TimeUnit.MILLISECONDS);
}
-
/**
* Creates a new instance of ProfilerFilter. An example
* of this call would be:
@@ -82,36 +79,34 @@
* @param unit
* Used to determine the level of precision you need in your timing.
*/
- public ProfilerTimerFilter( EnumSet<IoEventType> eventsToProfile, TimeUnit
unit )
- {
+ public ProfilerTimerFilter(EnumSet<IoEventType> eventsToProfile,
+ TimeUnit unit) {
this.eventsToProfile = eventsToProfile;
-
- setTimeUnit( unit );
-
- timerManager = new HashMap<IoEventType,TimerWorker>();
-
- for( IoEventType type : eventsToProfile ){
- timerManager.put( type, new TimerWorker() );
+
+ setTimeUnit(unit);
+
+ timerManager = new HashMap<IoEventType, TimerWorker>();
+
+ for (IoEventType type : eventsToProfile) {
+ timerManager.put(type, new TimerWorker());
}
}
-
/**
* Sets the [EMAIL PROTECTED] ProfilerTimerUnit} being used.
*
* @param timeUnit
* Sets the new [EMAIL PROTECTED] ProfilerTimerUnit} to be used.
*/
- public void setTimeUnit( TimeUnit unit )
- {
- if( unit == TimeUnit.MILLISECONDS ){
+ public void setTimeUnit(TimeUnit unit) {
+ if (unit == TimeUnit.MILLISECONDS) {
this.timeUnit = ProfilerTimerUnit.MILLISECONDS;
- } else if( unit == TimeUnit.NANOSECONDS ){
+ } else if (unit == TimeUnit.NANOSECONDS) {
this.timeUnit = ProfilerTimerUnit.NANOSECONDS;
- } else if( unit == TimeUnit.SECONDS ){
+ } else if (unit == TimeUnit.SECONDS) {
this.timeUnit = ProfilerTimerUnit.SECONDS;
} else {
- throw new IllegalArgumentException( "Invalid Time specified" );
+ throw new IllegalArgumentException("Invalid Time specified");
}
}
@@ -121,22 +116,22 @@
* @param type
* The [EMAIL PROTECTED] IoEventType} to profile
*/
- public void addEventToProfile( IoEventType type ){
- if( !timerManager.containsKey( type )){
- timerManager.put( type, new TimerWorker() );
+ public void addEventToProfile(IoEventType type) {
+ if (!timerManager.containsKey(type)) {
+ timerManager.put(type, new TimerWorker());
}
}
-
+
/**
* Remove an [EMAIL PROTECTED] IoEventType} to profile
*
* @param type
* The [EMAIL PROTECTED] IoEventType} to profile
*/
- public void removeEventToProfile( IoEventType type ){
- timerManager.remove( type );
+ public void removeEventToProfile(IoEventType type) {
+ timerManager.remove(type);
}
-
+
/**
* Return the bitmask that is being used to display
* timing information for this filter.
@@ -144,12 +139,10 @@
* @return
* An int representing the methods that will be logged
*/
- public EnumSet<IoEventType> getEventsToProfile()
- {
+ public EnumSet<IoEventType> getEventsToProfile() {
return eventsToProfile;
}
-
/**
* Set the bitmask in order to tell this filter which
* methods to print out timing information
@@ -157,81 +150,80 @@
* @param eventsToProfile
* An int representing the new methods that should be logged
*/
- public void setEventsToProfile( EnumSet<IoEventType> methodsToLog )
- {
+ public void setEventsToProfile(EnumSet<IoEventType> methodsToLog) {
this.eventsToProfile = methodsToLog;
}
-
@Override
- public void messageReceived( NextFilter nextFilter, IoSession session,
Object message ) throws Exception
- {
+ public void messageReceived(NextFilter nextFilter, IoSession session,
+ Object message) throws Exception {
long start = timeUnit.timeNow();
- nextFilter.messageReceived( session, message );
+ nextFilter.messageReceived(session, message);
long end = timeUnit.timeNow();
- if ( getEventsToProfile().contains( IoEventType.MESSAGE_RECEIVED ) )
- timerManager.get( IoEventType.MESSAGE_RECEIVED ).addNewReading(
end-start );
+ if (getEventsToProfile().contains(IoEventType.MESSAGE_RECEIVED))
+ timerManager.get(IoEventType.MESSAGE_RECEIVED).addNewReading(
+ end - start);
}
-
@Override
- public void messageSent( NextFilter nextFilter, IoSession session,
WriteRequest writeRequest ) throws Exception
- {
+ public void messageSent(NextFilter nextFilter, IoSession session,
+ WriteRequest writeRequest) throws Exception {
long start = timeUnit.timeNow();
- nextFilter.messageSent( session, writeRequest );
+ nextFilter.messageSent(session, writeRequest);
long end = timeUnit.timeNow();
- if ( getEventsToProfile().contains( IoEventType.MESSAGE_SENT ) )
- timerManager.get( IoEventType.MESSAGE_SENT ).addNewReading(
end-start );
+ if (getEventsToProfile().contains(IoEventType.MESSAGE_SENT))
+ timerManager.get(IoEventType.MESSAGE_SENT).addNewReading(
+ end - start);
}
-
@Override
- public void sessionClosed( NextFilter nextFilter, IoSession session )
throws Exception
- {
+ public void sessionClosed(NextFilter nextFilter, IoSession session)
+ throws Exception {
long start = timeUnit.timeNow();
- nextFilter.sessionClosed( session );
+ nextFilter.sessionClosed(session);
long end = timeUnit.timeNow();
- if ( getEventsToProfile().contains( IoEventType.SESSION_CLOSED ) )
- timerManager.get( IoEventType.SESSION_CLOSED ).addNewReading(
end-start );
+ if (getEventsToProfile().contains(IoEventType.SESSION_CLOSED))
+ timerManager.get(IoEventType.SESSION_CLOSED).addNewReading(
+ end - start);
}
-
@Override
- public void sessionCreated( NextFilter nextFilter, IoSession session )
throws Exception
- {
+ public void sessionCreated(NextFilter nextFilter, IoSession session)
+ throws Exception {
long start = timeUnit.timeNow();
- nextFilter.sessionCreated( session );
+ nextFilter.sessionCreated(session);
long end = timeUnit.timeNow();
- if ( getEventsToProfile().contains( IoEventType.SESSION_CREATED ) )
- timerManager.get( IoEventType.SESSION_CREATED ).addNewReading(
end-start );
+ if (getEventsToProfile().contains(IoEventType.SESSION_CREATED))
+ timerManager.get(IoEventType.SESSION_CREATED).addNewReading(
+ end - start);
}
-
@Override
- public void sessionIdle( NextFilter nextFilter, IoSession session,
IdleStatus status ) throws Exception
- {
+ public void sessionIdle(NextFilter nextFilter, IoSession session,
+ IdleStatus status) throws Exception {
long start = timeUnit.timeNow();
- nextFilter.sessionIdle( session, status );
+ nextFilter.sessionIdle(session, status);
long end = timeUnit.timeNow();
- if ( getEventsToProfile().contains( IoEventType.SESSION_IDLE ) )
- timerManager.get( IoEventType.SESSION_IDLE ).addNewReading(
end-start );
+ if (getEventsToProfile().contains(IoEventType.SESSION_IDLE))
+ timerManager.get(IoEventType.SESSION_IDLE).addNewReading(
+ end - start);
}
-
@Override
- public void sessionOpened( NextFilter nextFilter, IoSession session )
throws Exception
- {
+ public void sessionOpened(NextFilter nextFilter, IoSession session)
+ throws Exception {
long start = timeUnit.timeNow();
- nextFilter.sessionOpened( session );
+ nextFilter.sessionOpened(session);
long end = timeUnit.timeNow();
- if ( getEventsToProfile().contains( IoEventType.SESSION_OPENED ) )
- timerManager.get( IoEventType.SESSION_OPENED ).addNewReading(
end-start );
+ if (getEventsToProfile().contains(IoEventType.SESSION_OPENED))
+ timerManager.get(IoEventType.SESSION_OPENED).addNewReading(
+ end - start);
}
/**
@@ -242,14 +234,15 @@
* @return
* The average time it took to execute the method represented by the
[EMAIL PROTECTED] IoEventType}
*/
- public double getAverageTime( IoEventType type ){
- if( !timerManager.containsKey( type )){
- throw new IllegalArgumentException("You are not monitoring this
event. Please add this event first.");
+ public double getAverageTime(IoEventType type) {
+ if (!timerManager.containsKey(type)) {
+ throw new IllegalArgumentException(
+ "You are not monitoring this event. Please add this event
first.");
}
-
- return timerManager.get( type ).getAverage();
+
+ return timerManager.get(type).getAverage();
}
-
+
/**
* Gets the total number of times the method has been called that is
represented by the
* [EMAIL PROTECTED] IoEventType}
@@ -259,14 +252,15 @@
* @return
* The total number of method calls for the method represented by the
[EMAIL PROTECTED] IoEventType}
*/
- public long getTotalCalls( IoEventType type ){
- if( !timerManager.containsKey( type )){
- throw new IllegalArgumentException("You are not monitoring this
event. Please add this event first.");
+ public long getTotalCalls(IoEventType type) {
+ if (!timerManager.containsKey(type)) {
+ throw new IllegalArgumentException(
+ "You are not monitoring this event. Please add this event
first.");
}
-
- return timerManager.get( type ).getCalls();
+
+ return timerManager.get(type).getCalls();
}
-
+
/**
* The total time this method has been executing
*
@@ -276,14 +270,15 @@
* @return
* The total time for the method represented by the [EMAIL PROTECTED]
IoEventType}
*/
- public long getTotalTime( IoEventType type ){
- if( !timerManager.containsKey( type )){
- throw new IllegalArgumentException("You are not monitoring this
event. Please add this event first.");
+ public long getTotalTime(IoEventType type) {
+ if (!timerManager.containsKey(type)) {
+ throw new IllegalArgumentException(
+ "You are not monitoring this event. Please add this event
first.");
}
-
- return timerManager.get( type ).getTotal();
+
+ return timerManager.get(type).getTotal();
}
-
+
/**
* The minimum time the method represented by [EMAIL PROTECTED]
IoEventType} has executed
*
@@ -293,14 +288,15 @@
* @return
* The minimum time this method has executed represented by the [EMAIL
PROTECTED] IoEventType}
*/
- public long getMinValue( IoEventType type ){
- if( !timerManager.containsKey( type )){
- throw new IllegalArgumentException("You are not monitoring this
event. Please add this event first.");
+ public long getMinValue(IoEventType type) {
+ if (!timerManager.containsKey(type)) {
+ throw new IllegalArgumentException(
+ "You are not monitoring this event. Please add this event
first.");
}
-
- return timerManager.get( type ).getMin();
+
+ return timerManager.get(type).getMin();
}
-
+
/**
* The maximum time the method represented by [EMAIL PROTECTED]
IoEventType} has executed
*
@@ -310,38 +306,43 @@
* @return
* The maximum time this method has executed represented by the [EMAIL
PROTECTED] IoEventType}
*/
- public long getMaxValue( IoEventType type ){
- if( !timerManager.containsKey( type )){
- throw new IllegalArgumentException("You are not monitoring this
event. Please add this event first.");
+ public long getMaxValue(IoEventType type) {
+ if (!timerManager.containsKey(type)) {
+ throw new IllegalArgumentException(
+ "You are not monitoring this event. Please add this event
first.");
}
-
- return timerManager.get( type ).getMax();
+
+ return timerManager.get(type).getMax();
}
-
+
/**
* Class that will track the time each method takes and be able to provide
information
* for each method.
*
*/
class TimerWorker {
-
+
private AtomicLong total;
+
private AtomicLong calls;
+
private AtomicLong min;
+
private AtomicLong max;
+
private Object lock = new Object();
-
+
/**
* Creates a new instance of TimerWorker.
*
*/
- public TimerWorker(){
+ public TimerWorker() {
total = new AtomicLong();
calls = new AtomicLong();
min = new AtomicLong();
max = new AtomicLong();
}
-
+
/**
* Add a new reading to this class. Total is updated
* and calls is incremented
@@ -349,112 +350,99 @@
* @param newReading
* The new reading
*/
- public void addNewReading( long newReading ){
+ public void addNewReading(long newReading) {
calls.incrementAndGet();
- total.addAndGet( newReading );
-
- synchronized( lock ){
+ total.addAndGet(newReading);
+
+ synchronized (lock) {
// this is not entirely thread-safe, must lock
- if( newReading < min.longValue() ){
- min.set( newReading );
+ if (newReading < min.longValue()) {
+ min.set(newReading);
}
-
+
// this is not entirely thread-safe, must lock
- if( newReading > max.longValue() ){
- max.set( newReading );
+ if (newReading > max.longValue()) {
+ max.set(newReading);
}
}
}
-
+
/**
* Gets the average reading for this event
*
* @return
* Gets the average reading for this event
*/
- public double getAverage(){
+ public double getAverage() {
return total.longValue() / calls.longValue();
}
-
+
/**
* Returns the total number of readings
*
* @return
* total number of readings
*/
- public long getCalls(){
+ public long getCalls() {
return calls.longValue();
}
-
+
/**
* Returns the total time
*
* @return
* the total time
*/
- public long getTotal(){
+ public long getTotal() {
return total.longValue();
}
-
+
/**
* Returns the minimum value
*
* @return
* the minimum value
*/
- public long getMin(){
+ public long getMin() {
return min.longValue();
}
-
+
/**
* Returns the maximum value
*
* @return
* the maximum value
*/
- public long getMax(){
+ public long getMax() {
return max.longValue();
}
}
-
- enum ProfilerTimerUnit
- {
- SECONDS
- {
- public long timeNow()
- {
+
+ enum ProfilerTimerUnit {
+ SECONDS {
+ public long timeNow() {
return System.currentTimeMillis() / 1000;
}
-
- public String getDescription()
- {
+ public String getDescription() {
return "seconds";
}
},
- MILLISECONDS
- {
- public long timeNow()
- {
+ MILLISECONDS {
+ public long timeNow() {
return System.currentTimeMillis();
}
-
- public String getDescription()
- {
+ public String getDescription() {
return "milliseconds";
}
},
- NANOSECONDS
- {
- public long timeNow()
- {
+ NANOSECONDS {
+ public long timeNow() {
return System.nanoTime();
}
-
- public String getDescription()
- {
+ public String getDescription() {
return "nanoseconds";
}
};
@@ -470,14 +458,11 @@
* enum classes should not be listed as abstract), method convert
* etc. are not declared abstract but otherwise act as abstract
methods.
*/
- public long timeNow()
- {
+ public long timeNow() {
throw new AbstractMethodError();
}
-
- public String getDescription()
- {
+ public String getDescription() {
throw new AbstractMethodError();
}
}