📄 profilertimerfilter.java
字号:
long start = timeNow();
nextFilter.sessionIdle(session, status);
long end = timeNow();
sessionIdleTimerWorker.addNewDuration(end - start);
} else {
nextFilter.sessionIdle(session, status);
}
}
/**
* Profile a SessionClosed event. This method will gather the following
* informations :
* - the method duration
* - the shortest execution time
* - the slowest execution time
* - the average execution time
* - the global number of calls
*
* @param nextFilter The filter to call next
* @param session The associated session
*/
@Override
public void sessionClosed(NextFilter nextFilter, IoSession session)
throws Exception {
if (profileSessionClosed) {
long start = timeNow();
nextFilter.sessionClosed(session);
long end = timeNow();
sessionClosedTimerWorker.addNewDuration(end - start);
} else {
nextFilter.sessionClosed(session);
}
}
/**
* Get the average time for the specified method represented by the {@link IoEventType}
*
* @param type
* The {@link IoEventType} that the user wants to get the average method call time
* @return
* The average time it took to execute the method represented by the {@link IoEventType}
*/
public double getAverageTime(IoEventType type) {
switch (type) {
case MESSAGE_RECEIVED :
if (profileMessageReceived) {
return messageReceivedTimerWorker.getAverage();
}
break;
case MESSAGE_SENT :
if (profileMessageSent) {
return messageSentTimerWorker.getAverage();
}
break;
case SESSION_CREATED :
if (profileSessionCreated) {
return sessionCreatedTimerWorker.getAverage();
}
break;
case SESSION_OPENED :
if (profileSessionOpened) {
return sessionOpenedTimerWorker.getAverage();
}
break;
case SESSION_IDLE :
if (profileSessionIdle) {
return sessionIdleTimerWorker.getAverage();
}
break;
case SESSION_CLOSED :
if (profileSessionClosed) {
return sessionClosedTimerWorker.getAverage();
}
break;
}
throw new IllegalArgumentException(
"You are not monitoring this event. Please add this event first.");
}
/**
* Gets the total number of times the method has been called that is represented by the
* {@link IoEventType}
*
* @param type
* The {@link IoEventType} that the user wants to get the total number of method calls
* @return
* The total number of method calls for the method represented by the {@link IoEventType}
*/
public long getTotalCalls(IoEventType type) {
switch (type) {
case MESSAGE_RECEIVED :
if (profileMessageReceived) {
return messageReceivedTimerWorker.getCallsNumber();
}
break;
case MESSAGE_SENT :
if (profileMessageSent) {
return messageSentTimerWorker.getCallsNumber();
}
break;
case SESSION_CREATED :
if (profileSessionCreated) {
return sessionCreatedTimerWorker.getCallsNumber();
}
break;
case SESSION_OPENED :
if (profileSessionOpened) {
return sessionOpenedTimerWorker.getCallsNumber();
}
break;
case SESSION_IDLE :
if (profileSessionIdle) {
return sessionIdleTimerWorker.getCallsNumber();
}
break;
case SESSION_CLOSED :
if (profileSessionClosed) {
return sessionClosedTimerWorker.getCallsNumber();
}
break;
}
throw new IllegalArgumentException(
"You are not monitoring this event. Please add this event first.");
}
/**
* The total time this method has been executing
*
* @param type
* The {@link IoEventType} that the user wants to get the total time this method has
* been executing
* @return
* The total time for the method represented by the {@link IoEventType}
*/
public long getTotalTime(IoEventType type) {
switch (type) {
case MESSAGE_RECEIVED :
if (profileMessageReceived) {
return messageReceivedTimerWorker.getTotal();
}
break;
case MESSAGE_SENT :
if (profileMessageSent) {
return messageSentTimerWorker.getTotal();
}
break;
case SESSION_CREATED :
if (profileSessionCreated) {
return sessionCreatedTimerWorker.getTotal();
}
break;
case SESSION_OPENED :
if (profileSessionOpened) {
return sessionOpenedTimerWorker.getTotal();
}
break;
case SESSION_IDLE :
if (profileSessionIdle) {
return sessionIdleTimerWorker.getTotal();
}
break;
case SESSION_CLOSED :
if (profileSessionClosed) {
return sessionClosedTimerWorker.getTotal();
}
break;
}
throw new IllegalArgumentException(
"You are not monitoring this event. Please add this event first.");
}
/**
* The minimum time the method represented by {@link IoEventType} has executed
*
* @param type
* The {@link IoEventType} that the user wants to get the minimum time this method has
* executed
* @return
* The minimum time this method has executed represented by the {@link IoEventType}
*/
public long getMinimumTime(IoEventType type) {
switch (type) {
case MESSAGE_RECEIVED :
if (profileMessageReceived) {
return messageReceivedTimerWorker.getMinimum();
}
break;
case MESSAGE_SENT :
if (profileMessageSent) {
return messageSentTimerWorker.getMinimum();
}
break;
case SESSION_CREATED :
if (profileSessionCreated) {
return sessionCreatedTimerWorker.getMinimum();
}
break;
case SESSION_OPENED :
if (profileSessionOpened) {
return sessionOpenedTimerWorker.getMinimum();
}
break;
case SESSION_IDLE :
if (profileSessionIdle) {
return sessionIdleTimerWorker.getMinimum();
}
break;
case SESSION_CLOSED :
if (profileSessionClosed) {
return sessionClosedTimerWorker.getMinimum();
}
break;
}
throw new IllegalArgumentException(
"You are not monitoring this event. Please add this event first.");
}
/**
* The maximum time the method represented by {@link IoEventType} has executed
*
* @param type
* The {@link IoEventType} that the user wants to get the maximum time this method has
* executed
* @return
* The maximum time this method has executed represented by the {@link IoEventType}
*/
public long getMaximumTime(IoEventType type) {
switch (type) {
case MESSAGE_RECEIVED :
if (profileMessageReceived) {
return messageReceivedTimerWorker.getMaximum();
}
break;
case MESSAGE_SENT :
if (profileMessageSent) {
return messageSentTimerWorker.getMaximum();
}
break;
case SESSION_CREATED :
if (profileSessionCreated) {
return sessionCreatedTimerWorker.getMaximum();
}
break;
case SESSION_OPENED :
if (profileSessionOpened) {
return sessionOpenedTimerWorker.getMaximum();
}
break;
case SESSION_IDLE :
if (profileSessionIdle) {
return sessionIdleTimerWorker.getMaximum();
}
break;
case SESSION_CLOSED :
if (profileSessionClosed) {
return sessionClosedTimerWorker.getMaximum();
}
break;
}
throw new IllegalArgumentException(
"You are not monitoring this event. Please add this event first.");
}
/**
* Class that will track the time each method takes and be able to provide information
* for each method.
*
*/
private class TimerWorker {
/** The sum of all operation durations */
private final AtomicLong total;
/** The number of calls */
private final AtomicLong callsNumber;
/** The fastest operation */
private final AtomicLong minimum;
/** The slowest operation */
private final AtomicLong maximum;
/** A lock for synchinized blocks */
private final Object lock = new Object();
/**
* Creates a new instance of TimerWorker.
*
*/
public TimerWorker() {
total = new AtomicLong();
callsNumber = new AtomicLong();
minimum = new AtomicLong();
maximum = new AtomicLong();
}
/**
* Add a new operation duration to this class. Total is updated
* and calls is incremented
*
* @param duration
* The new operation duration
*/
public void addNewDuration(long duration) {
callsNumber.incrementAndGet();
total.addAndGet(duration);
synchronized (lock) {
// this is not entirely thread-safe, must lock
if (duration < minimum.longValue()) {
minimum.set(duration);
}
// this is not entirely thread-safe, must lock
if (duration > maximum.longValue()) {
maximum.set(duration);
}
}
}
/**
* Gets the average reading for this event
*
* @return the average reading for this event
*/
public double getAverage() {
synchronized (lock) {
// There are two operations, we need to synchronize the block
return total.longValue() / callsNumber.longValue();
}
}
/**
* Returns the total number of profiled operations
*
* @return The total number of profiled operation
*/
public long getCallsNumber() {
return callsNumber.longValue();
}
/**
* Returns the total time
*
* @return the total time
*/
public long getTotal() {
return total.longValue();
}
/**
* Returns the lowest execution time
*
* @return the lowest execution time
*/
public long getMinimum() {
return minimum.longValue();
}
/**
* Returns the longest execution time
*
* @return the longest execution time
*/
public long getMaximum() {
return maximum.longValue();
}
}
/**
* @return the current time, expressed using the fixed TimeUnit.
*/
private long timeNow() {
switch (timeUnit) {
case SECONDS :
return System.currentTimeMillis()/1000;
case MICROSECONDS :
return System.nanoTime()/1000;
case NANOSECONDS :
return System.nanoTime();
default :
return System.currentTimeMillis();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -