Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Now PeriodicalLog allows explicit mode settings. Closes #13.

  • Loading branch information...
commit 4a96b274b1479ce4ad077459b6bc6f4b35592adc 1 parent 20a8700
Janne Jalkanen authored
Showing with 96 additions and 34 deletions.
  1. +13 −2 README
  2. +83 −32 src/main/java/com/ecyrd/speed4j/log/PeriodicalLog.java
15 README
View
@@ -178,8 +178,19 @@ TRICKS AND TIPS
Q: I want to see just the JMX data, not the logging.
-A: Set the slf4jLogname to an empty string. For example
- speed4j.loggingFactory.slf4jLogname=
+A: Configure the PeriodicalLog's mode with
+
+ log.setMode( PeriodicalLog.Mode.JMX_ONLY );
+
+ or from a property file
+
+ mylog.mode = JMX_ONLY
+
+
+Q: Ok, so how about not showing JMX statistics?
+
+A: Just use mode "LOG_ONLY". By default, Speed4j is in "ALL" mode, but you
+ can also set it to "QUIET" if you don't want any logging to occur.
Q: It seems that there are a lot of StopWatches in memory! Memory leak! Waaaa!
115 src/main/java/com/ecyrd/speed4j/log/PeriodicalLog.java
View
@@ -88,6 +88,8 @@
private static Logger log = LoggerFactory.getLogger( PeriodicalLog.class );
+ private Mode m_mode = Mode.ALL;
+
/**
* Creates an instance of PeriodicalLog.
*/
@@ -129,6 +131,39 @@ public void setJmx( String value )
}
/**
+ * You may set the mode for the PeriodicalLog. This can be
+ * <ul>
+ * <li>QUIET - when you don't want any logging</li>
+ * <li>JMX_ONLY - when you want JMX only to show up</li>
+ * <li>LOG_ONLY - when you want just log, not JMX</li>
+ * <li>ALL - When you want both JMX and log</li>
+ * </ul>
+ * <p>
+ * By default PeriodicalLog is in ALL mode.
+ *
+ * @param mode One of the above strings. If the string cannot
+ * be recognized, the value is ignored.
+ */
+ public void setMode( String mode )
+ {
+ Mode m = Mode.valueOf( mode );
+
+ if( m != null )
+ m_mode = m;
+ }
+
+ /**
+ * You can set the mode also directly, see {@link #setMode(String)}.
+ *
+ * @param mode Mode to set.
+ */
+ public void setMode( Mode mode )
+ {
+ if( mode != null )
+ m_mode = mode;
+ }
+
+ /**
* For limiting the queue size in case the calculation is simply too slow
* and events are gathering too fast. Once the queue becomes too big,
* events are silently dropped, and the JMX attribute {@value #JMX_DROPPED_STOPWATCHES} will be
@@ -283,51 +318,58 @@ private boolean emptyQueue(long finalMoment)
*/
private void doLog(long lastRun, long finalMoment)
{
- if( m_log == null || !m_log.isInfoEnabled() )
+ //
+ // Do logging, if requested.
+ //
+ if( m_mode == Mode.LOG_ONLY || m_mode == Mode.ALL )
{
- resetForNextPeriod();
- return;
- }
+ if( m_log != null && m_log.isInfoEnabled() )
+ {
+ printf("Statistics from %tc to %tc", new Date(lastRun), new Date(finalMoment));
- printf("Statistics from %tc to %tc", new Date(lastRun), new Date(finalMoment));
+ printf("Tag Avg(ms) Min Max Std Dev 95th Count");
- printf("Tag Avg(ms) Min Max Std Dev 95th Count");
+ for( Map.Entry<String,CollectedStatistics> e : m_stats.entrySet() )
+ {
+ CollectedStatistics cs = e.getValue();
+ printf("%-60s %8.2f %8.2f %8.2f %8.2f %8.2f %7d", e.getKey(),cs.getAverageMS(), cs.getMin(), cs.getMax(), cs.getStdDev(), cs.getPercentile( 95 ), cs.getInvocations());
+ }
- for( Map.Entry<String,CollectedStatistics> e : m_stats.entrySet() )
- {
- CollectedStatistics cs = e.getValue();
- printf("%-60s %8.2f %8.2f %8.2f %8.2f %8.2f %7d", e.getKey(),cs.getAverageMS(), cs.getMin(), cs.getMax(), cs.getStdDev(), cs.getPercentile( 95 ), cs.getInvocations());
+ printf("");
+ }
}
//
- // Finally, store these to the JMX attribute list
+ // Store these to the JMX attribute list
//
- if( m_jmxAttributes != null )
+ if( m_mode == Mode.JMX_ONLY || m_mode == Mode.ALL )
{
- m_jmxStatistics = new ConcurrentHashMap<String, PeriodicalLog.JmxStatistics>();
-
- for( String name : m_jmxAttributes )
+ if( m_jmxAttributes != null )
{
- // TODO: Unfortunately we now calculate the stddev and 95th percentile twice, which is a bit of overhead.
- String n = name.trim();
- CollectedStatistics cs = m_stats.get(n);
-
- if ( cs == null )
- continue;
-
- JmxStatistics js = new JmxStatistics();
- js.count = cs.getInvocations();
- js.max = cs.getMax();
- js.min = cs.getMin();
- js.mean = cs.getAverageMS();
- js.perc95 = cs.getPercentile( 95 );
- js.stddev = cs.getStdDev();
- m_jmxStatistics.put(n, js);
+ m_jmxStatistics = new ConcurrentHashMap<String, PeriodicalLog.JmxStatistics>();
+
+ for( String name : m_jmxAttributes )
+ {
+ // TODO: Unfortunately we now calculate the stddev and 95th percentile twice, which is a bit of overhead.
+ String n = name.trim();
+ CollectedStatistics cs = m_stats.get(n);
+
+ if ( cs == null )
+ continue;
+
+ JmxStatistics js = new JmxStatistics();
+ js.count = cs.getInvocations();
+ js.max = cs.getMax();
+ js.min = cs.getMin();
+ js.mean = cs.getAverageMS();
+ js.perc95 = cs.getPercentile( 95 );
+ js.stddev = cs.getStdDev();
+ m_jmxStatistics.put(n, js);
+ }
}
}
- printf("");
resetForNextPeriod();
}
@@ -578,5 +620,14 @@ private void buildMBeanInfo() throws IntrospectionException
public double perc95;
}
-
+ /**
+ * Describes the possible modes in which this system can be.
+ */
+ public static enum Mode
+ {
+ QUIET,
+ JMX_ONLY,
+ LOG_ONLY,
+ ALL
+ }
}
Please sign in to comment.
Something went wrong with that request. Please try again.