camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject svn commit: r1173149 - in /camel/trunk/camel-core/src: main/java/org/apache/camel/management/ main/java/org/apache/camel/management/mbean/ test/java/org/apache/camel/impl/ test/java/org/apache/camel/management/
Date Tue, 20 Sep 2011 13:50:29 GMT
Author: davsclaus
Date: Tue Sep 20 13:50:29 2011
New Revision: 1173149

URL: http://svn.apache.org/viewvc?rev=1173149&view=rev
Log:
CAMEL-4366: Added load statitics to JMX for CamelContext and Route mbeans. Thanks to Christian
Ohr for the patch.

Added:
    camel/trunk/camel-core/src/main/java/org/apache/camel/management/mbean/LoadTriplet.java
    camel/trunk/camel-core/src/test/java/org/apache/camel/management/LoadTimerTest.java
    camel/trunk/camel-core/src/test/java/org/apache/camel/management/LoadTripletTest.java
Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/management/DefaultManagementLifecycleStrategy.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/management/mbean/ManagedCamelContext.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/management/mbean/ManagedRoute.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/management/mbean/Statistic.java
    camel/trunk/camel-core/src/test/java/org/apache/camel/impl/MultipleLifecycleStrategyTest.java

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/management/DefaultManagementLifecycleStrategy.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/management/DefaultManagementLifecycleStrategy.java?rev=1173149&r1=1173148&r2=1173149&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/management/DefaultManagementLifecycleStrategy.java
(original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/management/DefaultManagementLifecycleStrategy.java
Tue Sep 20 13:50:29 2011
@@ -23,6 +23,7 @@ import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
+import java.util.concurrent.ScheduledExecutorService;
 import java.util.concurrent.ThreadPoolExecutor;
 import javax.management.JMException;
 import javax.management.MalformedObjectNameException;
@@ -40,6 +41,7 @@ import org.apache.camel.Processor;
 import org.apache.camel.Producer;
 import org.apache.camel.Route;
 import org.apache.camel.Service;
+import org.apache.camel.TimerListener;
 import org.apache.camel.VetoCamelContextStartException;
 import org.apache.camel.api.management.PerformanceCounter;
 import org.apache.camel.impl.ConsumerCache;
@@ -73,8 +75,11 @@ import org.apache.camel.spi.ManagementOb
 import org.apache.camel.spi.ManagementStrategy;
 import org.apache.camel.spi.RouteContext;
 import org.apache.camel.spi.UnitOfWork;
+import org.apache.camel.support.ServiceSupport;
+import org.apache.camel.support.TimerListenerManager;
 import org.apache.camel.util.KeyValueHolder;
 import org.apache.camel.util.ObjectHelper;
+import org.apache.camel.util.ServiceHelper;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -86,12 +91,13 @@ import org.slf4j.LoggerFactory;
  * @version 
  */
 @SuppressWarnings("deprecation")
-public class DefaultManagementLifecycleStrategy implements LifecycleStrategy, Service, CamelContextAware
{
+public class DefaultManagementLifecycleStrategy extends ServiceSupport implements LifecycleStrategy,
CamelContextAware {
 
     private static final Logger LOG = LoggerFactory.getLogger(DefaultManagementLifecycleStrategy.class);
     private final Map<Processor, KeyValueHolder<ProcessorDefinition, InstrumentationProcessor>>
wrappedProcessors =
             new HashMap<Processor, KeyValueHolder<ProcessorDefinition, InstrumentationProcessor>>();
     private final List<PreRegisterService> preServices = new ArrayList<PreRegisterService>();
+    private final TimerListenerManager timerListenerManager = new TimerListenerManager();
     private CamelContext camelContext;
     private volatile boolean initialized;
     private final Set<String> knowRouteIds = new HashSet<String>();
@@ -165,7 +171,7 @@ public class DefaultManagementLifecycleS
         context.setManagementName(managementName);
 
         try {
-            getManagementStrategy().manageObject(mc);
+            manageObject(mc);
         } catch (Exception e) {
             // must rethrow to allow CamelContext fallback to non JMX agent to allow
             // Camel to continue to run
@@ -190,7 +196,7 @@ public class DefaultManagementLifecycleS
                 // prefer to use the default naming strategy to compute the next free name
                 name = ((DefaultCamelContextNameStrategy) strategy).getNextName();
             } else {
-                // if explict name then use a counter prefix
+                // if explicit name then use a counter prefix
                 name = managementName + "-" + counter++;
             }
             ObjectName on = getManagementStrategy().getManagementNamingStrategy().getObjectNameForCamelContext(name);
@@ -240,7 +246,7 @@ public class DefaultManagementLifecycleS
             Object mc = getManagementObjectStrategy().getManagedObjectForCamelContext(context);
             // the context could have been removed already
             if (getManagementStrategy().isManaged(mc, null)) {
-                getManagementStrategy().unmanageObject(mc);
+                unmanageObject(mc);
             }
         } catch (Exception e) {
             LOG.warn("Could not unregister CamelContext MBean", e);
@@ -258,7 +264,7 @@ public class DefaultManagementLifecycleS
         }
         try {
             Object mc = getManagementObjectStrategy().getManagedObjectForComponent(camelContext,
component, name);
-            getManagementStrategy().manageObject(mc);
+            manageObject(mc);
         } catch (Exception e) {
             LOG.warn("Could not register Component MBean", e);
         }
@@ -271,7 +277,7 @@ public class DefaultManagementLifecycleS
         }
         try {
             Object mc = getManagementObjectStrategy().getManagedObjectForComponent(camelContext,
component, name);
-            getManagementStrategy().unmanageObject(mc);
+            unmanageObject(mc);
         } catch (Exception e) {
             LOG.warn("Could not unregister Component MBean", e);
         }
@@ -304,7 +310,7 @@ public class DefaultManagementLifecycleS
                 // endpoint should not be managed
                 return;
             }
-            getManagementStrategy().manageObject(me);
+            manageObject(me);
         } catch (Exception e) {
             LOG.warn("Could not register Endpoint MBean for uri: " + endpoint.getEndpointUri(),
e);
         }
@@ -318,7 +324,7 @@ public class DefaultManagementLifecycleS
 
         try {
             Object me = getManagementObjectStrategy().getManagedObjectForEndpoint(camelContext,
endpoint);
-            getManagementStrategy().unmanageObject(me);
+            unmanageObject(me);
         } catch (Exception e) {
             LOG.warn("Could not unregister Endpoint MBean for uri: " + endpoint.getEndpointUri(),
e);
         }
@@ -354,7 +360,7 @@ public class DefaultManagementLifecycleS
         }
 
         try {
-            getManagementStrategy().manageObject(managedObject);
+            manageObject(managedObject);
         } catch (Exception e) {
             LOG.warn("Could not register service: " + service + " as Service MBean.", e);
         }
@@ -369,7 +375,7 @@ public class DefaultManagementLifecycleS
         Object managedObject = getManagedObjectForService(context, service, route);
         if (managedObject != null) {
             try {
-                getManagementStrategy().unmanageObject(managedObject);
+                unmanageObject(managedObject);
             } catch (Exception e) {
                 LOG.warn("Could not unregister service: " + service + " as Service MBean.",
e);
             }
@@ -488,7 +494,7 @@ public class DefaultManagementLifecycleS
             }
 
             try {
-                getManagementStrategy().manageObject(mr);
+                manageObject(mr);
             } catch (JMException e) {
                 LOG.warn("Could not register Route MBean", e);
             } catch (Exception e) {
@@ -513,7 +519,7 @@ public class DefaultManagementLifecycleS
             }
 
             try {
-                getManagementStrategy().unmanageObject(mr);
+                unmanageObject(mr);
             } catch (Exception e) {
                 LOG.warn("Could not unregister Route MBean", e);
             }
@@ -535,7 +541,7 @@ public class DefaultManagementLifecycleS
         }
 
         try {
-            getManagementStrategy().manageObject(me);
+            manageObject(me);
         } catch (Exception e) {
             LOG.warn("Could not register error handler builder: " + errorHandlerBuilder +
" as ErrorHandler MBean.", e);
         }
@@ -558,7 +564,7 @@ public class DefaultManagementLifecycleS
         }
 
         try {
-            getManagementStrategy().manageObject(mtp);
+            manageObject(mtp);
         } catch (Exception e) {
             LOG.warn("Could not register thread pool: " + threadPool + " as ThreadPool MBean.",
e);
         }
@@ -662,14 +668,32 @@ public class DefaultManagementLifecycleS
         return camelContext.getManagementStrategy().getManagementObjectStrategy();
     }
 
-    public void start() throws Exception {
-        ObjectHelper.notNull(camelContext, "CamelContext");
+    /**
+     * Strategy for managing the object
+     *
+     * @param me the managed object
+     * @throws Exception is thrown if error registering the object for management
+     */
+    protected void manageObject(Object me) throws Exception {
+        getManagementStrategy().manageObject(me);
+        if (timerListenerManager != null && me instanceof TimerListener) {
+            TimerListener timer = (TimerListener) me;
+            timerListenerManager.addTimerListener(timer);
+        }
     }
 
-    public void stop() throws Exception {
-        initialized = false;
-        knowRouteIds.clear();
-        preServices.clear();
+    /**
+     * Un-manages the object.
+     *
+     * @param me the managed object
+     * @throws Exception is thrown if error unregistering the managed object
+     */
+    protected void unmanageObject(Object me) throws Exception {
+        if (timerListenerManager != null && me instanceof TimerListener) {
+            TimerListener timer = (TimerListener) me;
+            timerListenerManager.removeTimerListener(timer);
+        }
+        getManagementStrategy().unmanageObject(me);
     }
 
     /**
@@ -680,7 +704,6 @@ public class DefaultManagementLifecycleS
      * dynamic endpoints is not registered. This avoids to register excessive mbeans, which
      * most often is not desired.
      *
-     *
      * @param service the object to register
      * @param route   an optional route the mbean is associated with, can be <tt>null</tt>
      * @return <tt>true</tt> to register, <tt>false</tt> to skip
registering
@@ -719,6 +742,27 @@ public class DefaultManagementLifecycleS
         return false;
     }
 
+    @Override
+    protected void doStart() throws Exception {
+        ObjectHelper.notNull(camelContext, "CamelContext");
+
+        boolean enabled = camelContext.getManagementStrategy().getStatisticsLevel() != ManagementStatisticsLevel.Off;
+        if (enabled) {
+            LOG.info("StatiticsLevel at {} so enabling load performance statistics", camelContext.getManagementStrategy().getStatisticsLevel());
+            ScheduledExecutorService executorService = camelContext.getExecutorServiceManager().newDefaultScheduledThreadPool(this,
"ManagementLoadTask");
+            timerListenerManager.setExecutorService(executorService);
+            ServiceHelper.startService(timerListenerManager);
+        }
+    }
+
+    @Override
+    protected void doStop() throws Exception {
+        initialized = false;
+        knowRouteIds.clear();
+        preServices.clear();
+        ServiceHelper.stopService(timerListenerManager);
+    }
+
     /**
      * Class which holds any pre registration details.
      *

Added: camel/trunk/camel-core/src/main/java/org/apache/camel/management/mbean/LoadTriplet.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/management/mbean/LoadTriplet.java?rev=1173149&view=auto
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/management/mbean/LoadTriplet.java
(added)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/management/mbean/LoadTriplet.java
Tue Sep 20 13:50:29 2011
@@ -0,0 +1,65 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.management.mbean;
+
+/**
+ * Holds the loads averaged over 1min, 5min, and 15min.
+ */
+public final class LoadTriplet {
+
+    // Exponents for EWMA: exp(-INTERVAL / WINDOW) (in seconds)
+    private static final double EXP_1 = Math.exp(-1 / (60.0 * 1.0));
+    private static final double EXP_5 = Math.exp(-1 / (60.0 * 5.0));
+    private static final double EXP_15 = Math.exp(-1 / (60.0 * 15.0));
+
+    private double load01 = Double.NaN;
+    private double load05 = Double.NaN;
+    private double load15 = Double.NaN;
+
+    /**
+     * Update the load statistics
+     *
+     * @param currentReading the current reading
+     */
+    public void update(int currentReading) {
+        load01 = updateLoad(currentReading, EXP_1, load01);
+        load05 = updateLoad(currentReading, EXP_5, load05);
+        load15 = updateLoad(currentReading, EXP_15, load15);
+    }
+
+    private double updateLoad(int reading, double exp, double recentLoad) {
+        return Double.isNaN(recentLoad) ? reading : reading + exp * (recentLoad - reading);
+    }
+
+    public double getLoad1() {
+        return load01;
+    }
+
+    public double getLoad5() {
+        return load05;
+    }
+
+    public double getLoad15() {
+        return load15;
+    }
+
+    @Override
+    public String toString() {
+        return String.format("%.2f, %.2f, %.2f", getLoad1(), getLoad5(), getLoad15());
+    }
+
+}

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/management/mbean/ManagedCamelContext.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/management/mbean/ManagedCamelContext.java?rev=1173149&r1=1173148&r2=1173149&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/management/mbean/ManagedCamelContext.java
(original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/management/mbean/ManagedCamelContext.java
Tue Sep 20 13:50:29 2011
@@ -28,6 +28,7 @@ import org.apache.camel.CamelContext;
 import org.apache.camel.Endpoint;
 import org.apache.camel.ProducerTemplate;
 import org.apache.camel.ServiceStatus;
+import org.apache.camel.TimerListener;
 import org.apache.camel.api.management.ManagedAttribute;
 import org.apache.camel.api.management.ManagedOperation;
 import org.apache.camel.api.management.ManagedResource;
@@ -41,8 +42,9 @@ import org.apache.camel.spi.ManagementSt
  * @version 
  */
 @ManagedResource(description = "Managed CamelContext")
-public class ManagedCamelContext {
-    private final ModelCamelContext context;
+public class ManagedCamelContext implements TimerListener {
+    private final ModelCamelContext context;   
+    private final LoadTriplet load = new LoadTriplet();
 
     public ManagedCamelContext(ModelCamelContext context) {
         this.context = context;
@@ -134,6 +136,26 @@ public class ManagedCamelContext {
     public boolean isShutdownNowOnTimeout() {
         return context.getShutdownStrategy().isShutdownNowOnTimeout();
     }
+    
+    @ManagedAttribute(description = "Average load over the last minute")
+    public String getLoad01() {
+        return String.format("%.2f", load.getLoad1());
+    }
+
+    @ManagedAttribute(description = "Average load over the last five minutes")
+    public String getLoad05() {
+        return String.format("%.2f", load.getLoad5());
+    }
+
+    @ManagedAttribute(description = "Average load over the last fifteen minutes")
+    public String getLoad15() {
+        return String.format("%.2f", load.getLoad15());
+    }
+
+    @Override
+    public void onTimer() {
+        load.update(getInflightExchanges());
+    }
 
     @ManagedOperation(description = "Start Camel")
     public void start() throws Exception {

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/management/mbean/ManagedRoute.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/management/mbean/ManagedRoute.java?rev=1173149&r1=1173148&r2=1173149&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/management/mbean/ManagedRoute.java
(original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/management/mbean/ManagedRoute.java
Tue Sep 20 13:50:29 2011
@@ -24,6 +24,7 @@ import org.apache.camel.Endpoint;
 import org.apache.camel.ManagementStatisticsLevel;
 import org.apache.camel.Route;
 import org.apache.camel.ServiceStatus;
+import org.apache.camel.TimerListener;
 import org.apache.camel.api.management.ManagedAttribute;
 import org.apache.camel.api.management.ManagedOperation;
 import org.apache.camel.api.management.ManagedResource;
@@ -34,11 +35,12 @@ import org.apache.camel.spi.RoutePolicy;
 import org.apache.camel.util.ObjectHelper;
 
 @ManagedResource(description = "Managed Route")
-public class ManagedRoute extends ManagedPerformanceCounter {
+public class ManagedRoute extends ManagedPerformanceCounter implements TimerListener {
     public static final String VALUE_UNKNOWN = "Unknown";
     protected final Route route;
     protected final String description;
     protected final ModelCamelContext context;
+    private final LoadTriplet load = new LoadTriplet();
 
     public ManagedRoute(ModelCamelContext context, Route route) {
         this.route = route;
@@ -132,6 +134,26 @@ public class ManagedRoute extends Manage
         return sb.toString();
     }
 
+    @ManagedAttribute(description = "Average load over the last minute")
+    public String getLoad01() {
+        return String.format("%.2f", load.getLoad1());
+    }
+
+    @ManagedAttribute(description = "Average load over the last five minutes")
+    public String getLoad05() {
+        return String.format("%.2f", load.getLoad5());
+    }
+
+    @ManagedAttribute(description = "Average load over the last fifteen minutes")
+    public String getLoad15() {
+        return String.format("%.2f", load.getLoad15());
+    }
+
+    @Override
+    public void onTimer() {
+        load.update(getInflightExchanges());
+    }
+    
     @ManagedOperation(description = "Start route")
     public void start() throws Exception {
         if (!context.getStatus().isStarted()) {

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/management/mbean/Statistic.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/management/mbean/Statistic.java?rev=1173149&r1=1173148&r2=1173149&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/management/mbean/Statistic.java
(original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/management/mbean/Statistic.java
Tue Sep 20 13:50:29 2011
@@ -20,6 +20,7 @@ package org.apache.camel.management.mbea
  * Default implementation of {@link Statistic}
  */
 public class Statistic {
+
     /**
      * Statistics mode
      * <ul>
@@ -46,9 +47,9 @@ public class Statistic {
 
     /**
      * Instantiates a new statistic.
-     * @param owner 
-     * @param name 
      *
+     * @param name  name of statistic
+     * @param owner owner
      * @param updateMode The statistic update mode.
      */
     public Statistic(String name, Object owner, UpdateMode updateMode) {

Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/impl/MultipleLifecycleStrategyTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/impl/MultipleLifecycleStrategyTest.java?rev=1173149&r1=1173148&r2=1173149&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/impl/MultipleLifecycleStrategyTest.java
(original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/impl/MultipleLifecycleStrategyTest.java
Tue Sep 20 13:50:29 2011
@@ -50,7 +50,7 @@ public class MultipleLifecycleStrategyTe
         context.removeComponent("log");
         context.stop();
 
-        List<String> expectedEvents = Arrays.asList("onContextStart", "onServiceAdd",
"onServiceAdd", "onServiceAdd", "onServiceAdd", "onServiceAdd", 
+        List<String> expectedEvents = Arrays.asList("onThreadPoolAdd", "onContextStart",
"onServiceAdd", "onServiceAdd", "onServiceAdd", "onServiceAdd", "onServiceAdd",
              "onServiceAdd", "onServiceAdd", "onComponentAdd", "onEndpointAdd", "onComponentRemove",
"onThreadPoolAdd", "onContextStop"); 
         
         assertEquals(expectedEvents, dummy1.getEvents());

Added: camel/trunk/camel-core/src/test/java/org/apache/camel/management/LoadTimerTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/management/LoadTimerTest.java?rev=1173149&view=auto
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/management/LoadTimerTest.java (added)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/management/LoadTimerTest.java Tue
Sep 20 13:50:29 2011
@@ -0,0 +1,66 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.management;
+
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+
+import junit.framework.TestCase;
+import org.apache.camel.TimerListener;
+import org.apache.camel.management.mbean.LoadTriplet;
+import org.apache.camel.support.TimerListenerManager;
+
+public class LoadTimerTest extends TestCase {
+
+    private static final int SAMPLES = 3;
+
+    public void testTimer() throws Exception {
+        ScheduledExecutorService executorService = Executors.newScheduledThreadPool(1);
+
+        TimerListenerManager myTimer = new TimerListenerManager();
+        myTimer.setExecutorService(executorService);
+        myTimer.start();
+
+        TestLoadAware test = new TestLoadAware();
+        myTimer.addTimerListener(test);
+        try {
+            Thread.sleep(1000 * (SAMPLES + 1));
+            assertTrue(test.counter >= SAMPLES);
+            assertFalse(Double.isNaN(test.load.getLoad1()));
+            assertTrue(test.load.getLoad1() > 0.0d);
+            assertTrue(test.load.getLoad1() < SAMPLES);
+        } finally {
+            myTimer.removeTimerListener(test);
+        }
+
+        myTimer.stop();
+        executorService.shutdown();
+    }
+
+    private class TestLoadAware implements TimerListener {
+
+        int counter;
+        LoadTriplet load = new LoadTriplet();
+
+        @Override
+        public void onTimer() {
+            counter++;
+            load.update(counter);
+        }
+
+    }
+}

Added: camel/trunk/camel-core/src/test/java/org/apache/camel/management/LoadTripletTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/management/LoadTripletTest.java?rev=1173149&view=auto
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/management/LoadTripletTest.java
(added)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/management/LoadTripletTest.java
Tue Sep 20 13:50:29 2011
@@ -0,0 +1,63 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.management;
+
+import junit.framework.TestCase;
+import org.apache.camel.management.mbean.LoadTriplet;
+
+public class LoadTripletTest extends TestCase {
+
+    public void testConstantUpdate() {
+        LoadTriplet t = new LoadTriplet();
+        t.update(1);
+        assertEquals(1.0, t.getLoad1(), Math.ulp(1.0) * 5);
+        assertEquals(1.0, t.getLoad5(), Math.ulp(1.0) * 5);
+        assertEquals(1.0, t.getLoad15(), Math.ulp(1.0) * 5);
+        for (int i = 0; i < 100; i++) {
+            t.update(1);
+        }
+        assertEquals(1.0, t.getLoad1(), Math.ulp(1.0) * 5);
+        assertEquals(1.0, t.getLoad5(), Math.ulp(1.0) * 5);
+        assertEquals(1.0, t.getLoad15(), Math.ulp(1.0) * 5);
+    }
+
+    public void testChargeDischarge() {
+        LoadTriplet t = new LoadTriplet();
+        t.update(0);
+        double last = t.getLoad15();
+        double lastDiff = Double.MAX_VALUE;
+        double diff;
+        for (int i = 0; i < 1000; i++) {
+            t.update(5);
+            diff = t.getLoad15() - last;
+            assertTrue(diff > 0.0);
+            assertTrue(diff < lastDiff);
+            lastDiff = diff;
+            last = t.getLoad15();
+        }
+        lastDiff = -Double.MAX_VALUE;
+        for (int i = 0; i < 1000; i++) {
+            t.update(0);
+            diff = t.getLoad15() - last;
+            assertTrue(diff < 0.0);
+            assertTrue(String.format("%f is smaller than %f", diff, lastDiff), diff >
lastDiff);
+            lastDiff = diff;
+            last = t.getLoad15();
+        }
+    }
+
+}



Mime
View raw message