commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nico...@apache.org
Subject svn commit: r745420 - in /commons/sandbox/monitoring/branches/modules: core/src/main/java/org/apache/commons/monitoring/ core/src/main/java/org/apache/commons/monitoring/stopwatches/ core/src/test/java/org/apache/commons/monitoring/ instrumentation/src...
Date Wed, 18 Feb 2009 08:14:30 GMT
Author: nicolas
Date: Wed Feb 18 08:14:29 2009
New Revision: 745420

URL: http://svn.apache.org/viewvc?rev=745420&view=rev
Log:
Cleaner Unit API

Modified:
    commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/Monitor.java
    commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/Unit.java
    commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/stopwatches/CpuTimeStopWatch.java
    commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/stopwatches/SimpleStopWatch.java
    commons/sandbox/monitoring/branches/modules/core/src/test/java/org/apache/commons/monitoring/UnitTest.java
    commons/sandbox/monitoring/branches/modules/instrumentation/src/main/java/org/apache/commons/monitoring/instrumentation/aop/AbstractPerformanceInterceptor.java
    commons/sandbox/monitoring/branches/modules/reporting/src/main/java/org/apache/commons/monitoring/reporting/AbstractPeriodicLogger.java

Modified: commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/Monitor.java
URL: http://svn.apache.org/viewvc/commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/Monitor.java?rev=745420&r1=745419&r2=745420&view=diff
==============================================================================
--- commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/Monitor.java
(original)
+++ commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/Monitor.java
Wed Feb 18 08:14:29 2009
@@ -17,6 +17,8 @@
 
 package org.apache.commons.monitoring;
 
+import static org.apache.commons.monitoring.Unit.Time.MILLISECOND;
+
 import java.util.Collection;
 import java.util.EventListener;
 
@@ -50,10 +52,10 @@
     // --- Predefined roles -------------------------------------------------------------------
 
     /** Predefined role key for code performances */
-    Role PERFORMANCES = new Role( "performances", Unit.MILLIS, Metric.Type.COUNTER );
+    Role PERFORMANCES = new Role( "performances", MILLISECOND, Metric.Type.COUNTER );
 
     /** Predefined role key for cpu time */
-    Role CPU = new Role( "cpu", Unit.MILLIS, Metric.Type.COUNTER );
+    Role CPU = new Role( "cpu", MILLISECOND, Metric.Type.COUNTER );
 
     /** Predefined role for multi-thread concurrency */
     Role CONCURRENCY = new Role( "concurrency", Unit.UNARY, Metric.Type.GAUGE );

Modified: commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/Unit.java
URL: http://svn.apache.org/viewvc/commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/Unit.java?rev=745420&r1=745419&r2=745420&view=diff
==============================================================================
--- commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/Unit.java
(original)
+++ commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/Unit.java
Wed Feb 18 08:14:29 2009
@@ -43,21 +43,54 @@
     private static final Map<String, Unit> UNITS = new ConcurrentHashMap<String,Unit>();
 
     /** Time based units */
-    public static final Unit NANOS = new Unit( "ns" );
-    public static final Unit MICROS = new Unit( "µs", NANOS, 1000 );
-    public static final Unit MILLIS = new Unit( "ms", MICROS, 1000 );
-    public static final Unit SECOND = new Unit( "s", MILLIS, 1000 );
-    public static final Unit MINUTE = new Unit( "min", SECOND, 60 );
-    public static final Unit HOUR = new Unit( "h", MINUTE, 60 );
-    public static final Unit DAY = new Unit( "day", HOUR, 24 );
+    public static class Time
+        extends Unit
+    {
+        public static final Unit NANOSECOND = new Unit( "ns" );
+        public static final Unit MICROSECOND = new Unit( "µs", NANOSECOND, 1000 );
+        public static final Unit MILLISECOND = new Unit( "ms", MICROSECOND, 1000 );
+        public static final Unit SECOND = new Unit( "s", MILLISECOND, 1000 );
+        public static final Unit MINUTE = new Unit( "min", SECOND, 60 );
+        public static final Unit HOUR = new Unit( "h", MINUTE, 60 );
+        public static final Unit DAY = new Unit( "day", HOUR, 24 );
+
+        public Time( String name )
+        {
+            super( name );
+        }
+
+        public Time( String name, Unit derived, long scale )
+        {
+            super( name, derived, scale );
+        }
+
+    }
 
     /** Binary data units */
-    public static final Unit BYTE = new Unit( "b" );
-    public static final Unit KBYTE = new Unit( "Kb", BYTE, 1024 );
-    public static final Unit MBYTE = new Unit( "Mb", KBYTE, 1024 );
-    public static final Unit GBYTE = new Unit( "Gb", MBYTE, 1024 );
+    public static class Binary
+        extends Unit
+    {
+
+        public static final Unit BYTE = new Unit( "b" );
 
+        public static final Unit KBYTE = new Unit( "Kb", BYTE, 1024 );
 
+        public static final Unit MBYTE = new Unit( "Mb", KBYTE, 1024 );
+
+        public static final Unit GBYTE = new Unit( "Gb", MBYTE, 1024 );
+
+        public Binary( String name )
+        {
+            super( name );
+        }
+
+        public Binary( String name, Unit derived, long scale )
+        {
+            super( name, derived, scale );
+        }
+
+
+    }
     /** unit for basic item counters & gauges */
     // "BILLION" does not have same signification depending on country (10^12 or 10^9).
     // We use International system of unit names to avoid confusion

Modified: commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/stopwatches/CpuTimeStopWatch.java
URL: http://svn.apache.org/viewvc/commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/stopwatches/CpuTimeStopWatch.java?rev=745420&r1=745419&r2=745420&view=diff
==============================================================================
--- commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/stopwatches/CpuTimeStopWatch.java
(original)
+++ commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/stopwatches/CpuTimeStopWatch.java
Wed Feb 18 08:14:29 2009
@@ -64,7 +64,7 @@
         if ( mx.isCurrentThreadCpuTimeSupported() )
         {
             long cpu = mx.getCurrentThreadCpuTime() - cpuStartedAt;
-            monitor.getCounter( Monitor.CPU ).add( cpu, Unit.NANOS );
+            monitor.getCounter( Monitor.CPU ).add( cpu, Unit.Time.NANOSECOND );
         }
     }
 }

Modified: commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/stopwatches/SimpleStopWatch.java
URL: http://svn.apache.org/viewvc/commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/stopwatches/SimpleStopWatch.java?rev=745420&r1=745419&r2=745420&view=diff
==============================================================================
--- commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/stopwatches/SimpleStopWatch.java
(original)
+++ commons/sandbox/monitoring/branches/modules/core/src/main/java/org/apache/commons/monitoring/stopwatches/SimpleStopWatch.java
Wed Feb 18 08:14:29 2009
@@ -1,12 +1,13 @@
 package org.apache.commons.monitoring.stopwatches;
 
+import static org.apache.commons.monitoring.Unit.Time.NANOSECOND;
+
 import org.apache.commons.monitoring.Monitor;
 import org.apache.commons.monitoring.StopWatch;
-import org.apache.commons.monitoring.Unit;
 
 /**
  * Simple implementation of StopWatch that estimate monitored element execution time.
- * 
+ *
  * @author <a href="mailto:nicolas@apache.org">Nicolas De Loof</a>
  */
 public class SimpleStopWatch
@@ -34,7 +35,7 @@
      * Constructor.
      * <p>
      * The monitor can be set to null to use the StopWatch without the monitoring infrastructure.
-     * 
+     *
      * @param monitor the monitor associated with the process to be monitored
      */
     public SimpleStopWatch( Monitor monitor )
@@ -57,7 +58,7 @@
      * Returns the current value of the most precise available system timer, in nanoseconds.
The real precision depends
      * on the JVM and the underlying system. On JRE before java5, <tt>backport-util-concurrent</tt>
provides some
      * limited support for equivalent timer.
-     * 
+     *
      * @see System#nanoTime()
      * @return time in nanosecond
      */
@@ -68,7 +69,7 @@
 
     /**
      * {@inheritDoc}
-     * 
+     *
      * @see org.apache.commons.monitoring.StopWatch#getElapsedTime()
      */
     public long getElapsedTime()
@@ -86,7 +87,7 @@
 
     /**
      * {@inheritDoc}
-     * 
+     *
      * @see org.apache.commons.monitoring.StopWatch#pause()
      */
     public StopWatch pause()
@@ -101,7 +102,7 @@
 
     /**
      * {@inheritDoc}
-     * 
+     *
      * @see org.apache.commons.monitoring.StopWatch#resume()
      */
     public StopWatch resume()
@@ -117,7 +118,7 @@
 
     /**
      * {@inheritDoc}
-     * 
+     *
      * @return TODO
      * @see org.apache.commons.monitoring.StopWatch#stop()
      */
@@ -139,12 +140,12 @@
 
     protected void doStop()
     {
-        monitor.getCounter( Monitor.PERFORMANCES ).add( getElapsedTime(), Unit.NANOS );
+        monitor.getCounter( Monitor.PERFORMANCES ).add( getElapsedTime(), NANOSECOND );
     }
 
     /**
      * {@inheritDoc}
-     * 
+     *
      * @see org.apache.commons.monitoring.StopWatch#stop(boolean)
      */
     public StopWatch stop( boolean canceled )
@@ -162,7 +163,7 @@
 
     /**
      * {@inheritDoc}
-     * 
+     *
      * @see org.apache.commons.monitoring.StopWatch#cancel()
      */
     public StopWatch cancel()
@@ -187,7 +188,7 @@
      * {@link #cancel()} method is invoked, even when an exception occurs. To avoid StopWatches
to keep running if the
      * application didn't follow this recommendation, the finalizer is used to cancel the
StopWatch and will log a
      * educational warning.
-     * 
+     *
      * @see java.lang.Object#finalize()
      */
     protected void finalize()
@@ -203,7 +204,7 @@
 
     /**
      * {@inheritDoc}
-     * 
+     *
      * @see org.apache.commons.monitoring.StopWatch#isStoped()
      */
     public boolean isStoped()
@@ -213,7 +214,7 @@
 
     /**
      * {@inheritDoc}
-     * 
+     *
      * @see org.apache.commons.monitoring.StopWatch#isPaused()
      */
     public boolean isPaused()
@@ -247,7 +248,7 @@
 
     /**
      * {@inheritDoc}
-     * 
+     *
      * @see org.apache.commons.monitoring.StopWatch#getMonitor()
      */
     public Monitor getMonitor()

Modified: commons/sandbox/monitoring/branches/modules/core/src/test/java/org/apache/commons/monitoring/UnitTest.java
URL: http://svn.apache.org/viewvc/commons/sandbox/monitoring/branches/modules/core/src/test/java/org/apache/commons/monitoring/UnitTest.java?rev=745420&r1=745419&r2=745420&view=diff
==============================================================================
--- commons/sandbox/monitoring/branches/modules/core/src/test/java/org/apache/commons/monitoring/UnitTest.java
(original)
+++ commons/sandbox/monitoring/branches/modules/core/src/test/java/org/apache/commons/monitoring/UnitTest.java
Wed Feb 18 08:14:29 2009
@@ -18,6 +18,11 @@
 package org.apache.commons.monitoring;
 
 import static junit.framework.Assert.assertEquals;
+import static org.apache.commons.monitoring.Unit.Time.HOUR;
+import static org.apache.commons.monitoring.Unit.Time.MICROSECOND;
+import static org.apache.commons.monitoring.Unit.Time.MILLISECOND;
+import static org.apache.commons.monitoring.Unit.Time.NANOSECOND;
+import static org.apache.commons.monitoring.Unit.Time.SECOND;
 
 import org.junit.Test;
 
@@ -30,19 +35,19 @@
     @Test
     public void derived()
     {
-        assertEquals( Unit.NANOS, Unit.HOUR.getPrimary() );
-        assertEquals( Unit.NANOS, Unit.NANOS.getDerived( "ns" ) );
-        assertEquals( Unit.MICROS, Unit.NANOS.getDerived( "µs" ) );
-        assertEquals( Unit.MILLIS, Unit.NANOS.getDerived( "ms" ) );
-        assertEquals( Unit.SECOND, Unit.NANOS.getDerived( "s" ) );
+        assertEquals( NANOSECOND, HOUR.getPrimary() );
+        assertEquals( NANOSECOND, NANOSECOND.getDerived( "ns" ) );
+        assertEquals( MICROSECOND, NANOSECOND.getDerived( "µs" ) );
+        assertEquals( MILLISECOND, NANOSECOND.getDerived( "ms" ) );
+        assertEquals( SECOND, NANOSECOND.getDerived( "s" ) );
     }
 
     @Test
     public void scales()
     {
-        assertEquals( 1L, Unit.NANOS.getScale() );
-        assertEquals( 1000L, Unit.MICROS.getScale() );
-        assertEquals( 1000000L, Unit.MILLIS.getScale() );
-        assertEquals( 1000000000L, Unit.SECOND.getScale() );
+        assertEquals( 1L, NANOSECOND.getScale() );
+        assertEquals( 1000L, MICROSECOND.getScale() );
+        assertEquals( 1000000L, MILLISECOND.getScale() );
+        assertEquals( 1000000000L, SECOND.getScale() );
     }
 }

Modified: commons/sandbox/monitoring/branches/modules/instrumentation/src/main/java/org/apache/commons/monitoring/instrumentation/aop/AbstractPerformanceInterceptor.java
URL: http://svn.apache.org/viewvc/commons/sandbox/monitoring/branches/modules/instrumentation/src/main/java/org/apache/commons/monitoring/instrumentation/aop/AbstractPerformanceInterceptor.java?rev=745420&r1=745419&r2=745420&view=diff
==============================================================================
--- commons/sandbox/monitoring/branches/modules/instrumentation/src/main/java/org/apache/commons/monitoring/instrumentation/aop/AbstractPerformanceInterceptor.java
(original)
+++ commons/sandbox/monitoring/branches/modules/instrumentation/src/main/java/org/apache/commons/monitoring/instrumentation/aop/AbstractPerformanceInterceptor.java
Wed Feb 18 08:14:29 2009
@@ -17,12 +17,13 @@
 
 package org.apache.commons.monitoring.instrumentation.aop;
 
+import static org.apache.commons.monitoring.Unit.Time.NANOSECOND;
+
 import java.lang.reflect.Method;
 
 import org.apache.commons.monitoring.Monitor;
 import org.apache.commons.monitoring.Repository;
 import org.apache.commons.monitoring.StopWatch;
-import org.apache.commons.monitoring.Unit;
 
 /**
  * A method interceptor that compute method invocation performances.
@@ -111,7 +112,7 @@
     {
         if ( error != null )
         {
-            monitor.getCounter( Monitor.FAILURES ).add( duration, Unit.NANOS );
+            monitor.getCounter( Monitor.FAILURES ).add( duration, NANOSECOND );
         }
     }
 

Modified: commons/sandbox/monitoring/branches/modules/reporting/src/main/java/org/apache/commons/monitoring/reporting/AbstractPeriodicLogger.java
URL: http://svn.apache.org/viewvc/commons/sandbox/monitoring/branches/modules/reporting/src/main/java/org/apache/commons/monitoring/reporting/AbstractPeriodicLogger.java?rev=745420&r1=745419&r2=745420&view=diff
==============================================================================
--- commons/sandbox/monitoring/branches/modules/reporting/src/main/java/org/apache/commons/monitoring/reporting/AbstractPeriodicLogger.java
(original)
+++ commons/sandbox/monitoring/branches/modules/reporting/src/main/java/org/apache/commons/monitoring/reporting/AbstractPeriodicLogger.java
Wed Feb 18 08:14:29 2009
@@ -17,16 +17,19 @@
 
 package org.apache.commons.monitoring.reporting;
 
+import static org.apache.commons.monitoring.Unit.Time.MILLISECOND;
+
 import java.util.concurrent.Executors;
 import java.util.concurrent.ScheduledExecutorService;
 import java.util.concurrent.TimeUnit;
 
 import org.apache.commons.monitoring.Repository;
+import org.apache.commons.monitoring.Unit;
 import org.apache.commons.monitoring.repositories.ObserverRepository;
 
 /**
  * Base class to periodically log a fixed set of monitored data.
- * 
+ *
  * @author <a href="mailto:nicolas@apache.org">Nicolas De Loof</a>
  */
 public abstract class AbstractPeriodicLogger
@@ -94,7 +97,7 @@
     /**
      * An error occured during logging. To avoid the scheduler to stop we catch any throwable.
By default we just log to
      * System.err, this method is expected to be overriden.
-     * 
+     *
      * @param t error
      */
     protected void handleLogError( Throwable t )
@@ -157,6 +160,15 @@
     }
 
     /**
+     * @param period The period to wait between logging processes
+     * @param unit time unit
+     */
+    public void setPeriod( long period, Unit.Time unit )
+    {
+        this.period = (long) MILLISECOND.convert( period, unit );
+    }
+
+    /**
      * @param delay in MILLISECONDS
      * @see #setDelay(long, TimeUnit)
      */
@@ -174,6 +186,15 @@
         this.delay = TimeUnit.MILLISECONDS.convert( delay, unit );
     }
 
+    /**
+     * @param delay The delay to wait before first logging process
+     * @param unit time unit
+     */
+    public void setDelay( long period, Unit.Time unit )
+    {
+        this.delay = (long) MILLISECOND.convert( period, unit );
+    }
+
     public long getPeriod()
     {
         return period;



Mime
View raw message