Author: pferraro
Date: 2008-10-21 11:20:30 -0400 (Tue, 21 Oct 2008)
New Revision: 1965
Added:
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/metric/LoadContext.java
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/metric/impl/MBeanLoadContext.java
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/metric/impl/MBeanLoadMetricSource.java
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/metric/impl/MBeanQueryLoadContext.java
trunk/mod_cluster/src/test/java/org/jboss/modcluster/load/metric/ConnectionPoolLoadMetricSourceTestCase.java
trunk/mod_cluster/src/test/java/org/jboss/modcluster/load/metric/MBeanAttributeLoadMetricTestCase.java
trunk/mod_cluster/src/test/java/org/jboss/modcluster/load/metric/MBeanAttributeRatioLoadMetricTestCase.java
trunk/mod_cluster/src/test/java/org/jboss/modcluster/load/metric/MBeanLoadContextTestCase.java
trunk/mod_cluster/src/test/java/org/jboss/modcluster/load/metric/MBeanLoadMetricSourceTestCase.java
trunk/mod_cluster/src/test/java/org/jboss/modcluster/load/metric/MBeanQueryLoadContextTestCase.java
trunk/mod_cluster/src/test/java/org/jboss/modcluster/load/metric/MBeanQueryLoadMetricSourceTestCase.java
trunk/mod_cluster/src/test/java/org/jboss/modcluster/load/metric/OperatingSystemLoadMetricSourceTestCase.java
trunk/mod_cluster/src/test/java/org/jboss/modcluster/load/metric/RequestProcessorLoadMetricSourceTestCase.java
trunk/mod_cluster/src/test/java/org/jboss/modcluster/load/metric/SessionLoadMetricSourceTestCase.java
trunk/mod_cluster/src/test/java/org/jboss/modcluster/load/metric/ThreadPoolLoadMetricSourceTestCase.java
Removed:
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/metric/LoadMetricSourceRegistration.java
Modified:
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/impl/DynamicLoadBalanceFactorProvider.java
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/metric/LoadMetric.java
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/metric/LoadMetricMBean.java
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/metric/LoadMetricSource.java
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/metric/impl/AbstractLoadMetric.java
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/metric/impl/AbstractLoadMetricSource.java
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/metric/impl/ActiveSessionsLoadMetric.java
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/metric/impl/AverageSystemLoadMetric.java
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/metric/impl/BusyConnectorsLoadMetric.java
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/metric/impl/ConnectionPoolLoadMetricSource.java
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/metric/impl/ConnectionPoolUsageLoadMetric.java
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/metric/impl/HeapMemoryUsageLoadMetric.java
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/metric/impl/MBeanAttributeLoadMetric.java
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/metric/impl/MBeanAttributeRatioLoadMetric.java
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/metric/impl/MBeanQueryLoadMetricSource.java
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/metric/impl/OperatingSystemLoadMetricSource.java
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/metric/impl/ReceiveTrafficLoadMetric.java
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/metric/impl/RequestCountLoadMetric.java
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/metric/impl/RequestProcessorLoadMetricSource.java
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/metric/impl/SendTrafficLoadMetric.java
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/metric/impl/SessionLoadMetricSource.java
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/metric/impl/SingleLoadMetricSource.java
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/metric/impl/SystemMemoryUsageLoadMetric.java
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/metric/impl/ThreadCountLoadMetric.java
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/metric/impl/ThreadPoolLoadMetricSource.java
trunk/mod_cluster/src/test/java/org/jboss/modcluster/load/DynamicLoadBalanceFactorProviderTestCase.java
trunk/mod_cluster/src/test/java/org/jboss/modcluster/load/StaticLoadBalanceFactorProviderTestCase.java
trunk/mod_cluster/src/test/java/org/jboss/modcluster/load/metric/ActiveSessionsLoadMetricTestCase.java
trunk/mod_cluster/src/test/java/org/jboss/modcluster/load/metric/AverageSystemLoadMetricTestCase.java
trunk/mod_cluster/src/test/java/org/jboss/modcluster/load/metric/BusyConnectorsLoadMetricTestCase.java
trunk/mod_cluster/src/test/java/org/jboss/modcluster/load/metric/ConnectionPoolUsageLoadMetricTestCase.java
trunk/mod_cluster/src/test/java/org/jboss/modcluster/load/metric/HeapMemoryUsageLoadMetricTestCase.java
trunk/mod_cluster/src/test/java/org/jboss/modcluster/load/metric/ReceiveTrafficLoadMetricTestCase.java
trunk/mod_cluster/src/test/java/org/jboss/modcluster/load/metric/RequestCountLoadMetricTestCase.java
trunk/mod_cluster/src/test/java/org/jboss/modcluster/load/metric/SendTrafficLoadMetricTestCase.java
trunk/mod_cluster/src/test/java/org/jboss/modcluster/load/metric/SystemMemoryUsageLoadMetricTestCase.java
trunk/mod_cluster/src/test/java/org/jboss/modcluster/load/metric/ThreadCountLoadMetricTestCase.java
Log:
Refactored interaction between metric, source, and provider. The new responsibility of
the LoadMetricSource is to create a LoadContext which is passed to each
LoadMetric.getLoad(...) for a given source.
Modified:
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/impl/DynamicLoadBalanceFactorProvider.java
===================================================================
---
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/impl/DynamicLoadBalanceFactorProvider.java 2008-10-20
11:53:50 UTC (rev 1964)
+++
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/impl/DynamicLoadBalanceFactorProvider.java 2008-10-21
15:20:30 UTC (rev 1965)
@@ -29,35 +29,31 @@
import org.apache.catalina.util.StringManager;
import org.jboss.logging.Logger;
+import org.jboss.modcluster.Constants;
import org.jboss.modcluster.load.LoadBalanceFactorProvider;
+import org.jboss.modcluster.load.metric.LoadContext;
import org.jboss.modcluster.load.metric.LoadMetric;
import org.jboss.modcluster.load.metric.LoadMetricSource;
-import org.jboss.modcluster.load.metric.LoadMetricSourceRegistration;
-import org.jboss.modcluster.Constants;
/**
* {@link LoadBalanceFactorProvider} implementation that periodically aggregates load
from a set of {@link LoadMetricSource}s.
*
* @author Paul Ferraro
*/
-public class DynamicLoadBalanceFactorProvider implements LoadBalanceFactorProvider,
LoadMetricSourceRegistration, DynamicLoadBalanceFactorProviderMBean
+public class DynamicLoadBalanceFactorProvider implements LoadBalanceFactorProvider,
DynamicLoadBalanceFactorProviderMBean
{
private final Logger log = Logger.getLogger(this.getClass());
private final StringManager sm = StringManager.getManager(Constants.Package);
- private final Collection<LoadMetricSource> sources = new
ArrayList<LoadMetricSource>();
- private final Map<LoadMetric, List<Double>> loadHistory = new
HashMap<LoadMetric, List<Double>>();
+ private final Collection<LoadMetricSource<LoadContext>> sources;
+ private final Map<LoadMetric<LoadContext>, List<Double>> loadHistory
= new HashMap<LoadMetric<LoadContext>, List<Double>>();
private volatile int decayFactor = 2;
private volatile int history = 10;
- /**
- * @{inheritDoc}
- * @see
org.jboss.modcluster.load.metric.MetricRegistration#register(org.jboss.modcluster.load.metric.LoadMetricSource)
- */
- public void add(LoadMetricSource source)
+ public
DynamicLoadBalanceFactorProvider(Collection<LoadMetricSource<LoadContext>>
sources)
{
- this.sources.add(source);
+ this.sources = sources;
}
/**
@@ -69,14 +65,14 @@
int totalWeight = 0;
double totalWeightedLoad = 0;
- for (LoadMetricSource source: this.sources)
+ for (LoadMetricSource<LoadContext> source: this.sources)
{
- Collection<? extends LoadMetric> metrics = source.getMetrics();
+ Collection<LoadMetric<LoadContext>> metrics = source.getMetrics();
- List<LoadMetric> metricList = new
ArrayList<LoadMetric>(metrics.size());
+ List<LoadMetric<LoadContext>> metricList = new
ArrayList<LoadMetric<LoadContext>>(metrics.size());
// Prune 0 weights since they will not contribute anything
- for (LoadMetric metric: metrics)
+ for (LoadMetric<LoadContext> metric: metrics)
{
if (metric.getWeight() != 0)
{
@@ -86,16 +82,16 @@
if (!metricList.isEmpty())
{
- source.prepare();
+ LoadContext context = source.createContext();
try
{
- for (LoadMetric metric: metricList)
+ for (LoadMetric<LoadContext> metric: metricList)
{
try
{
// Normalize load with respect to capacity
- List<Double> queue = this.recordLoad(metric, metric.getLoad()
/ metric.getCapacity());
+ List<Double> queue = this.recordLoad(metric,
metric.getLoad(context) / metric.getCapacity());
int weight = metric.getWeight();
@@ -110,7 +106,7 @@
}
finally
{
- source.cleanup();
+ context.close();
}
}
}
@@ -122,7 +118,7 @@
return 100 - Math.max(0, Math.min(load, 100));
}
- private List<Double> recordLoad(LoadMetric metric, double load)
+ private List<Double> recordLoad(LoadMetric<LoadContext> metric, double
load)
{
List<Double> queue = this.loadHistory.get(metric);
Added: trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/metric/LoadContext.java
===================================================================
--- trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/metric/LoadContext.java
(rev 0)
+++
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/metric/LoadContext.java 2008-10-21
15:20:30 UTC (rev 1965)
@@ -0,0 +1,31 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+package org.jboss.modcluster.load.metric;
+
+/**
+ * @author Paul Ferraro
+ *
+ */
+public interface LoadContext
+{
+ public void close();
+}
Modified:
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/metric/LoadMetric.java
===================================================================
---
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/metric/LoadMetric.java 2008-10-20
11:53:50 UTC (rev 1964)
+++
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/metric/LoadMetric.java 2008-10-21
15:20:30 UTC (rev 1965)
@@ -25,7 +25,7 @@
* Represents a specific load metric.
* @author Paul Ferraro
*/
-public interface LoadMetric
+public interface LoadMetric<C extends LoadContext>
{
/**
* Returns the "weight" of this metric, i.e. significance of this load
metric compared to the other metrics.
@@ -46,5 +46,5 @@
* @return the current load.
* @throws Exception if there was an error fetching this metric.
*/
- double getLoad() throws Exception;
+ double getLoad(C context) throws Exception;
}
Modified:
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/metric/LoadMetricMBean.java
===================================================================
---
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/metric/LoadMetricMBean.java 2008-10-20
11:53:50 UTC (rev 1964)
+++
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/metric/LoadMetricMBean.java 2008-10-21
15:20:30 UTC (rev 1965)
@@ -26,7 +26,7 @@
* @author Paul Ferraro
*
*/
-public interface LoadMetricMBean extends LoadMetric
+public interface LoadMetricMBean<C extends LoadContext> extends
LoadMetric<C>
{
void setWeight(int weight);
Modified:
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/metric/LoadMetricSource.java
===================================================================
---
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/metric/LoadMetricSource.java 2008-10-20
11:53:50 UTC (rev 1964)
+++
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/metric/LoadMetricSource.java 2008-10-21
15:20:30 UTC (rev 1965)
@@ -27,21 +27,16 @@
* A source for obtaining load metrics.
* @author Paul Ferraro
*/
-public interface LoadMetricSource
+public interface LoadMetricSource<C extends LoadContext>
{
/**
* Returns the collection of metrics registered with this source.
* @return the metrics from this source
*/
- Collection<LoadMetric> getMetrics();
+ Collection<LoadMetric<C>> getMetrics();
/**
- * Prepare any resource required to collect load metrics
+ * Prepare any resources required to collect load metrics
*/
- void prepare();
-
- /**
- * Cleanup any resources used to collect load metrics
- */
- void cleanup();
+ C createContext();
}
Deleted:
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/metric/LoadMetricSourceRegistration.java
===================================================================
---
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/metric/LoadMetricSourceRegistration.java 2008-10-20
11:53:50 UTC (rev 1964)
+++
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/metric/LoadMetricSourceRegistration.java 2008-10-21
15:20:30 UTC (rev 1965)
@@ -1,32 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source.
- * Copyright 2008, Red Hat Middleware LLC, and individual contributors
- * as indicated by the @author tags. See the copyright.txt file in the
- * distribution for a full listing of individual contributors.
- *
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
- */
-package org.jboss.modcluster.load.metric;
-
-
-/**
- * @author Paul Ferraro
- *
- */
-public interface LoadMetricSourceRegistration
-{
- void add(LoadMetricSource source);
-}
Modified:
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/metric/impl/AbstractLoadMetric.java
===================================================================
---
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/metric/impl/AbstractLoadMetric.java 2008-10-20
11:53:50 UTC (rev 1964)
+++
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/metric/impl/AbstractLoadMetric.java 2008-10-21
15:20:30 UTC (rev 1965)
@@ -21,6 +21,7 @@
*/
package org.jboss.modcluster.load.metric.impl;
+import org.jboss.modcluster.load.metric.LoadContext;
import org.jboss.modcluster.load.metric.LoadMetric;
import org.jboss.modcluster.load.metric.LoadMetricMBean;
@@ -29,7 +30,7 @@
*
* @author Paul Ferraro
*/
-public abstract class AbstractLoadMetric implements LoadMetricMBean
+public abstract class AbstractLoadMetric<C extends LoadContext> implements
LoadMetricMBean<C>
{
private volatile int weight = 0;
private volatile double capacity = 1;
@@ -69,7 +70,7 @@
{
if (capacity <= 0)
{
- throw new IllegalArgumentException("Capacity be greater than zero.");
+ throw new IllegalArgumentException("Capacity must be greater than
zero.");
}
this.capacity = capacity;
Modified:
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/metric/impl/AbstractLoadMetricSource.java
===================================================================
---
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/metric/impl/AbstractLoadMetricSource.java 2008-10-20
11:53:50 UTC (rev 1964)
+++
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/metric/impl/AbstractLoadMetricSource.java 2008-10-21
15:20:30 UTC (rev 1965)
@@ -22,55 +22,31 @@
package org.jboss.modcluster.load.metric.impl;
import java.util.Collection;
-import java.util.LinkedList;
+import org.jboss.modcluster.load.metric.LoadContext;
import org.jboss.modcluster.load.metric.LoadMetric;
import org.jboss.modcluster.load.metric.LoadMetricSource;
-import org.jboss.modcluster.load.metric.LoadMetricSourceRegistration;
/**
* Abstract {@link LoadMetricSource} that allows a {@link LoadMetric} to register with
it.
*
* @author Paul Ferraro
*/
-public abstract class AbstractLoadMetricSource implements LoadMetricSource
+public abstract class AbstractLoadMetricSource<C extends LoadContext> implements
LoadMetricSource<C>
{
- private Collection<LoadMetric> metrics = new LinkedList<LoadMetric>();
+ private final Collection<LoadMetric<C>> metrics;
- protected AbstractLoadMetricSource(LoadMetricSourceRegistration registration)
+ protected AbstractLoadMetricSource(Collection<LoadMetric<C>> metrics)
{
- registration.add(this);
+ this.metrics = metrics;
}
- public void add(LoadMetric metric)
- {
- this.metrics.add(metric);
- }
-
/**
* @{inheritDoc}
* @see org.jboss.modcluster.load.metric.LoadMetricSource#getMetrics()
*/
- public Collection<LoadMetric> getMetrics()
+ public Collection<LoadMetric<C>> getMetrics()
{
return this.metrics;
}
-
- /**
- * @{inheritDoc}
- * @see org.jboss.modcluster.load.metric.LoadMetricSource#prepare()
- */
- public void prepare()
- {
- // Nothing to open
- }
-
- /**
- * @{inheritDoc}
- * @see org.jboss.modcluster.load.metric.LoadMetricSource#cleanup()
- */
- public void cleanup()
- {
- // Nothing to close
- }
}
Modified:
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/metric/impl/ActiveSessionsLoadMetric.java
===================================================================
---
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/metric/impl/ActiveSessionsLoadMetric.java 2008-10-20
11:53:50 UTC (rev 1964)
+++
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/metric/impl/ActiveSessionsLoadMetric.java 2008-10-21
15:20:30 UTC (rev 1965)
@@ -30,15 +30,15 @@
*/
public class ActiveSessionsLoadMetric extends MBeanAttributeLoadMetric
{
- private static final String DEFAULT_ATTRIBUTE = "activeSessions";
+ public static final String DEFAULT_ATTRIBUTE = "activeSessions";
- public ActiveSessionsLoadMetric(SessionLoadMetricSource source)
+ public ActiveSessionsLoadMetric()
{
- this(source, DEFAULT_ATTRIBUTE);
+ this(DEFAULT_ATTRIBUTE);
}
- public ActiveSessionsLoadMetric(SessionLoadMetricSource source, String attribute)
+ public ActiveSessionsLoadMetric(String attribute)
{
- super(source, attribute);
+ super(attribute);
}
}
Modified:
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/metric/impl/AverageSystemLoadMetric.java
===================================================================
---
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/metric/impl/AverageSystemLoadMetric.java 2008-10-20
11:53:50 UTC (rev 1964)
+++
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/metric/impl/AverageSystemLoadMetric.java 2008-10-21
15:20:30 UTC (rev 1965)
@@ -23,6 +23,7 @@
import java.lang.management.OperatingSystemMXBean;
+import javax.management.AttributeNotFoundException;
import javax.management.JMException;
import org.jboss.logging.Logger;
@@ -33,39 +34,30 @@
*
* @author Paul Ferraro
*/
-public class AverageSystemLoadMetric extends AbstractLoadMetric
+public class AverageSystemLoadMetric extends AbstractLoadMetric<MBeanLoadContext>
{
- private static final String SYSTEM_LOAD_AVERAGE = "SystemLoadAverage";
+ public static final String SYSTEM_LOAD_AVERAGE = "SystemLoadAverage";
- private final OperatingSystemLoadMetricSource source;
- private final Logger log = Logger.getLogger(this.getClass());
+ private Logger logger = Logger.getLogger(this.getClass());
/**
- * Create a new SystemLoadMetric.
- *
- * @param registration
+ * @{inheritDoc}
+ * @see org.jboss.modcluster.load.metric.LoadMetric#getLoad()
*/
- public AverageSystemLoadMetric(OperatingSystemLoadMetricSource source)
+ public double getLoad(MBeanLoadContext context) throws JMException
{
- this.source = source;
-
- if (this.source.exists(SYSTEM_LOAD_AVERAGE))
+ try
{
- this.source.add(this);
+ return context.getAttribute(SYSTEM_LOAD_AVERAGE, Double.class).doubleValue();
}
- else
+ catch (AttributeNotFoundException e)
{
- this.log.warn(this.getClass().getName() + " discarded. This metric
requires the "
- + SYSTEM_LOAD_AVERAGE + " OperatingSystemMXBean attribute found in
Java 1.6 or later.");
+ this.logger.warn(this.getClass().getSimpleName() + " requires Java 1.6 or
later.");
+
+ // Disable this metric
+ this.setWeight(0);
+
+ return 0;
}
}
-
- /**
- * @{inheritDoc}
- * @see org.jboss.modcluster.load.metric.LoadMetric#getLoad()
- */
- public double getLoad() throws JMException
- {
- return this.source.getAttribute(SYSTEM_LOAD_AVERAGE, Double.class).doubleValue();
- }
}
Modified:
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/metric/impl/BusyConnectorsLoadMetric.java
===================================================================
---
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/metric/impl/BusyConnectorsLoadMetric.java 2008-10-20
11:53:50 UTC (rev 1964)
+++
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/metric/impl/BusyConnectorsLoadMetric.java 2008-10-21
15:20:30 UTC (rev 1965)
@@ -30,11 +30,16 @@
*/
public class BusyConnectorsLoadMetric extends MBeanAttributeRatioLoadMetric
{
- private static final String CURRENT_THREADS_BUSY = "currentThreadsBusy";
- private static final String MAX_THREADS = "maxThreads";
+ public static final String DEFAULT_DIVIDEND_ATTRIBUTE =
"currentThreadsBusy";
+ public static final String DEFAULT_DIVISOR_ATTRIBUTE = "maxThreads";
- public BusyConnectorsLoadMetric(ThreadPoolLoadMetricSource source)
+ public BusyConnectorsLoadMetric()
{
- super(source, CURRENT_THREADS_BUSY, MAX_THREADS);
+ super(DEFAULT_DIVIDEND_ATTRIBUTE, DEFAULT_DIVISOR_ATTRIBUTE);
}
+
+ public BusyConnectorsLoadMetric(String currentThreadsBusy, String maxThreads)
+ {
+ super(currentThreadsBusy, maxThreads);
+ }
}
Modified:
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/metric/impl/ConnectionPoolLoadMetricSource.java
===================================================================
---
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/metric/impl/ConnectionPoolLoadMetricSource.java 2008-10-20
11:53:50 UTC (rev 1964)
+++
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/metric/impl/ConnectionPoolLoadMetricSource.java 2008-10-21
15:20:30 UTC (rev 1965)
@@ -21,10 +21,13 @@
*/
package org.jboss.modcluster.load.metric.impl;
+import java.util.Collection;
+
import javax.management.MBeanServer;
import javax.management.MalformedObjectNameException;
-import org.jboss.modcluster.load.metric.LoadMetricSourceRegistration;
+import org.jboss.modcluster.load.metric.LoadMetric;
+import org.jboss.modcluster.load.metric.LoadMetricSource;
/**
* {@link LoadMetricSource} implementation that simplifies generic mbean access to
@@ -32,7 +35,7 @@
*/
public class ConnectionPoolLoadMetricSource extends MBeanQueryLoadMetricSource
{
- private static final String DEFAULT_PATTERN =
"jboss.jca:service=ManagedConnectionPool,*";
+ public static final String DEFAULT_PATTERN =
"jboss.jca:service=ManagedConnectionPool,*";
/**
* Create a new ConnectionPoolLoadMetricSource.
@@ -41,9 +44,9 @@
* @param server
* @throws MalformedObjectNameException
*/
- public ConnectionPoolLoadMetricSource(LoadMetricSourceRegistration registration,
MBeanServer server) throws MalformedObjectNameException
+ public
ConnectionPoolLoadMetricSource(Collection<LoadMetric<MBeanQueryLoadContext>>
metrics, MBeanServer server) throws MalformedObjectNameException
{
- super(registration, DEFAULT_PATTERN, server);
+ super(metrics, DEFAULT_PATTERN, server);
}
/**
@@ -54,9 +57,9 @@
* @param server
* @throws MalformedObjectNameException
*/
- public ConnectionPoolLoadMetricSource(LoadMetricSourceRegistration registration,
String pattern, MBeanServer server) throws MalformedObjectNameException
+ public
ConnectionPoolLoadMetricSource(Collection<LoadMetric<MBeanQueryLoadContext>>
metrics, String pattern, MBeanServer server) throws MalformedObjectNameException
{
- super(registration, pattern, server);
+ super(metrics, pattern, server);
}
/**
@@ -65,9 +68,9 @@
* @param registration
* @throws MalformedObjectNameException
*/
- public ConnectionPoolLoadMetricSource(LoadMetricSourceRegistration registration)
throws MalformedObjectNameException
+ public
ConnectionPoolLoadMetricSource(Collection<LoadMetric<MBeanQueryLoadContext>>
metrics) throws MalformedObjectNameException
{
- super(registration, DEFAULT_PATTERN);
+ super(metrics, DEFAULT_PATTERN);
}
/**
@@ -77,8 +80,8 @@
* @param pattern
* @throws MalformedObjectNameException
*/
- public ConnectionPoolLoadMetricSource(LoadMetricSourceRegistration registration,
String pattern) throws MalformedObjectNameException
+ public
ConnectionPoolLoadMetricSource(Collection<LoadMetric<MBeanQueryLoadContext>>
metrics, String pattern) throws MalformedObjectNameException
{
- super(registration, pattern);
+ super(metrics, pattern);
}
}
Modified:
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/metric/impl/ConnectionPoolUsageLoadMetric.java
===================================================================
---
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/metric/impl/ConnectionPoolUsageLoadMetric.java 2008-10-20
11:53:50 UTC (rev 1964)
+++
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/metric/impl/ConnectionPoolUsageLoadMetric.java 2008-10-21
15:20:30 UTC (rev 1965)
@@ -30,16 +30,16 @@
*/
public class ConnectionPoolUsageLoadMetric extends MBeanAttributeRatioLoadMetric
{
- private static final String DEFAULT_USED_ATTRIBUTE =
"InUseConnectionCount";
- private static final String DEFAULT_MAX_ATTRIBUTE = "MaxSize";
+ public static final String DEFAULT_DIVIDEND_ATTRIBUTE =
"InUseConnectionCount";
+ public static final String DEFAULT_DIVISOR_ATTRIBUTE = "MaxSize";
- public ConnectionPoolUsageLoadMetric(ConnectionPoolLoadMetricSource source)
+ public ConnectionPoolUsageLoadMetric()
{
- this(source, DEFAULT_USED_ATTRIBUTE, DEFAULT_MAX_ATTRIBUTE);
+ super(DEFAULT_DIVIDEND_ATTRIBUTE, DEFAULT_DIVISOR_ATTRIBUTE);
}
- public ConnectionPoolUsageLoadMetric(ConnectionPoolLoadMetricSource source, String
usedAttribute, String maxAttribute)
+ public ConnectionPoolUsageLoadMetric(String usedAttribute, String maxAttribute)
{
- super(source, usedAttribute, maxAttribute);
+ super(usedAttribute, maxAttribute);
}
}
Modified:
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/metric/impl/HeapMemoryUsageLoadMetric.java
===================================================================
---
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/metric/impl/HeapMemoryUsageLoadMetric.java 2008-10-20
11:53:50 UTC (rev 1964)
+++
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/metric/impl/HeapMemoryUsageLoadMetric.java 2008-10-21
15:20:30 UTC (rev 1965)
@@ -25,9 +25,9 @@
import java.lang.management.MemoryMXBean;
import java.lang.management.MemoryUsage;
-import org.jboss.modcluster.load.metric.LoadMetricSourceRegistration;
+import org.jboss.modcluster.load.metric.LoadContext;
+import org.jboss.modcluster.load.metric.LoadMetric;
-
/**
* {@link LoadMetric} implementation that returns the heap memory usage ratio.
*
@@ -37,33 +37,21 @@
{
private final MemoryMXBean bean;
- /**
- * Create a new SystemLoadBalanceMetricProvider.
- *
- * @param registration
- */
- public HeapMemoryUsageLoadMetric(LoadMetricSourceRegistration registration)
+ public HeapMemoryUsageLoadMetric()
{
- this(registration, ManagementFactory.getMemoryMXBean());
+ this(ManagementFactory.getMemoryMXBean());
}
-
- /**
- * Create a new SystemLoadBalanceMetricProvider.
- *
- * @param registration
- */
- public HeapMemoryUsageLoadMetric(LoadMetricSourceRegistration registration,
MemoryMXBean bean)
+
+ public HeapMemoryUsageLoadMetric(MemoryMXBean bean)
{
- super(registration);
-
this.bean = bean;
}
-
+
/**
* @{inheritDoc}
* @see org.jboss.modcluster.load.metric.LoadMetric#getLoad()
*/
- public double getLoad()
+ public double getLoad(LoadContext context)
{
MemoryUsage usage = this.bean.getHeapMemoryUsage();
Modified:
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/metric/impl/MBeanAttributeLoadMetric.java
===================================================================
---
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/metric/impl/MBeanAttributeLoadMetric.java 2008-10-20
11:53:50 UTC (rev 1964)
+++
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/metric/impl/MBeanAttributeLoadMetric.java 2008-10-21
15:20:30 UTC (rev 1965)
@@ -29,28 +29,24 @@
* Generic {@link LoadMetric} whose load is the aggregated value of an mbean attribute.
* @author Paul Ferraro
*/
-public class MBeanAttributeLoadMetric extends AbstractLoadMetric
+public class MBeanAttributeLoadMetric extends
AbstractLoadMetric<MBeanQueryLoadContext>
{
- private final MBeanQueryLoadMetricSource source;
private final String attribute;
- protected MBeanAttributeLoadMetric(MBeanQueryLoadMetricSource source, String
attribute)
+ public MBeanAttributeLoadMetric(String attribute)
{
- this.source = source;
this.attribute = attribute;
-
- this.source.add(this);
}
/**
* @{inheritDoc}
- * @see org.jboss.modcluster.load.metric.LoadMetric#getLoad()
+ * @see
org.jboss.modcluster.load.metric.LoadMetric#getLoad(org.jboss.modcluster.load.metric.LoadContext)
*/
- public double getLoad() throws JMException
+ public double getLoad(MBeanQueryLoadContext context) throws JMException
{
double load = 0;
- List<Number> results = this.source.getAttributes(this.attribute,
Number.class);
+ List<Number> results = context.getAttributes(this.attribute, Number.class);
for (Number result: results)
{
Modified:
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/metric/impl/MBeanAttributeRatioLoadMetric.java
===================================================================
---
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/metric/impl/MBeanAttributeRatioLoadMetric.java 2008-10-20
11:53:50 UTC (rev 1964)
+++
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/metric/impl/MBeanAttributeRatioLoadMetric.java 2008-10-21
15:20:30 UTC (rev 1965)
@@ -31,30 +31,26 @@
* Generic {@link LoadMetric} whose load is the ratio of 2 aggregated mbean attributes.
* @author Paul Ferraro
*/
-public class MBeanAttributeRatioLoadMetric extends AbstractLoadMetric
+public class MBeanAttributeRatioLoadMetric extends
AbstractLoadMetric<MBeanQueryLoadContext>
{
- private final MBeanQueryLoadMetricSource source;
private final String dividendAttribute;
private final String divisorAttribute;
- protected MBeanAttributeRatioLoadMetric(MBeanQueryLoadMetricSource source, String
currentAttribute, String maxAttribute)
+ public MBeanAttributeRatioLoadMetric(String currentAttribute, String maxAttribute)
{
- this.source = source;
this.dividendAttribute = currentAttribute;
this.divisorAttribute = maxAttribute;
-
- this.source.add(this);
}
/**
* @{inheritDoc}
* @see org.jboss.modcluster.load.metric.LoadMetric#getLoad()
*/
- public double getLoad() throws JMException
+ public double getLoad(MBeanQueryLoadContext context) throws JMException
{
double dividend = 0;
- List<Number> results = this.source.getAttributes(this.dividendAttribute,
Number.class);
+ List<Number> results = context.getAttributes(this.dividendAttribute,
Number.class);
for (Number result: results)
{
@@ -63,7 +59,7 @@
double divisor = 0;
- results = this.source.getAttributes(this.divisorAttribute, Number.class);
+ results = context.getAttributes(this.divisorAttribute, Number.class);
for (Number result: results)
{
Added:
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/metric/impl/MBeanLoadContext.java
===================================================================
---
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/metric/impl/MBeanLoadContext.java
(rev 0)
+++
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/metric/impl/MBeanLoadContext.java 2008-10-21
15:20:30 UTC (rev 1965)
@@ -0,0 +1,67 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+package org.jboss.modcluster.load.metric.impl;
+
+import javax.management.JMException;
+import javax.management.MBeanServer;
+import javax.management.ObjectName;
+
+import org.jboss.modcluster.load.metric.LoadContext;
+
+/**
+ * @author Paul Ferraro
+ *
+ */
+public class MBeanLoadContext implements LoadContext
+{
+ private final MBeanServer server;
+ private final ObjectName name;
+
+ public MBeanLoadContext(MBeanServer server, ObjectName name)
+ {
+ this.server = server;
+ this.name = name;
+ }
+
+ /**
+ * @{inheritDoc}
+ * @see org.jboss.modcluster.load.metric.LoadContext#close()
+ */
+ public void close()
+ {
+ // nothing to close
+ }
+
+ /**
+ * Returns the value of the specified attribute, casted to the specified type.
+ *
+ * @param <T> the type of the attribute
+ * @param attribute an attribute name
+ * @param targetClass the type of the attribute
+ * @return the attribute value
+ * @throws JMException
+ */
+ public <T> T getAttribute(String attribute, Class<T> targetClass) throws
JMException
+ {
+ return targetClass.cast(this.server.getAttribute(this.name, attribute));
+ }
+}
Added:
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/metric/impl/MBeanLoadMetricSource.java
===================================================================
---
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/metric/impl/MBeanLoadMetricSource.java
(rev 0)
+++
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/metric/impl/MBeanLoadMetricSource.java 2008-10-21
15:20:30 UTC (rev 1965)
@@ -0,0 +1,61 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+package org.jboss.modcluster.load.metric.impl;
+
+import java.lang.management.ManagementFactory;
+import java.util.Collection;
+
+import javax.management.MBeanServer;
+import javax.management.MalformedObjectNameException;
+import javax.management.ObjectName;
+
+import org.jboss.modcluster.load.metric.LoadMetric;
+
+/**
+ * @author Paul Ferraro
+ *
+ */
+public class MBeanLoadMetricSource extends
AbstractLoadMetricSource<MBeanLoadContext>
+{
+ private final MBeanLoadContext context;
+
+ public MBeanLoadMetricSource(Collection<LoadMetric<MBeanLoadContext>>
metrics, String name) throws MalformedObjectNameException
+ {
+ this(metrics, name, ManagementFactory.getPlatformMBeanServer());
+ }
+
+ public MBeanLoadMetricSource(Collection<LoadMetric<MBeanLoadContext>>
metrics, String name, MBeanServer server) throws MalformedObjectNameException
+ {
+ super(metrics);
+
+ this.context = new MBeanLoadContext(server, ObjectName.getInstance(name));
+ }
+
+ /**
+ * @{inheritDoc}
+ * @see org.jboss.modcluster.load.metric.LoadMetricSource#createContext()
+ */
+ public MBeanLoadContext createContext()
+ {
+ return this.context;
+ }
+}
Added:
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/metric/impl/MBeanQueryLoadContext.java
===================================================================
---
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/metric/impl/MBeanQueryLoadContext.java
(rev 0)
+++
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/metric/impl/MBeanQueryLoadContext.java 2008-10-21
15:20:30 UTC (rev 1965)
@@ -0,0 +1,79 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+package org.jboss.modcluster.load.metric.impl;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Set;
+
+import javax.management.JMException;
+import javax.management.MBeanServer;
+import javax.management.ObjectName;
+
+import org.jboss.modcluster.load.metric.LoadContext;
+
+/**
+ * @author Paul Ferraro
+ *
+ */
+public class MBeanQueryLoadContext implements LoadContext
+{
+ private final MBeanServer server;
+ private final Set<ObjectName> names;
+
+ @SuppressWarnings("unchecked")
+ public MBeanQueryLoadContext(MBeanServer server, ObjectName pattern)
+ {
+ this.server = server;
+ this.names = pattern.isPattern() ? server.queryNames(pattern, null) :
Collections.singleton(pattern);
+ }
+
+ /**
+ * @{inheritDoc}
+ * @see org.jboss.modcluster.load.metric.LoadContext#close()
+ */
+ public void close()
+ {
+ // nothing to close
+ }
+
+ /**
+ * Collects the attribute values for each mbean matching the object name pattern
+ * @param <T> the type of the attribute
+ * @param attribute the mbean attribute name
+ * @param targetClass the type of the attribute
+ * @return a list of attribute values for each mbean
+ * @throws JMException
+ */
+ public <T> List<T> getAttributes(String attribute, Class<T>
targetClass) throws JMException
+ {
+ List<T> list = new ArrayList<T>(this.names.size());
+
+ for (ObjectName name: this.names)
+ {
+ list.add(targetClass.cast(this.server.getAttribute(name, attribute)));
+ }
+
+ return list;
+ }
+}
Modified:
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/metric/impl/MBeanQueryLoadMetricSource.java
===================================================================
---
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/metric/impl/MBeanQueryLoadMetricSource.java 2008-10-20
11:53:50 UTC (rev 1964)
+++
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/metric/impl/MBeanQueryLoadMetricSource.java 2008-10-21
15:20:30 UTC (rev 1965)
@@ -22,91 +22,44 @@
package org.jboss.modcluster.load.metric.impl;
import java.lang.management.ManagementFactory;
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.List;
-import java.util.Set;
+import java.util.Collection;
-import javax.management.JMException;
import javax.management.MBeanServer;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import org.jboss.modcluster.load.metric.LoadMetric;
import org.jboss.modcluster.load.metric.LoadMetricSource;
-import org.jboss.modcluster.load.metric.LoadMetricSourceRegistration;
/**
* Abstract {@link LoadMetricSource} that queries the mbean server for the set of beans
to be made available to its registered {@link LoadMetric}s.
*
* @author Paul Ferraro
*/
-public abstract class MBeanQueryLoadMetricSource extends AbstractLoadMetricSource
+public class MBeanQueryLoadMetricSource extends
AbstractLoadMetricSource<MBeanQueryLoadContext>
{
private final MBeanServer server;
- private final ObjectName pattern;
+ private final ObjectName name;
- private transient Set<ObjectName> objectNames;
-
- protected MBeanQueryLoadMetricSource(LoadMetricSourceRegistration registration, String
pattern) throws MalformedObjectNameException
+ public
MBeanQueryLoadMetricSource(Collection<LoadMetric<MBeanQueryLoadContext>>
metrics, String pattern) throws MalformedObjectNameException
{
- this(registration, pattern, ManagementFactory.getPlatformMBeanServer());
+ this(metrics, pattern, ManagementFactory.getPlatformMBeanServer());
}
- protected MBeanQueryLoadMetricSource(LoadMetricSourceRegistration registration, String
pattern, MBeanServer server) throws MalformedObjectNameException
+ public
MBeanQueryLoadMetricSource(Collection<LoadMetric<MBeanQueryLoadContext>>
metrics, String pattern, MBeanServer server) throws MalformedObjectNameException
{
- super(registration);
+ super(metrics);
- this.pattern = ObjectName.getInstance(pattern);
this.server = server;
+ this.name = ObjectName.getInstance(pattern);
}
/**
* @{inheritDoc}
- * @see org.jboss.modcluster.load.metric.impl.AbstractLoadMetricSource#prepare()
+ * @see org.jboss.modcluster.load.metric.LoadMetricSource#createContext()
*/
- @Override
- public void prepare()
+ public MBeanQueryLoadContext createContext()
{
- this.objectNames = this.queryNames();
+ return new MBeanQueryLoadContext(this.server, this.name);
}
-
- @SuppressWarnings("unchecked")
- private Set<ObjectName> queryNames()
- {
- return this.pattern.isPattern() ? this.server.queryNames(this.pattern, null) :
Collections.singleton(this.pattern);
- }
-
- /**
- * @{inheritDoc}
- * @see org.jboss.modcluster.load.metric.impl.AbstractLoadMetricSource#cleanup()
- */
- @Override
- public void cleanup()
- {
- this.objectNames = null;
- }
-
- /**
- * Collects the attribute values for each mbean matching the object name pattern
- * @param <T> the type of the attribute
- * @param attribute the mbean attribute name
- * @param targetClass the type of the attribute
- * @return a list of attribute values for each mbean
- * @throws JMException
- */
- public <T> List<T> getAttributes(String attribute, Class<T>
targetClass) throws JMException
- {
- // In case load was queried via mbean
- Set<ObjectName> names = (this.objectNames != null) ? this.objectNames :
this.queryNames();
-
- List<T> list = new ArrayList<T>(names.size());
-
- for (ObjectName name: names)
- {
- list.add(targetClass.cast(this.server.getAttribute(name, attribute)));
- }
-
- return list;
- }
}
Modified:
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/metric/impl/OperatingSystemLoadMetricSource.java
===================================================================
---
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/metric/impl/OperatingSystemLoadMetricSource.java 2008-10-20
11:53:50 UTC (rev 1964)
+++
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/metric/impl/OperatingSystemLoadMetricSource.java 2008-10-21
15:20:30 UTC (rev 1965)
@@ -22,105 +22,40 @@
package org.jboss.modcluster.load.metric.impl;
import java.lang.management.ManagementFactory;
-import java.util.Set;
-import java.util.TreeSet;
+import java.util.Collection;
-import javax.management.JMException;
-import javax.management.MBeanAttributeInfo;
-import javax.management.MBeanInfo;
import javax.management.MBeanServer;
import javax.management.MalformedObjectNameException;
-import javax.management.ObjectName;
+import org.jboss.modcluster.load.metric.LoadMetric;
import org.jboss.modcluster.load.metric.LoadMetricSource;
-import org.jboss.modcluster.load.metric.LoadMetricSourceRegistration;
/**
* Abstract {@link LoadMetricSource} implementation that simplifies generic access to the
{@link java.lang.management.OperatingSystemMXBean}.
*
* @author Paul Ferraro
*/
-public class OperatingSystemLoadMetricSource extends AbstractLoadMetricSource
+public class OperatingSystemLoadMetricSource extends MBeanLoadMetricSource
{
- private final MBeanServer server;
- private final ObjectName name;
-
/**
* Create a new OperatingSystemLoadMetricSource.
*
* @param registration
+ * @throws MalformedObjectNameException
*/
- public OperatingSystemLoadMetricSource(LoadMetricSourceRegistration registration)
+ public
OperatingSystemLoadMetricSource(Collection<LoadMetric<MBeanLoadContext>>
metrics) throws MalformedObjectNameException
{
- this(registration, ManagementFactory.getPlatformMBeanServer());
+ super(metrics, ManagementFactory.OPERATING_SYSTEM_MXBEAN_NAME);
}
/**
* Create a new OperatingSystemLoadMetricSource.
*
* @param registration
+ * @throws MalformedObjectNameException
*/
- public OperatingSystemLoadMetricSource(LoadMetricSourceRegistration registration,
MBeanServer server)
+ public
OperatingSystemLoadMetricSource(Collection<LoadMetric<MBeanLoadContext>>
metrics, MBeanServer server) throws MalformedObjectNameException
{
- super(registration);
-
- this.server = server;
-
- try
- {
- this.name =
ObjectName.getInstance(ManagementFactory.OPERATING_SYSTEM_MXBEAN_NAME);
- }
- catch (MalformedObjectNameException e)
- {
- // Should never happen for a platform mxbean
- throw new RuntimeException(e);
- }
+ super(metrics, ManagementFactory.OPERATING_SYSTEM_MXBEAN_NAME, server);
}
-
- /**
- * Indicates whether or not all of the specified attributes exist on this mbean
- *
- * @param attributes any number of attributes
- * @return true if all of the specified attributes exist, false otherwise
- */
- public boolean exists(String... attributes)
- {
- Set<String> missingAttributes = new TreeSet<String>();
-
- for (String attribute: attributes)
- {
- missingAttributes.add(attribute);
- }
-
- try
- {
- MBeanInfo info = this.server.getMBeanInfo(this.name);
-
- for (MBeanAttributeInfo attribute: info.getAttributes())
- {
- missingAttributes.remove(attribute.getName());
- }
-
- return missingAttributes.isEmpty();
- }
- catch (JMException e)
- {
- // Should never happen for a platform mxbean
- throw new RuntimeException(e);
- }
- }
-
- /**
- * Returns the value of the specified attribute, casted to the specified type.
- *
- * @param <T> the type of the attribute
- * @param attribute an attribute name
- * @param targetClass the type of the attribute
- * @return the attribute value
- * @throws JMException
- */
- public <T> T getAttribute(String attribute, Class<T> targetClass) throws
JMException
- {
- return targetClass.cast(this.server.getAttribute(this.name, attribute));
- }
}
Modified:
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/metric/impl/ReceiveTrafficLoadMetric.java
===================================================================
---
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/metric/impl/ReceiveTrafficLoadMetric.java 2008-10-20
11:53:50 UTC (rev 1964)
+++
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/metric/impl/ReceiveTrafficLoadMetric.java 2008-10-21
15:20:30 UTC (rev 1965)
@@ -31,23 +31,27 @@
*/
public class ReceiveTrafficLoadMetric extends MBeanAttributeLoadMetric
{
- private static final String RECEIVE_COUNT = "bytesReceived";
+ public static final String DEFAULT_ATTRIBUTE = "bytesReceived";
private final DeterministicLoadState state = new DeterministicLoadState();
- public ReceiveTrafficLoadMetric(RequestProcessorLoadMetricSource source)
+ public ReceiveTrafficLoadMetric()
{
- super(source, RECEIVE_COUNT);
+ super(DEFAULT_ATTRIBUTE);
}
+ public ReceiveTrafficLoadMetric(String attribute)
+ {
+ super(attribute);
+ }
+
/**
* @{inheritDoc}
- * @see org.jboss.modcluster.load.metric.impl.MBeanAttributeLoadMetric#getLoad()
+ * @see
org.jboss.modcluster.load.metric.impl.MBeanAttributeLoadMetric#getLoad(org.jboss.modcluster.load.metric.impl.MBeanQueryLoadContext)
*/
@Override
- public double getLoad() throws JMException
+ public double getLoad(MBeanQueryLoadContext context) throws JMException
{
- // Convert to KB/sec
- return this.state.delta(super.getLoad() / 1000);
+ return this.state.delta(super.getLoad(context) / 1000);
}
}
Modified:
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/metric/impl/RequestCountLoadMetric.java
===================================================================
---
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/metric/impl/RequestCountLoadMetric.java 2008-10-20
11:53:50 UTC (rev 1964)
+++
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/metric/impl/RequestCountLoadMetric.java 2008-10-21
15:20:30 UTC (rev 1965)
@@ -32,22 +32,27 @@
*/
public class RequestCountLoadMetric extends MBeanAttributeLoadMetric
{
- private static final String REQUEST_COUNT = "requestCount";
+ public static final String DEFAULT_ATTRIBUTE = "requestCount";
private final DeterministicLoadState state = new DeterministicLoadState();
- public RequestCountLoadMetric(RequestProcessorLoadMetricSource source)
+ public RequestCountLoadMetric()
{
- super(source, REQUEST_COUNT);
+ super(DEFAULT_ATTRIBUTE);
}
+ public RequestCountLoadMetric(String attribute)
+ {
+ super(attribute);
+ }
+
/**
* @{inheritDoc}
- * @see org.jboss.modcluster.load.metric.impl.MBeanAttributeLoadMetric#getLoad()
+ * @see
org.jboss.modcluster.load.metric.impl.MBeanAttributeLoadMetric#getLoad(org.jboss.modcluster.load.metric.impl.MBeanQueryLoadContext)
*/
@Override
- public double getLoad() throws JMException
+ public double getLoad(MBeanQueryLoadContext context) throws JMException
{
- return this.state.delta(super.getLoad());
+ return this.state.delta(super.getLoad(context));
}
}
Modified:
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/metric/impl/RequestProcessorLoadMetricSource.java
===================================================================
---
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/metric/impl/RequestProcessorLoadMetricSource.java 2008-10-20
11:53:50 UTC (rev 1964)
+++
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/metric/impl/RequestProcessorLoadMetricSource.java 2008-10-21
15:20:30 UTC (rev 1965)
@@ -21,10 +21,13 @@
*/
package org.jboss.modcluster.load.metric.impl;
+import java.util.Collection;
+
import javax.management.MBeanServer;
import javax.management.MalformedObjectNameException;
-import org.jboss.modcluster.load.metric.LoadMetricSourceRegistration;
+import org.jboss.modcluster.load.metric.LoadMetric;
+import org.jboss.modcluster.load.metric.LoadMetricSource;
/**
* {@link LoadMetricSource} implementation that simplifies generic mbean access to
@@ -34,7 +37,7 @@
*/
public class RequestProcessorLoadMetricSource extends MBeanQueryLoadMetricSource
{
- private static final String DEFAULT_PATTERN =
"jboss.web:type=GlobalRequestProcessor,*";
+ public static final String DEFAULT_PATTERN =
"jboss.web:type=GlobalRequestProcessor,*";
/**
* Create a new GlobalRequestProcessorLoadMetricSource.
@@ -42,9 +45,9 @@
* @param registration
* @throws MalformedObjectNameException
*/
- public RequestProcessorLoadMetricSource(LoadMetricSourceRegistration registration)
throws MalformedObjectNameException
+ public
RequestProcessorLoadMetricSource(Collection<LoadMetric<MBeanQueryLoadContext>>
metrics) throws MalformedObjectNameException
{
- super(registration, DEFAULT_PATTERN);
+ super(metrics, DEFAULT_PATTERN);
}
/**
@@ -54,9 +57,9 @@
* @param pattern
* @throws MalformedObjectNameException
*/
- public RequestProcessorLoadMetricSource(LoadMetricSourceRegistration registration,
String pattern) throws MalformedObjectNameException
+ public
RequestProcessorLoadMetricSource(Collection<LoadMetric<MBeanQueryLoadContext>>
metrics, String pattern) throws MalformedObjectNameException
{
- super(registration, pattern);
+ super(metrics, pattern);
}
/**
@@ -66,9 +69,9 @@
* @param server
* @throws MalformedObjectNameException
*/
- public RequestProcessorLoadMetricSource(LoadMetricSourceRegistration registration,
MBeanServer server) throws MalformedObjectNameException
+ public
RequestProcessorLoadMetricSource(Collection<LoadMetric<MBeanQueryLoadContext>>
metrics, MBeanServer server) throws MalformedObjectNameException
{
- super(registration, DEFAULT_PATTERN, server);
+ super(metrics, DEFAULT_PATTERN, server);
}
/**
@@ -79,8 +82,8 @@
* @param server
* @throws MalformedObjectNameException
*/
- public RequestProcessorLoadMetricSource(LoadMetricSourceRegistration registration,
String pattern, MBeanServer server) throws MalformedObjectNameException
+ public
RequestProcessorLoadMetricSource(Collection<LoadMetric<MBeanQueryLoadContext>>
metrics, String pattern, MBeanServer server) throws MalformedObjectNameException
{
- super(registration, pattern, server);
+ super(metrics, pattern, server);
}
}
Modified:
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/metric/impl/SendTrafficLoadMetric.java
===================================================================
---
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/metric/impl/SendTrafficLoadMetric.java 2008-10-20
11:53:50 UTC (rev 1964)
+++
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/metric/impl/SendTrafficLoadMetric.java 2008-10-21
15:20:30 UTC (rev 1965)
@@ -32,23 +32,28 @@
*/
public class SendTrafficLoadMetric extends MBeanAttributeLoadMetric
{
- private static final String SEND_COUNT = "bytesSent";
+ public static final String DEFAULT_ATTRIBUTE = "bytesSent";
private final DeterministicLoadState state = new DeterministicLoadState();
- public SendTrafficLoadMetric(RequestProcessorLoadMetricSource source)
+ public SendTrafficLoadMetric()
{
- super(source, SEND_COUNT);
+ this(DEFAULT_ATTRIBUTE);
}
+ public SendTrafficLoadMetric(String attribute)
+ {
+ super(attribute);
+ }
+
/**
* @{inheritDoc}
* @see org.jboss.modcluster.load.metric.impl.MBeanAttributeLoadMetric#getLoad()
*/
@Override
- public double getLoad() throws JMException
+ public double getLoad(MBeanQueryLoadContext context) throws JMException
{
// Convert to KB/sec
- return this.state.delta(super.getLoad() / 1000);
+ return this.state.delta(super.getLoad(context) / 1000);
}
}
Modified:
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/metric/impl/SessionLoadMetricSource.java
===================================================================
---
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/metric/impl/SessionLoadMetricSource.java 2008-10-20
11:53:50 UTC (rev 1964)
+++
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/metric/impl/SessionLoadMetricSource.java 2008-10-21
15:20:30 UTC (rev 1965)
@@ -21,10 +21,13 @@
*/
package org.jboss.modcluster.load.metric.impl;
+import java.util.Collection;
+
import javax.management.MBeanServer;
import javax.management.MalformedObjectNameException;
-import org.jboss.modcluster.load.metric.LoadMetricSourceRegistration;
+import org.jboss.modcluster.load.metric.LoadMetric;
+import org.jboss.modcluster.load.metric.LoadMetricSource;
/**
* {@link LoadMetricSource} implementation that simplifies generic mbean access to
@@ -34,7 +37,7 @@
*/
public class SessionLoadMetricSource extends MBeanQueryLoadMetricSource
{
- private static final String DEFAULT_PATTERN = "jboss.web:type=Manager,*";
+ public static final String DEFAULT_PATTERN = "jboss.web:type=Manager,*";
/**
* Create a new SessionLoadMetricSource.
@@ -42,9 +45,9 @@
* @param registration
* @throws MalformedObjectNameException
*/
- public SessionLoadMetricSource(LoadMetricSourceRegistration registration) throws
MalformedObjectNameException
+ public
SessionLoadMetricSource(Collection<LoadMetric<MBeanQueryLoadContext>> metrics)
throws MalformedObjectNameException
{
- super(registration, DEFAULT_PATTERN);
+ super(metrics, DEFAULT_PATTERN);
}
/**
@@ -54,9 +57,9 @@
* @param pattern
* @throws MalformedObjectNameException
*/
- public SessionLoadMetricSource(LoadMetricSourceRegistration registration, String
pattern) throws MalformedObjectNameException
+ public
SessionLoadMetricSource(Collection<LoadMetric<MBeanQueryLoadContext>> metrics,
String pattern) throws MalformedObjectNameException
{
- super(registration, pattern);
+ super(metrics, pattern);
}
/**
@@ -66,9 +69,9 @@
* @param server
* @throws MalformedObjectNameException
*/
- public SessionLoadMetricSource(LoadMetricSourceRegistration registration, MBeanServer
server) throws MalformedObjectNameException
+ public
SessionLoadMetricSource(Collection<LoadMetric<MBeanQueryLoadContext>> metrics,
MBeanServer server) throws MalformedObjectNameException
{
- super(registration, DEFAULT_PATTERN, server);
+ super(metrics, DEFAULT_PATTERN, server);
}
/**
@@ -79,8 +82,8 @@
* @param server
* @throws MalformedObjectNameException
*/
- public SessionLoadMetricSource(LoadMetricSourceRegistration registration, String
pattern, MBeanServer server) throws MalformedObjectNameException
+ public
SessionLoadMetricSource(Collection<LoadMetric<MBeanQueryLoadContext>> metrics,
String pattern, MBeanServer server) throws MalformedObjectNameException
{
- super(registration, pattern, server);
+ super(metrics, pattern, server);
}
}
Modified:
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/metric/impl/SingleLoadMetricSource.java
===================================================================
---
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/metric/impl/SingleLoadMetricSource.java 2008-10-20
11:53:50 UTC (rev 1964)
+++
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/metric/impl/SingleLoadMetricSource.java 2008-10-21
15:20:30 UTC (rev 1965)
@@ -24,48 +24,42 @@
import java.util.Collection;
import java.util.Collections;
+import org.jboss.modcluster.load.metric.LoadContext;
import org.jboss.modcluster.load.metric.LoadMetric;
import org.jboss.modcluster.load.metric.LoadMetricSource;
-import org.jboss.modcluster.load.metric.LoadMetricSourceRegistration;
-
/**
* Abstract {@link LoadMetricSource} + {@link LoadMetric} implementation that returns a
single metric.
*
* @author Paul Ferraro
*/
-public abstract class SingleLoadMetricSource extends AbstractLoadMetric implements
LoadMetricSource
+public abstract class SingleLoadMetricSource extends
AbstractLoadMetric<LoadContext> implements LoadMetricSource<LoadContext>
{
- protected SingleLoadMetricSource(LoadMetricSourceRegistration registration)
+ // We don't need to use a load context since there's only one metric in this
source
+ private final LoadContext context = new LoadContext()
{
- registration.add(this);
- }
-
+ public void close()
+ {
+ // nothing to close
+ }
+ };
+
/**
* @{inheritDoc}
- * @see org.jboss.modcluster.load.metric.LoadMetricSource#prepare()
+ * @see org.jboss.modcluster.load.metric.LoadMetricSource#createContext()
*/
- public void prepare()
+ public LoadContext createContext()
{
- // Nothing to prepare
+ return this.context;
}
/**
* @{inheritDoc}
- * @see org.jboss.modcluster.load.metric.LoadMetricSource#cleanup()
- */
- public void cleanup()
- {
- // Nothing to cleanup
- }
-
- /**
- * @{inheritDoc}
* @see org.jboss.modcluster.load.metric.LoadMetricSource#getMetrics()
*/
- public Collection<LoadMetric> getMetrics()
+ public Collection<LoadMetric<LoadContext>> getMetrics()
{
- // Silly that we need to cast to prevent a compiler error, no?
- return Collections.singleton((LoadMetric) this);
+ // Silly that we need to down cast to prevent a compiler error, no?
+ return Collections.singleton((LoadMetric<LoadContext>) this);
}
}
Modified:
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/metric/impl/SystemMemoryUsageLoadMetric.java
===================================================================
---
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/metric/impl/SystemMemoryUsageLoadMetric.java 2008-10-20
11:53:50 UTC (rev 1964)
+++
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/metric/impl/SystemMemoryUsageLoadMetric.java 2008-10-21
15:20:30 UTC (rev 1965)
@@ -21,58 +21,45 @@
*/
package org.jboss.modcluster.load.metric.impl;
-import java.util.Arrays;
-
+import javax.management.AttributeNotFoundException;
import javax.management.JMException;
import org.jboss.logging.Logger;
import org.jboss.modcluster.load.metric.LoadMetric;
-
/**
* {@link LoadMetric} implementation that uses {@link
com.sun.management.OperatingSystemMXBean}
* to determine system memory usage.
*
* @author Paul Ferraro
*/
-public class SystemMemoryUsageLoadMetric extends AbstractLoadMetric
+public class SystemMemoryUsageLoadMetric extends
AbstractLoadMetric<MBeanLoadContext>
{
- private static final String FREE_MEMORY = "FreePhysicalMemorySize";
- private static final String TOTAL_MEMORY = "TotalPhysicalMemorySize";
+ public static final String FREE_MEMORY = "FreePhysicalMemorySize";
+ public static final String TOTAL_MEMORY = "TotalPhysicalMemorySize";
- private final OperatingSystemLoadMetricSource source;
- private final Logger log = Logger.getLogger(this.getClass());
-
+ private Logger logger = Logger.getLogger(this.getClass());
+
/**
- * Create a new SystemMemoryUsageLoadMetric.
- *
- * @param registration
+ * @{inheritDoc}
+ * @see org.jboss.modcluster.load.metric.LoadMetric#getLoad()
*/
- public SystemMemoryUsageLoadMetric(OperatingSystemLoadMetricSource source)
+ public double getLoad(MBeanLoadContext context) throws JMException
{
- this.source = source;
-
- if (this.source.exists(FREE_MEMORY, TOTAL_MEMORY))
+ try
{
- this.source.add(this);
+ double free = context.getAttribute(FREE_MEMORY, Number.class).doubleValue();
+ double total = context.getAttribute(TOTAL_MEMORY, Number.class).doubleValue();
+
+ return free / total;
}
- else
+ catch (AttributeNotFoundException e)
{
- this.log.warn(this.getClass().getName() + " discarded."
- + "This metric requires the following OperatingSystemMXBean
attributes: "
- + Arrays.asList(FREE_MEMORY, TOTAL_MEMORY));
+ this.logger.warn(this.getClass().getSimpleName() + " requires
com.sun.management.OperatingSystemMXBean.");
+
+ this.setWeight(0);
+
+ return 0;
}
}
-
- /**
- * @{inheritDoc}
- * @see org.jboss.modcluster.load.metric.LoadMetric#getLoad()
- */
- public double getLoad() throws JMException
- {
- long free = this.source.getAttribute(FREE_MEMORY, Long.class).longValue();
- long total = this.source.getAttribute(TOTAL_MEMORY, Long.class).longValue();
-
- return ((double) free) / total;
- }
}
Modified:
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/metric/impl/ThreadCountLoadMetric.java
===================================================================
---
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/metric/impl/ThreadCountLoadMetric.java 2008-10-20
11:53:50 UTC (rev 1964)
+++
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/metric/impl/ThreadCountLoadMetric.java 2008-10-21
15:20:30 UTC (rev 1965)
@@ -24,7 +24,7 @@
import java.lang.management.ManagementFactory;
import java.lang.management.ThreadMXBean;
-import org.jboss.modcluster.load.metric.LoadMetricSourceRegistration;
+import org.jboss.modcluster.load.metric.LoadContext;
/**
* @author Paul Ferraro
@@ -33,34 +33,22 @@
public class ThreadCountLoadMetric extends SingleLoadMetricSource
{
private final ThreadMXBean bean;
-
- /**
- * Create a new ThreadCountLoadMetric.
- *
- * @param registration
- */
- public ThreadCountLoadMetric(LoadMetricSourceRegistration registration)
+
+ public ThreadCountLoadMetric()
{
- this(registration, ManagementFactory.getThreadMXBean());
+ this(ManagementFactory.getThreadMXBean());
}
- /**
- * Create a new ThreadCountLoadMetric.
- *
- * @param registration
- */
- public ThreadCountLoadMetric(LoadMetricSourceRegistration registration, ThreadMXBean
bean)
+ public ThreadCountLoadMetric(ThreadMXBean bean)
{
- super(registration);
-
this.bean = bean;
}
-
+
/**
* @{inheritDoc}
* @see org.jboss.modcluster.load.metric.LoadMetric#getLoad()
*/
- public double getLoad()
+ public double getLoad(LoadContext context)
{
return this.bean.getThreadCount();
}
Modified:
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/metric/impl/ThreadPoolLoadMetricSource.java
===================================================================
---
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/metric/impl/ThreadPoolLoadMetricSource.java 2008-10-20
11:53:50 UTC (rev 1964)
+++
trunk/mod_cluster/src/main/java/org/jboss/modcluster/load/metric/impl/ThreadPoolLoadMetricSource.java 2008-10-21
15:20:30 UTC (rev 1965)
@@ -21,10 +21,13 @@
*/
package org.jboss.modcluster.load.metric.impl;
+import java.util.Collection;
+
import javax.management.MBeanServer;
import javax.management.MalformedObjectNameException;
-import org.jboss.modcluster.load.metric.LoadMetricSourceRegistration;
+import org.jboss.modcluster.load.metric.LoadMetric;
+import org.jboss.modcluster.load.metric.LoadMetricSource;
/**
* {@link LoadMetricSource} implementation that simplifies generic mbean access to
@@ -34,7 +37,7 @@
*/
public class ThreadPoolLoadMetricSource extends MBeanQueryLoadMetricSource
{
- private static final String DEFAULT_PATTERN =
"jboss.web:type=ThreadPool,*";
+ public static final String DEFAULT_PATTERN = "jboss.web:type=ThreadPool,*";
/**
* Create a new ThreadPoolLoadMetricSource.
@@ -42,9 +45,9 @@
* @param registration
* @throws MalformedObjectNameException
*/
- public ThreadPoolLoadMetricSource(LoadMetricSourceRegistration registration) throws
MalformedObjectNameException
+ public
ThreadPoolLoadMetricSource(Collection<LoadMetric<MBeanQueryLoadContext>>
metrics) throws MalformedObjectNameException
{
- super(registration, DEFAULT_PATTERN);
+ super(metrics, DEFAULT_PATTERN);
}
/**
@@ -54,9 +57,9 @@
* @param patterh
* @throws MalformedObjectNameException
*/
- public ThreadPoolLoadMetricSource(LoadMetricSourceRegistration registration, String
pattern) throws MalformedObjectNameException
+ public
ThreadPoolLoadMetricSource(Collection<LoadMetric<MBeanQueryLoadContext>>
metrics, String pattern) throws MalformedObjectNameException
{
- super(registration, pattern);
+ super(metrics, pattern);
}
/**
@@ -66,9 +69,9 @@
* @param server
* @throws MalformedObjectNameException
*/
- public ThreadPoolLoadMetricSource(LoadMetricSourceRegistration registration,
MBeanServer server) throws MalformedObjectNameException
+ public
ThreadPoolLoadMetricSource(Collection<LoadMetric<MBeanQueryLoadContext>>
metrics, MBeanServer server) throws MalformedObjectNameException
{
- super(registration, DEFAULT_PATTERN, server);
+ super(metrics, DEFAULT_PATTERN, server);
}
/**
@@ -79,8 +82,8 @@
* @param server
* @throws MalformedObjectNameException
*/
- public ThreadPoolLoadMetricSource(LoadMetricSourceRegistration registration, String
pattern, MBeanServer server) throws MalformedObjectNameException
+ public
ThreadPoolLoadMetricSource(Collection<LoadMetric<MBeanQueryLoadContext>>
metrics, String pattern, MBeanServer server) throws MalformedObjectNameException
{
- super(registration, pattern, server);
+ super(metrics, pattern, server);
}
}
Modified:
trunk/mod_cluster/src/test/java/org/jboss/modcluster/load/DynamicLoadBalanceFactorProviderTestCase.java
===================================================================
---
trunk/mod_cluster/src/test/java/org/jboss/modcluster/load/DynamicLoadBalanceFactorProviderTestCase.java 2008-10-20
11:53:50 UTC (rev 1964)
+++
trunk/mod_cluster/src/test/java/org/jboss/modcluster/load/DynamicLoadBalanceFactorProviderTestCase.java 2008-10-21
15:20:30 UTC (rev 1965)
@@ -24,135 +24,138 @@
import java.util.Arrays;
import java.util.Collections;
-import junit.framework.TestCase;
-
import org.easymock.EasyMock;
import org.jboss.modcluster.load.impl.DynamicLoadBalanceFactorProvider;
+import org.jboss.modcluster.load.metric.LoadContext;
import org.jboss.modcluster.load.metric.LoadMetric;
import org.jboss.modcluster.load.metric.LoadMetricSource;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
/**
* @author Paul Ferraro
*
*/
-public class DynamicLoadBalanceFactorProviderTestCase extends TestCase
+@SuppressWarnings({ "unchecked", "boxing" })
+public class DynamicLoadBalanceFactorProviderTestCase
{
- private final LoadMetricSource source1 =
EasyMock.createStrictMock(LoadMetricSource.class);
- private final LoadMetricSource source2 =
EasyMock.createStrictMock(LoadMetricSource.class);
- private final LoadMetric metric1 = EasyMock.createStrictMock(LoadMetric.class);
- private final LoadMetric metric2 = EasyMock.createStrictMock(LoadMetric.class);
- private final LoadMetric metric3 = EasyMock.createStrictMock(LoadMetric.class);
+ private final LoadMetricSource<LoadContext> source1 =
EasyMock.createStrictMock(LoadMetricSource.class);
+ private final LoadMetricSource<LoadContext> source2 =
EasyMock.createStrictMock(LoadMetricSource.class);
- private final DynamicLoadBalanceFactorProvider provider = new
DynamicLoadBalanceFactorProvider();
+ private final DynamicLoadBalanceFactorProvider provider = new
DynamicLoadBalanceFactorProvider(Arrays.asList(this.source1, this.source2));
/**
* @{inheritDoc}
* @see junit.framework.TestCase#setUp()
*/
- @Override
- protected void setUp() throws Exception
+ @Before
+ public void init() throws Exception
{
- this.provider.add(this.source1);
- this.provider.add(this.source2);
this.provider.setHistory(2);
}
-
- public void testGetLoadBalanceFactor() throws Exception
+ @Test
+ public void getLoadBalanceFactor() throws Exception
{
- EasyMock.expect(this.source1.getMetrics()).andReturn(Arrays.asList(this.metric1,
this.metric2));
+ LoadContext context1 = EasyMock.createStrictMock(LoadContext.class);
+ LoadMetric<LoadContext> metric1 =
EasyMock.createStrictMock(LoadMetric.class);
+ LoadMetric<LoadContext> metric2 =
EasyMock.createStrictMock(LoadMetric.class);
+ LoadMetric<LoadContext> metric3 =
EasyMock.createStrictMock(LoadMetric.class);
- EasyMock.expect(this.metric1.getWeight()).andReturn(1);
- EasyMock.expect(this.metric2.getWeight()).andReturn(2);
+ EasyMock.expect(this.source1.getMetrics()).andReturn(Arrays.asList(metric1,
metric2));
- this.source1.prepare();
+ EasyMock.expect(metric1.getWeight()).andReturn(1);
+ EasyMock.expect(metric2.getWeight()).andReturn(2);
+
+ EasyMock.expect(this.source1.createContext()).andReturn(context1);
- EasyMock.expect(this.metric1.getLoad()).andReturn(0.2);
- EasyMock.expect(this.metric1.getCapacity()).andReturn(1d);
- EasyMock.expect(this.metric1.getWeight()).andReturn(1);
+ EasyMock.expect(metric1.getLoad(context1)).andReturn(0.2);
+ EasyMock.expect(metric1.getCapacity()).andReturn(1d);
+ EasyMock.expect(metric1.getWeight()).andReturn(1);
- EasyMock.expect(this.metric2.getLoad()).andReturn(400d);
- EasyMock.expect(this.metric2.getCapacity()).andReturn(1000d);
- EasyMock.expect(this.metric2.getWeight()).andReturn(2);
+ EasyMock.expect(metric2.getLoad(context1)).andReturn(400d);
+ EasyMock.expect(metric2.getCapacity()).andReturn(1000d);
+ EasyMock.expect(metric2.getWeight()).andReturn(2);
- this.source1.cleanup();
+ context1.close();
-
EasyMock.expect(this.source2.getMetrics()).andReturn(Collections.singleton(this.metric3));
+
EasyMock.expect(this.source2.getMetrics()).andReturn(Collections.singleton(metric3));
- EasyMock.expect(this.metric3.getWeight()).andReturn(0);
+ EasyMock.expect(metric3.getWeight()).andReturn(0);
- EasyMock.replay(this.source1, this.source2, this.metric1, this.metric2,
this.metric3);
+ EasyMock.replay(this.source1, this.source2, context1, metric1, metric2, metric3);
int loadBalanceFactor = this.provider.getLoadBalanceFactor();
- EasyMock.verify(this.source1, this.source2, this.metric1, this.metric2,
this.metric3);
+ EasyMock.verify(this.source1, this.source2, context1, metric1, metric2, metric3);
- assertEquals(67, loadBalanceFactor);
+ Assert.assertEquals(67, loadBalanceFactor);
- EasyMock.reset(this.source1, this.source2, this.metric1, this.metric2,
this.metric3);
+ EasyMock.reset(this.source1, this.source2, context1, metric1, metric2, metric3);
// Test time-decay function
- EasyMock.expect(this.source1.getMetrics()).andReturn(Arrays.asList(this.metric1,
this.metric2));
+ EasyMock.expect(this.source1.getMetrics()).andReturn(Arrays.asList(metric1,
metric2));
- EasyMock.expect(this.metric1.getWeight()).andReturn(1);
- EasyMock.expect(this.metric2.getWeight()).andReturn(2);
+ EasyMock.expect(metric1.getWeight()).andReturn(1);
+ EasyMock.expect(metric2.getWeight()).andReturn(2);
- this.source1.prepare();
+ EasyMock.expect(this.source1.createContext()).andReturn(context1);
- EasyMock.expect(this.metric1.getLoad()).andReturn(0.4);
- EasyMock.expect(this.metric1.getCapacity()).andReturn(1d);
- EasyMock.expect(this.metric1.getWeight()).andReturn(1);
+ EasyMock.expect(metric1.getLoad(context1)).andReturn(0.4);
+ EasyMock.expect(metric1.getCapacity()).andReturn(1d);
+ EasyMock.expect(metric1.getWeight()).andReturn(1);
- EasyMock.expect(this.metric2.getLoad()).andReturn(600d);
- EasyMock.expect(this.metric2.getCapacity()).andReturn(1000d);
- EasyMock.expect(this.metric2.getWeight()).andReturn(2);
+ EasyMock.expect(metric2.getLoad(context1)).andReturn(600d);
+ EasyMock.expect(metric2.getCapacity()).andReturn(1000d);
+ EasyMock.expect(metric2.getWeight()).andReturn(2);
- this.source1.cleanup();
+ context1.close();
-
EasyMock.expect(this.source2.getMetrics()).andReturn(Collections.singleton(this.metric3));
+
EasyMock.expect(this.source2.getMetrics()).andReturn(Collections.singleton(metric3));
- EasyMock.expect(this.metric3.getWeight()).andReturn(0);
+ EasyMock.expect(metric3.getWeight()).andReturn(0);
- EasyMock.replay(this.source1, this.source2, this.metric1, this.metric2,
this.metric3);
+ EasyMock.replay(this.source1, this.source2, context1, metric1, metric2, metric3);
loadBalanceFactor = this.provider.getLoadBalanceFactor();
- EasyMock.verify(this.source1, this.source2, this.metric1, this.metric2,
this.metric3);
+ EasyMock.verify(this.source1, this.source2, context1, metric1, metric2, metric3);
- assertEquals(53, loadBalanceFactor);
+ Assert.assertEquals(53, loadBalanceFactor);
- EasyMock.reset(this.source1, this.source2, this.metric1, this.metric2,
this.metric3);
+ EasyMock.reset(this.source1, this.source2, context1, metric1, metric2, metric3);
//Test decay queue overflow
- EasyMock.expect(this.source1.getMetrics()).andReturn(Arrays.asList(this.metric1,
this.metric2));
+ EasyMock.expect(this.source1.getMetrics()).andReturn(Arrays.asList(metric1,
metric2));
- EasyMock.expect(this.metric1.getWeight()).andReturn(1);
- EasyMock.expect(this.metric2.getWeight()).andReturn(2);
+ EasyMock.expect(metric1.getWeight()).andReturn(1);
+ EasyMock.expect(metric2.getWeight()).andReturn(2);
- this.source1.prepare();
+ EasyMock.expect(this.source1.createContext()).andReturn(context1);
- EasyMock.expect(this.metric1.getLoad()).andReturn(0.3);
- EasyMock.expect(this.metric1.getCapacity()).andReturn(1d);
- EasyMock.expect(this.metric1.getWeight()).andReturn(1);
+ EasyMock.expect(metric1.getLoad(context1)).andReturn(0.3);
+ EasyMock.expect(metric1.getCapacity()).andReturn(1d);
+ EasyMock.expect(metric1.getWeight()).andReturn(1);
- EasyMock.expect(this.metric2.getLoad()).andReturn(300d);
- EasyMock.expect(this.metric2.getCapacity()).andReturn(1000d);
- EasyMock.expect(this.metric2.getWeight()).andReturn(2);
+ EasyMock.expect(metric2.getLoad(context1)).andReturn(300d);
+ EasyMock.expect(metric2.getCapacity()).andReturn(1000d);
+ EasyMock.expect(metric2.getWeight()).andReturn(2);
- this.source1.cleanup();
+ context1.close();
-
EasyMock.expect(this.source2.getMetrics()).andReturn(Collections.singleton(this.metric3));
+
EasyMock.expect(this.source2.getMetrics()).andReturn(Collections.singleton(metric3));
- EasyMock.expect(this.metric3.getWeight()).andReturn(0);
+ EasyMock.expect(metric3.getWeight()).andReturn(0);
- EasyMock.replay(this.source1, this.source2, this.metric1, this.metric2,
this.metric3);
+ EasyMock.replay(this.source1, this.source2, context1, metric1, metric2, metric3);
loadBalanceFactor = this.provider.getLoadBalanceFactor();
- EasyMock.verify(this.source1, this.source2, this.metric1, this.metric2,
this.metric3);
+ EasyMock.verify(this.source1, this.source2, context1, metric1, metric2, metric3);
- assertEquals(62, loadBalanceFactor);
+ Assert.assertEquals(62, loadBalanceFactor);
- EasyMock.reset(this.source1, this.source2, this.metric1, this.metric2,
this.metric3);
+ EasyMock.reset(this.source1, this.source2, context1, metric1, metric2, metric3);
}
}
Modified:
trunk/mod_cluster/src/test/java/org/jboss/modcluster/load/StaticLoadBalanceFactorProviderTestCase.java
===================================================================
---
trunk/mod_cluster/src/test/java/org/jboss/modcluster/load/StaticLoadBalanceFactorProviderTestCase.java 2008-10-20
11:53:50 UTC (rev 1964)
+++
trunk/mod_cluster/src/test/java/org/jboss/modcluster/load/StaticLoadBalanceFactorProviderTestCase.java 2008-10-21
15:20:30 UTC (rev 1965)
@@ -22,27 +22,28 @@
package org.jboss.modcluster.load;
import org.jboss.modcluster.load.impl.StaticLoadBalanceFactorProvider;
+import org.junit.Assert;
+import org.junit.Test;
-import junit.framework.TestCase;
-
/**
* @author Paul Ferraro
*
*/
-public class StaticLoadBalanceFactorProviderTestCase extends TestCase
+public class StaticLoadBalanceFactorProviderTestCase
{
private StaticLoadBalanceFactorProvider provider = new
StaticLoadBalanceFactorProvider();
-
- public void testGetLoadBalanceFactor()
+
+ @Test
+ public void getLoadBalanceFactor()
{
int factor = this.provider.getLoadBalanceFactor();
- assertEquals(1, factor);
+ Assert.assertEquals(1, factor);
this.provider.setLoadBalanceFactor(3);
factor = this.provider.getLoadBalanceFactor();
- assertEquals(3, factor);
+ Assert.assertEquals(3, factor);
}
}
Modified:
trunk/mod_cluster/src/test/java/org/jboss/modcluster/load/metric/ActiveSessionsLoadMetricTestCase.java
===================================================================
---
trunk/mod_cluster/src/test/java/org/jboss/modcluster/load/metric/ActiveSessionsLoadMetricTestCase.java 2008-10-20
11:53:50 UTC (rev 1964)
+++
trunk/mod_cluster/src/test/java/org/jboss/modcluster/load/metric/ActiveSessionsLoadMetricTestCase.java 2008-10-21
15:20:30 UTC (rev 1965)
@@ -21,113 +21,16 @@
*/
package org.jboss.modcluster.load.metric;
-import java.util.Arrays;
-import java.util.Collection;
-import java.util.Comparator;
-import java.util.Set;
-import java.util.TreeSet;
-
-import javax.management.MBeanServer;
-import javax.management.ObjectName;
-
-import junit.framework.TestCase;
-
-import org.easymock.Capture;
-import org.easymock.EasyMock;
-import org.jboss.modcluster.load.metric.LoadMetric;
-import org.jboss.modcluster.load.metric.LoadMetricSource;
-import org.jboss.modcluster.load.metric.LoadMetricSourceRegistration;
import org.jboss.modcluster.load.metric.impl.ActiveSessionsLoadMetric;
-import org.jboss.modcluster.load.metric.impl.SessionLoadMetricSource;
/**
* @author Paul Ferraro
*
*/
-public class ActiveSessionsLoadMetricTestCase extends TestCase
+public class ActiveSessionsLoadMetricTestCase extends MBeanAttributeLoadMetricTestCase
{
- private final MBeanServer server = EasyMock.createStrictMock(MBeanServer.class);
-
- private LoadMetricSource source;
- private LoadMetric metric;
-
- /**
- * @{inheritDoc}
- * @see junit.framework.TestCase#setUp()
- */
- @Override
- protected void setUp() throws Exception
+ public ActiveSessionsLoadMetricTestCase()
{
- LoadMetricSourceRegistration registration =
EasyMock.createStrictMock(LoadMetricSourceRegistration.class);
-
- Capture<LoadMetricSource> captured = new Capture<LoadMetricSource>();
-
- registration.add(EasyMock.capture(captured));
-
- EasyMock.replay(registration, this.server);
-
- SessionLoadMetricSource source = new SessionLoadMetricSource(registration,
this.server);
-
- EasyMock.verify(registration, this.server);
-
- assertSame(source, captured.getValue());
-
- EasyMock.reset(registration, this.server);
-
- this.source = source;
- this.metric = new ActiveSessionsLoadMetric(source);
-
- Collection<? extends LoadMetric> metrics = this.source.getMetrics();
-
- assertNotNull(metrics);
- assertEquals(1, metrics.size());
- assertSame(this.metric, metrics.iterator().next());
+ super(new ActiveSessionsLoadMetric(), ActiveSessionsLoadMetric.DEFAULT_ATTRIBUTE);
}
-
- public void testGetLoad() throws Exception
- {
- ObjectName pattern = ObjectName.getInstance("jboss.web:type=Manager,*");
- ObjectName name1 =
ObjectName.getInstance("jboss.web:type=Manager,path=/app1,host=localhost");
- ObjectName name2 =
ObjectName.getInstance("jboss.web:type=Manager,path=/app2,host=localhost");
-
- Comparator<ObjectName> comparator = new Comparator<ObjectName>()
- {
- public int compare(ObjectName name1, ObjectName name2)
- {
- return name1.getCanonicalName().compareTo(name2.getCanonicalName());
- }
- };
-
- Set<ObjectName> names = new TreeSet<ObjectName>(comparator);
- names.addAll(Arrays.asList(name1, name2));
-
- EasyMock.expect(this.server.queryNames(pattern, null)).andReturn(names);
-
- EasyMock.replay(this.server);
-
- this.source.prepare();
-
- EasyMock.verify(this.server);
- EasyMock.reset(this.server);
-
- EasyMock.expect(this.server.getAttribute(name1,
"activeSessions")).andReturn(1);
- EasyMock.expect(this.server.getAttribute(name2,
"activeSessions")).andReturn(2);
-
- EasyMock.replay(this.server);
-
- double load = this.metric.getLoad();
-
- EasyMock.verify(this.server);
-
- assertEquals(3.0, load);
-
- EasyMock.reset(this.server);
-
- EasyMock.replay(this.server);
-
- this.source.cleanup();
-
- EasyMock.verify(this.server);
- EasyMock.reset(this.server);
- }
}
Modified:
trunk/mod_cluster/src/test/java/org/jboss/modcluster/load/metric/AverageSystemLoadMetricTestCase.java
===================================================================
---
trunk/mod_cluster/src/test/java/org/jboss/modcluster/load/metric/AverageSystemLoadMetricTestCase.java 2008-10-20
11:53:50 UTC (rev 1964)
+++
trunk/mod_cluster/src/test/java/org/jboss/modcluster/load/metric/AverageSystemLoadMetricTestCase.java 2008-10-21
15:20:30 UTC (rev 1965)
@@ -22,130 +22,66 @@
package org.jboss.modcluster.load.metric;
import java.lang.management.ManagementFactory;
-import java.lang.management.OperatingSystemMXBean;
-import java.util.Collection;
-import javax.management.MBeanAttributeInfo;
-import javax.management.MBeanInfo;
+import javax.management.AttributeNotFoundException;
import javax.management.MBeanServer;
+import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
-import junit.framework.TestCase;
-
-import org.easymock.Capture;
import org.easymock.EasyMock;
-import org.jboss.modcluster.load.metric.LoadMetric;
-import org.jboss.modcluster.load.metric.LoadMetricSource;
-import org.jboss.modcluster.load.metric.LoadMetricSourceRegistration;
import org.jboss.modcluster.load.metric.impl.AverageSystemLoadMetric;
-import org.jboss.modcluster.load.metric.impl.OperatingSystemLoadMetricSource;
+import org.jboss.modcluster.load.metric.impl.MBeanLoadContext;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
/**
* @author Paul Ferraro
*
*/
-public class AverageSystemLoadMetricTestCase extends TestCase
+@SuppressWarnings("boxing")
+public class AverageSystemLoadMetricTestCase
{
private final MBeanServer server = EasyMock.createStrictMock(MBeanServer.class);
- private LoadMetricSource source;
- private LoadMetric metric;
+ private LoadMetric<MBeanLoadContext> metric = new AverageSystemLoadMetric();
- /**
- * @{inheritDoc}
- * @see junit.framework.TestCase#setUp()
- */
- @Override
- protected void setUp() throws Exception
+ private ObjectName name;
+ private MBeanLoadContext context;
+
+ @Before
+ public void prepare() throws MalformedObjectNameException
{
- LoadMetricSourceRegistration registration =
EasyMock.createStrictMock(LoadMetricSourceRegistration.class);
+ this.name =
ObjectName.getInstance(ManagementFactory.OPERATING_SYSTEM_MXBEAN_NAME);
+ this.context = new MBeanLoadContext(this.server, this.name);
+ }
+
+ @Test
+ public void getLoad() throws Exception
+ {
+ EasyMock.expect(this.server.getAttribute(this.name,
AverageSystemLoadMetric.SYSTEM_LOAD_AVERAGE)).andReturn(0.1);
- Capture<LoadMetricSource> captured = new Capture<LoadMetricSource>();
-
- registration.add(EasyMock.capture(captured));
-
- EasyMock.replay(registration, this.server);
-
- OperatingSystemLoadMetricSource source = new
OperatingSystemLoadMetricSource(registration, this.server);
-
- EasyMock.verify(registration, this.server);
-
- assertSame(source, captured.getValue());
-
- EasyMock.reset(registration, this.server);
-
- this.source = source;
-
- ObjectName name =
ObjectName.getInstance(ManagementFactory.OPERATING_SYSTEM_MXBEAN_NAME);
-
- // Test Java 1.5 case
- MBeanInfo info = new MBeanInfo(OperatingSystemMXBean.class.getName(), null, null,
null, null, null);
-
- EasyMock.expect(this.server.getMBeanInfo(name)).andReturn(info);
-
EasyMock.replay(this.server);
- this.metric = new AverageSystemLoadMetric(source);
+ double load = this.metric.getLoad(this.context);
EasyMock.verify(this.server);
- Collection<? extends LoadMetric> metrics = this.source.getMetrics();
+ Assert.assertEquals(0.1, load, 0.0);
- assertNotNull(metrics);
- assertTrue(metrics.isEmpty());
-
EasyMock.reset(this.server);
- // Test Java 1.6 case
- MBeanAttributeInfo attribute = new
MBeanAttributeInfo("SystemLoadAverage", Double.TYPE.getName(), null, true,
false, false);
-
- info = new MBeanInfo(OperatingSystemMXBean.class.getName(), "", new
MBeanAttributeInfo[] { attribute }, null, null, null);
- EasyMock.expect(this.server.getMBeanInfo(name)).andReturn(info);
+ // Test Java 1.5 behavior
+ EasyMock.expect(this.server.getAttribute(this.name,
AverageSystemLoadMetric.SYSTEM_LOAD_AVERAGE)).andThrow(new AttributeNotFoundException());
EasyMock.replay(this.server);
- this.metric = new AverageSystemLoadMetric(source);
+ load = this.metric.getLoad(this.context);
EasyMock.verify(this.server);
- metrics = this.source.getMetrics();
-
- assertNotNull(metrics);
- assertEquals(1, metrics.size());
- assertSame(this.metric, metrics.iterator().next());
-
- EasyMock.reset(this.server);
+ Assert.assertEquals(0.0, load, 0.0);
+ Assert.assertEquals(0, this.metric.getWeight());
}
-
- public void testGetLoad() throws Exception
- {
- ObjectName name =
ObjectName.getInstance(ManagementFactory.OPERATING_SYSTEM_MXBEAN_NAME);
-
- EasyMock.replay(this.server);
-
- this.source.prepare();
-
- EasyMock.verify(this.server);
- EasyMock.reset(this.server);
-
- EasyMock.expect(this.server.getAttribute(name,
"SystemLoadAverage")).andReturn(0.1);
-
- EasyMock.replay(this.server);
-
- double load = this.metric.getLoad();
-
- EasyMock.verify(this.server);
-
- assertEquals(0.1, load);
-
- EasyMock.reset(this.server);
-
- EasyMock.replay(this.server);
-
- this.source.cleanup();
-
- EasyMock.verify(this.server);
- EasyMock.reset(this.server);
- }
}
Modified:
trunk/mod_cluster/src/test/java/org/jboss/modcluster/load/metric/BusyConnectorsLoadMetricTestCase.java
===================================================================
---
trunk/mod_cluster/src/test/java/org/jboss/modcluster/load/metric/BusyConnectorsLoadMetricTestCase.java 2008-10-20
11:53:50 UTC (rev 1964)
+++
trunk/mod_cluster/src/test/java/org/jboss/modcluster/load/metric/BusyConnectorsLoadMetricTestCase.java 2008-10-21
15:20:30 UTC (rev 1965)
@@ -21,116 +21,16 @@
*/
package org.jboss.modcluster.load.metric;
-import java.util.Arrays;
-import java.util.Collection;
-import java.util.Comparator;
-import java.util.Set;
-import java.util.TreeSet;
-
-import javax.management.MBeanServer;
-import javax.management.ObjectName;
-
-import junit.framework.TestCase;
-
-import org.easymock.Capture;
-import org.easymock.EasyMock;
-import org.jboss.modcluster.load.metric.LoadMetric;
-import org.jboss.modcluster.load.metric.LoadMetricSource;
-import org.jboss.modcluster.load.metric.LoadMetricSourceRegistration;
import org.jboss.modcluster.load.metric.impl.BusyConnectorsLoadMetric;
-import org.jboss.modcluster.load.metric.impl.ThreadPoolLoadMetricSource;
/**
* @author Paul Ferraro
*
*/
-public class BusyConnectorsLoadMetricTestCase extends TestCase
+public class BusyConnectorsLoadMetricTestCase extends
MBeanAttributeRatioLoadMetricTestCase
{
- private final MBeanServer server = EasyMock.createStrictMock(MBeanServer.class);
-
- private LoadMetricSource source;
- private LoadMetric metric;
-
- /**
- * @{inheritDoc}
- * @see junit.framework.TestCase#setUp()
- */
- @Override
- protected void setUp() throws Exception
+ public BusyConnectorsLoadMetricTestCase()
{
- LoadMetricSourceRegistration registration =
EasyMock.createStrictMock(LoadMetricSourceRegistration.class);
-
- Capture<LoadMetricSource> captured = new Capture<LoadMetricSource>();
-
- registration.add(EasyMock.capture(captured));
-
- EasyMock.replay(registration, this.server);
-
- ThreadPoolLoadMetricSource source = new ThreadPoolLoadMetricSource(registration,
this.server);
-
- EasyMock.verify(registration, this.server);
-
- assertSame(source, captured.getValue());
-
- EasyMock.reset(registration, this.server);
-
- this.source = source;
- this.metric = new BusyConnectorsLoadMetric(source);
-
- Collection<? extends LoadMetric> metrics = this.source.getMetrics();
-
- assertNotNull(metrics);
- assertEquals(1, metrics.size());
- assertEquals(this.metric, metrics.iterator().next());
+ super(new BusyConnectorsLoadMetric(),
BusyConnectorsLoadMetric.DEFAULT_DIVIDEND_ATTRIBUTE,
BusyConnectorsLoadMetric.DEFAULT_DIVISOR_ATTRIBUTE);
}
-
- public void testGetLoad() throws Exception
- {
- ObjectName pattern =
ObjectName.getInstance("jboss.web:type=ThreadPool,*");
- ObjectName name1 =
ObjectName.getInstance("jboss.web:type=ThreadPool,name=http-8080");
- ObjectName name2 =
ObjectName.getInstance("jboss.web:type=ThreadPool,name=jk-8009");
-
- Comparator<ObjectName> comparator = new Comparator<ObjectName>()
- {
- public int compare(ObjectName name1, ObjectName name2)
- {
- return name1.getCanonicalName().compareTo(name2.getCanonicalName());
- }
- };
-
- Set<ObjectName> names = new TreeSet<ObjectName>(comparator);
- names.addAll(Arrays.asList(name1, name2));
-
- EasyMock.expect(this.server.queryNames(pattern, null)).andReturn(names);
-
- EasyMock.replay(this.server);
-
- this.source.prepare();
-
- EasyMock.verify(this.server);
- EasyMock.reset(this.server);
-
- EasyMock.expect(this.server.getAttribute(name1,
"currentThreadsBusy")).andReturn(1);
- EasyMock.expect(this.server.getAttribute(name2,
"currentThreadsBusy")).andReturn(2);
-
- EasyMock.expect(this.server.getAttribute(name1,
"maxThreads")).andReturn(2);
- EasyMock.expect(this.server.getAttribute(name2,
"maxThreads")).andReturn(2);
-
- EasyMock.replay(this.server);
-
- double load = this.metric.getLoad();
-
- EasyMock.verify(this.server);
-
- assertEquals(0.75, load);
-
- EasyMock.reset(this.server);
-
- EasyMock.replay(this.server);
-
- this.source.cleanup();
-
- EasyMock.verify(this.server);
- EasyMock.reset(this.server);
- }
}
Added:
trunk/mod_cluster/src/test/java/org/jboss/modcluster/load/metric/ConnectionPoolLoadMetricSourceTestCase.java
===================================================================
---
trunk/mod_cluster/src/test/java/org/jboss/modcluster/load/metric/ConnectionPoolLoadMetricSourceTestCase.java
(rev 0)
+++
trunk/mod_cluster/src/test/java/org/jboss/modcluster/load/metric/ConnectionPoolLoadMetricSourceTestCase.java 2008-10-21
15:20:30 UTC (rev 1965)
@@ -0,0 +1,49 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+package org.jboss.modcluster.load.metric;
+
+import java.util.Collection;
+
+import javax.management.MBeanServer;
+import javax.management.MalformedObjectNameException;
+
+import org.jboss.modcluster.load.metric.impl.ConnectionPoolLoadMetricSource;
+import org.jboss.modcluster.load.metric.impl.MBeanQueryLoadContext;
+import org.jboss.modcluster.load.metric.impl.MBeanQueryLoadMetricSource;
+
+/**
+ * @author Paul Ferraro
+ *
+ */
+public class ConnectionPoolLoadMetricSourceTestCase extends
MBeanQueryLoadMetricSourceTestCase
+{
+ public ConnectionPoolLoadMetricSourceTestCase() throws MalformedObjectNameException
+ {
+ super(ConnectionPoolLoadMetricSource.DEFAULT_PATTERN);
+ }
+
+ @Override
+ protected MBeanQueryLoadMetricSource
createSource(Collection<LoadMetric<MBeanQueryLoadContext>> metrics,
MBeanServer server) throws MalformedObjectNameException
+ {
+ return new ConnectionPoolLoadMetricSource(metrics, server);
+ }
+}
Modified:
trunk/mod_cluster/src/test/java/org/jboss/modcluster/load/metric/ConnectionPoolUsageLoadMetricTestCase.java
===================================================================
---
trunk/mod_cluster/src/test/java/org/jboss/modcluster/load/metric/ConnectionPoolUsageLoadMetricTestCase.java 2008-10-20
11:53:50 UTC (rev 1964)
+++
trunk/mod_cluster/src/test/java/org/jboss/modcluster/load/metric/ConnectionPoolUsageLoadMetricTestCase.java 2008-10-21
15:20:30 UTC (rev 1965)
@@ -21,116 +21,17 @@
*/
package org.jboss.modcluster.load.metric;
-import java.util.Arrays;
-import java.util.Collection;
-import java.util.Comparator;
-import java.util.Set;
-import java.util.TreeSet;
-
-import javax.management.MBeanServer;
-import javax.management.ObjectName;
-
-import junit.framework.TestCase;
-
-import org.easymock.Capture;
-import org.easymock.EasyMock;
-import org.jboss.modcluster.load.metric.LoadMetric;
-import org.jboss.modcluster.load.metric.LoadMetricSource;
-import org.jboss.modcluster.load.metric.LoadMetricSourceRegistration;
-import org.jboss.modcluster.load.metric.impl.ConnectionPoolLoadMetricSource;
import org.jboss.modcluster.load.metric.impl.ConnectionPoolUsageLoadMetric;
+
/**
* @author Paul Ferraro
*
*/
-public class ConnectionPoolUsageLoadMetricTestCase extends TestCase
+public class ConnectionPoolUsageLoadMetricTestCase extends
MBeanAttributeRatioLoadMetricTestCase
{
- private final MBeanServer server = EasyMock.createStrictMock(MBeanServer.class);
-
- private LoadMetricSource source;
- private LoadMetric metric;
-
- /**
- * @{inheritDoc}
- * @see junit.framework.TestCase#setUp()
- */
- @Override
- protected void setUp() throws Exception
+ public ConnectionPoolUsageLoadMetricTestCase()
{
- LoadMetricSourceRegistration registration =
EasyMock.createStrictMock(LoadMetricSourceRegistration.class);
-
- Capture<LoadMetricSource> captured = new Capture<LoadMetricSource>();
-
- registration.add(EasyMock.capture(captured));
-
- EasyMock.replay(registration, this.server);
-
- ConnectionPoolLoadMetricSource source = new
ConnectionPoolLoadMetricSource(registration, this.server);
-
- EasyMock.verify(registration, this.server);
-
- assertSame(source, captured.getValue());
-
- EasyMock.reset(registration, this.server);
-
- this.source = source;
- this.metric = new ConnectionPoolUsageLoadMetric(source);
-
- Collection<? extends LoadMetric> metrics = this.source.getMetrics();
-
- assertNotNull(metrics);
- assertEquals(1, metrics.size());
- assertEquals(this.metric, metrics.iterator().next());
+ super(new ConnectionPoolUsageLoadMetric(),
ConnectionPoolUsageLoadMetric.DEFAULT_DIVIDEND_ATTRIBUTE,
ConnectionPoolUsageLoadMetric.DEFAULT_DIVISOR_ATTRIBUTE);
}
-
- public void testGetLoad() throws Exception
- {
- ObjectName pattern =
ObjectName.getInstance("jboss.jca:service=ManagedConnectionPool,*");
- ObjectName name1 =
ObjectName.getInstance("jboss.jca:service=ManagedConnectionPool,name=DS1");
- ObjectName name2 =
ObjectName.getInstance("jboss.jca:service=ManagedConnectionPool,name=DS2");
-
- Comparator<ObjectName> comparator = new Comparator<ObjectName>()
- {
- public int compare(ObjectName name1, ObjectName name2)
- {
- return name1.getCanonicalName().compareTo(name2.getCanonicalName());
- }
- };
-
- Set<ObjectName> names = new TreeSet<ObjectName>(comparator);
- names.addAll(Arrays.asList(name1, name2));
-
- EasyMock.expect(this.server.queryNames(pattern, null)).andReturn(names);
-
- EasyMock.replay(this.server);
-
- this.source.prepare();
-
- EasyMock.verify(this.server);
- EasyMock.reset(this.server);
-
- EasyMock.expect(this.server.getAttribute(name1,
"InUseConnectionCount")).andReturn(10);
- EasyMock.expect(this.server.getAttribute(name2,
"InUseConnectionCount")).andReturn(20);
-
- EasyMock.expect(this.server.getAttribute(name1,
"MaxSize")).andReturn(20);
- EasyMock.expect(this.server.getAttribute(name2,
"MaxSize")).andReturn(20);
-
- EasyMock.replay(this.server);
-
- double load = this.metric.getLoad();
-
- EasyMock.verify(this.server);
-
- assertEquals(0.75, load);
-
- EasyMock.reset(this.server);
-
- EasyMock.replay(this.server);
-
- this.source.cleanup();
-
- EasyMock.verify(this.server);
- EasyMock.reset(this.server);
- }
}
Modified:
trunk/mod_cluster/src/test/java/org/jboss/modcluster/load/metric/HeapMemoryUsageLoadMetricTestCase.java
===================================================================
---
trunk/mod_cluster/src/test/java/org/jboss/modcluster/load/metric/HeapMemoryUsageLoadMetricTestCase.java 2008-10-20
11:53:50 UTC (rev 1964)
+++
trunk/mod_cluster/src/test/java/org/jboss/modcluster/load/metric/HeapMemoryUsageLoadMetricTestCase.java 2008-10-21
15:20:30 UTC (rev 1965)
@@ -23,69 +23,25 @@
import java.lang.management.MemoryMXBean;
import java.lang.management.MemoryUsage;
-import java.util.Collection;
-import junit.framework.TestCase;
-
-import org.easymock.Capture;
import org.easymock.EasyMock;
-import org.jboss.modcluster.load.metric.LoadMetric;
-import org.jboss.modcluster.load.metric.LoadMetricSource;
-import org.jboss.modcluster.load.metric.LoadMetricSourceRegistration;
import org.jboss.modcluster.load.metric.impl.HeapMemoryUsageLoadMetric;
+import org.junit.Assert;
+import org.junit.Test;
/**
* @author Paul Ferraro
*
*/
-public class HeapMemoryUsageLoadMetricTestCase extends TestCase
+public class HeapMemoryUsageLoadMetricTestCase
{
- private final MemoryMXBean bean = EasyMock.createStrictMock(MemoryMXBean.class);
-
- private LoadMetricSource source;
- private LoadMetric metric;
-
- /**
- * @{inheritDoc}
- * @see junit.framework.TestCase#setUp()
- */
- @Override
- protected void setUp() throws Exception
- {
- LoadMetricSourceRegistration registration =
EasyMock.createStrictMock(LoadMetricSourceRegistration.class);
-
- Capture<LoadMetricSource> captured = new Capture<LoadMetricSource>();
-
- registration.add(EasyMock.capture(captured));
-
- EasyMock.replay(registration, this.bean);
-
- HeapMemoryUsageLoadMetric source = new HeapMemoryUsageLoadMetric(registration,
this.bean);
-
- EasyMock.verify(registration, this.bean);
+ private MemoryMXBean bean = EasyMock.createStrictMock(MemoryMXBean.class);
+ private HeapMemoryUsageLoadMetric metric = new HeapMemoryUsageLoadMetric(this.bean);
- assertSame(source, captured.getValue());
-
- EasyMock.reset(registration, this.bean);
-
- this.source = source;
- this.metric = source;
-
- Collection<? extends LoadMetric> metrics = this.source.getMetrics();
-
- assertNotNull(metrics);
- assertEquals(1, metrics.size());
- assertSame(this.metric, metrics.iterator().next());
- }
-
- public void testGetLoad() throws Exception
+ @Test
+ public void getLoad() throws Exception
{
- EasyMock.replay(this.bean);
-
- this.source.prepare();
-
- EasyMock.verify(this.bean);
- EasyMock.reset(this.bean);
+ LoadContext context = EasyMock.createMock(LoadContext.class);
MemoryUsage usage = new MemoryUsage(0, 1000, 2000, 5000);
@@ -93,19 +49,20 @@
EasyMock.replay(this.bean);
- double load = this.metric.getLoad();
+ double load = this.metric.getLoad(context);
EasyMock.verify(this.bean);
- assertEquals(0.2, load);
+ Assert.assertEquals(0.2, load, 0.0);
EasyMock.reset(this.bean);
+ }
+
+ @Test
+ public void createContext()
+ {
+ LoadContext context = this.metric.createContext();
- EasyMock.replay(this.bean);
-
- this.source.cleanup();
-
- EasyMock.verify(this.bean);
- EasyMock.reset(this.bean);
+ Assert.assertNotNull(context);
}
}
Added:
trunk/mod_cluster/src/test/java/org/jboss/modcluster/load/metric/MBeanAttributeLoadMetricTestCase.java
===================================================================
---
trunk/mod_cluster/src/test/java/org/jboss/modcluster/load/metric/MBeanAttributeLoadMetricTestCase.java
(rev 0)
+++
trunk/mod_cluster/src/test/java/org/jboss/modcluster/load/metric/MBeanAttributeLoadMetricTestCase.java 2008-10-21
15:20:30 UTC (rev 1965)
@@ -0,0 +1,102 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+package org.jboss.modcluster.load.metric;
+
+import java.util.Arrays;
+import java.util.LinkedHashSet;
+
+import javax.management.MBeanServer;
+import javax.management.MalformedObjectNameException;
+import javax.management.ObjectName;
+
+import org.easymock.EasyMock;
+import org.jboss.modcluster.load.metric.impl.MBeanAttributeLoadMetric;
+import org.jboss.modcluster.load.metric.impl.MBeanQueryLoadContext;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * @author Paul Ferraro
+ *
+ */
+@SuppressWarnings("boxing")
+public class MBeanAttributeLoadMetricTestCase
+{
+ MBeanServer server = EasyMock.createStrictMock(MBeanServer.class);
+ ObjectName name1;
+ ObjectName name2;
+ MBeanQueryLoadContext context;
+
+ private String attribute;
+ LoadMetric<MBeanQueryLoadContext> metric;
+
+ public MBeanAttributeLoadMetricTestCase()
+ {
+ this("attribute");
+ }
+
+ private MBeanAttributeLoadMetricTestCase(String attribute)
+ {
+ this(new MBeanAttributeLoadMetric(attribute), attribute);
+ }
+
+ protected MBeanAttributeLoadMetricTestCase(LoadMetric<MBeanQueryLoadContext>
metric, String attribute)
+ {
+ this.metric = metric;
+ this.attribute = attribute;
+ }
+
+ @Before
+ public void prepare() throws MalformedObjectNameException
+ {
+ ObjectName pattern = ObjectName.getInstance("domain:*");
+ this.name1 = ObjectName.getInstance("domain:name=test1");
+ this.name2 = ObjectName.getInstance("domain:name=test2");
+
+ EasyMock.expect(this.server.queryNames(pattern, null)).andReturn(new
LinkedHashSet<ObjectName>(Arrays.asList(this.name1, this.name2)));
+
+ EasyMock.replay(this.server);
+
+ this.context = new MBeanQueryLoadContext(this.server, pattern);
+
+ EasyMock.verify(this.server);
+ EasyMock.reset(this.server);
+ }
+
+ @Test
+ public void getLoad() throws Exception
+ {
+ EasyMock.expect(this.server.getAttribute(this.name1,
this.attribute)).andReturn(1);
+ EasyMock.expect(this.server.getAttribute(this.name2,
this.attribute)).andReturn(2);
+
+ EasyMock.replay(this.server);
+
+ double load = this.metric.getLoad(this.context);
+
+ EasyMock.verify(this.server);
+
+ Assert.assertEquals(3.0, load, 0.0);
+
+ EasyMock.reset(this.server);
+ }
+}
Added:
trunk/mod_cluster/src/test/java/org/jboss/modcluster/load/metric/MBeanAttributeRatioLoadMetricTestCase.java
===================================================================
---
trunk/mod_cluster/src/test/java/org/jboss/modcluster/load/metric/MBeanAttributeRatioLoadMetricTestCase.java
(rev 0)
+++
trunk/mod_cluster/src/test/java/org/jboss/modcluster/load/metric/MBeanAttributeRatioLoadMetricTestCase.java 2008-10-21
15:20:30 UTC (rev 1965)
@@ -0,0 +1,107 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+package org.jboss.modcluster.load.metric;
+
+import java.util.Arrays;
+import java.util.LinkedHashSet;
+
+import javax.management.MBeanServer;
+import javax.management.MalformedObjectNameException;
+import javax.management.ObjectName;
+
+import org.easymock.EasyMock;
+import org.jboss.modcluster.load.metric.impl.MBeanAttributeRatioLoadMetric;
+import org.jboss.modcluster.load.metric.impl.MBeanQueryLoadContext;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * @author Paul Ferraro
+ *
+ */
+@SuppressWarnings("boxing")
+public class MBeanAttributeRatioLoadMetricTestCase
+{
+ private MBeanServer server = EasyMock.createStrictMock(MBeanServer.class);
+ private ObjectName name1;
+ private ObjectName name2;
+ private MBeanQueryLoadContext context;
+
+ private String dividend;
+ private String divisor;
+ private LoadMetric<MBeanQueryLoadContext> metric;
+
+ public MBeanAttributeRatioLoadMetricTestCase()
+ {
+ this("dividend", "divisor");
+ }
+
+ private MBeanAttributeRatioLoadMetricTestCase(String dividend, String divisor)
+ {
+ this(new MBeanAttributeRatioLoadMetric(dividend, divisor), dividend, divisor);
+ }
+
+ protected
MBeanAttributeRatioLoadMetricTestCase(LoadMetric<MBeanQueryLoadContext> metric,
String dividend, String divisor)
+ {
+ this.metric = metric;
+ this.dividend = dividend;
+ this.divisor = divisor;
+ }
+
+ @Before
+ public void prepare() throws MalformedObjectNameException
+ {
+ ObjectName pattern = ObjectName.getInstance("domain:*");
+ this.name1 = ObjectName.getInstance("domain:name=test1");
+ this.name2 = ObjectName.getInstance("domain:name=test2");
+
+ EasyMock.expect(this.server.queryNames(pattern, null)).andReturn(new
LinkedHashSet<ObjectName>(Arrays.asList(this.name1, this.name2)));
+
+ EasyMock.replay(this.server);
+
+ this.context = new MBeanQueryLoadContext(this.server, pattern);
+
+ EasyMock.verify(this.server);
+ EasyMock.reset(this.server);
+ }
+
+ @Test
+ public void getLoad() throws Exception
+ {
+ EasyMock.expect(this.server.getAttribute(this.name1, this.dividend)).andReturn(1);
+ EasyMock.expect(this.server.getAttribute(this.name2, this.dividend)).andReturn(2);
+
+ EasyMock.expect(this.server.getAttribute(this.name1, this.divisor)).andReturn(2);
+ EasyMock.expect(this.server.getAttribute(this.name2, this.divisor)).andReturn(2);
+
+ EasyMock.replay(this.server);
+
+ double load = this.metric.getLoad(this.context);
+
+ EasyMock.verify(this.server);
+
+ Assert.assertEquals(0.75, load, 0.0);
+
+ EasyMock.reset(this.server);
+ }
+}
Added:
trunk/mod_cluster/src/test/java/org/jboss/modcluster/load/metric/MBeanLoadContextTestCase.java
===================================================================
---
trunk/mod_cluster/src/test/java/org/jboss/modcluster/load/metric/MBeanLoadContextTestCase.java
(rev 0)
+++
trunk/mod_cluster/src/test/java/org/jboss/modcluster/load/metric/MBeanLoadContextTestCase.java 2008-10-21
15:20:30 UTC (rev 1965)
@@ -0,0 +1,81 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+package org.jboss.modcluster.load.metric;
+
+import javax.management.MBeanServer;
+import javax.management.MalformedObjectNameException;
+import javax.management.ObjectName;
+
+import org.easymock.EasyMock;
+import org.jboss.modcluster.load.metric.impl.MBeanLoadContext;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * @author Paul Ferraro
+ *
+ */
+public class MBeanLoadContextTestCase
+{
+ private MBeanServer server = EasyMock.createStrictMock(MBeanServer.class);
+ private ObjectName name;
+ private MBeanLoadContext context;
+
+ @Before
+ public void construct() throws MalformedObjectNameException
+ {
+ this.name = ObjectName.getInstance("domain:name=test");
+
+ this.context = new MBeanLoadContext(this.server, this.name);
+ }
+
+ @Test
+ public void getAttribute() throws Exception
+ {
+ String attribute = "attribute";
+ String expected = "value";
+
+ EasyMock.expect(this.server.getAttribute(this.name,
attribute)).andReturn(expected);
+
+ EasyMock.replay(this.server);
+
+ String value = this.context.getAttribute(attribute, String.class);
+
+ EasyMock.verify(this.server);
+
+ Assert.assertSame(expected, value);
+
+ EasyMock.reset(this.server);
+ }
+
+ @Test
+ public void close()
+ {
+ EasyMock.replay(this.server);
+
+ this.context.close();
+
+ EasyMock.verify(this.server);
+ EasyMock.reset(this.server);
+ }
+}
Added:
trunk/mod_cluster/src/test/java/org/jboss/modcluster/load/metric/MBeanLoadMetricSourceTestCase.java
===================================================================
---
trunk/mod_cluster/src/test/java/org/jboss/modcluster/load/metric/MBeanLoadMetricSourceTestCase.java
(rev 0)
+++
trunk/mod_cluster/src/test/java/org/jboss/modcluster/load/metric/MBeanLoadMetricSourceTestCase.java 2008-10-21
15:20:30 UTC (rev 1965)
@@ -0,0 +1,94 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+package org.jboss.modcluster.load.metric;
+
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Iterator;
+
+import javax.management.MBeanServer;
+import javax.management.MalformedObjectNameException;
+
+import junit.framework.Assert;
+
+import org.easymock.EasyMock;
+import org.jboss.modcluster.load.metric.impl.MBeanLoadContext;
+import org.jboss.modcluster.load.metric.impl.MBeanLoadMetricSource;
+import org.junit.Test;
+
+/**
+ * @author Paul Ferraro
+ *
+ */
+@SuppressWarnings("unchecked")
+public class MBeanLoadMetricSourceTestCase
+{
+ private MBeanServer server = EasyMock.createStrictMock(MBeanServer.class);
+ private LoadMetric<MBeanLoadContext> metric1 =
EasyMock.createStrictMock(LoadMetric.class);
+ private LoadMetric<MBeanLoadContext> metric2 =
EasyMock.createStrictMock(LoadMetric.class);
+
+ private String name;
+ private LoadMetricSource<MBeanLoadContext> source;
+
+ public MBeanLoadMetricSourceTestCase() throws MalformedObjectNameException
+ {
+ this("domain:name=test");
+ }
+
+ protected MBeanLoadMetricSourceTestCase(String name) throws
MalformedObjectNameException
+ {
+ this.name = name;
+ this.source = this.createSource(Arrays.asList(this.metric1, this.metric2),
this.server);
+ }
+
+ protected MBeanLoadMetricSource
createSource(Collection<LoadMetric<MBeanLoadContext>> metrics, MBeanServer
server) throws MalformedObjectNameException
+ {
+ return new MBeanLoadMetricSource(metrics, this.name, server);
+ }
+
+ @Test
+ public void createContext()
+ {
+ EasyMock.replay(this.server);
+
+ MBeanLoadContext context = this.source.createContext();
+
+ EasyMock.verify(this.server);
+
+ Assert.assertNotNull(context);
+
+ EasyMock.reset(this.server);
+ }
+
+ @Test
+ public void getMetrics()
+ {
+ Collection<LoadMetric<MBeanLoadContext>> metrics =
this.source.getMetrics();
+
+ Assert.assertEquals(2, metrics.size());
+
+ Iterator<LoadMetric<MBeanLoadContext>> iterator = metrics.iterator();
+
+ Assert.assertSame(this.metric1, iterator.next());
+ Assert.assertSame(this.metric2, iterator.next());
+ }
+}
Added:
trunk/mod_cluster/src/test/java/org/jboss/modcluster/load/metric/MBeanQueryLoadContextTestCase.java
===================================================================
---
trunk/mod_cluster/src/test/java/org/jboss/modcluster/load/metric/MBeanQueryLoadContextTestCase.java
(rev 0)
+++
trunk/mod_cluster/src/test/java/org/jboss/modcluster/load/metric/MBeanQueryLoadContextTestCase.java 2008-10-21
15:20:30 UTC (rev 1965)
@@ -0,0 +1,131 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+package org.jboss.modcluster.load.metric;
+
+import java.util.Arrays;
+import java.util.LinkedHashSet;
+import java.util.List;
+
+import javax.management.MBeanServer;
+import javax.management.MalformedObjectNameException;
+import javax.management.ObjectName;
+
+import org.easymock.EasyMock;
+import org.jboss.modcluster.load.metric.impl.MBeanQueryLoadContext;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * @author Paul Ferraro
+ *
+ */
+public class MBeanQueryLoadContextTestCase
+{
+ private MBeanServer server = EasyMock.createStrictMock(MBeanServer.class);
+ private ObjectName name1;
+ private ObjectName name2;
+ private MBeanQueryLoadContext context1;
+ private MBeanQueryLoadContext context2;
+
+ @Before
+ public void construct() throws MalformedObjectNameException
+ {
+ this.name1 = ObjectName.getInstance("domain:name=test1");
+ this.name2 = ObjectName.getInstance("domain:name=test2");
+
+ EasyMock.replay(this.server);
+
+ this.context1 = new MBeanQueryLoadContext(this.server, this.name1);
+
+ EasyMock.verify(this.server);
+ EasyMock.reset(this.server);
+
+ ObjectName pattern = ObjectName.getInstance("domain:*");
+
+ EasyMock.expect(this.server.queryNames(pattern, null)).andReturn(new
LinkedHashSet<ObjectName>(Arrays.asList(this.name1, this.name2)));
+
+ EasyMock.replay(this.server);
+
+ this.context2 = new MBeanQueryLoadContext(this.server, pattern);
+
+ EasyMock.verify(this.server);
+ EasyMock.reset(this.server);
+ }
+
+ @Test
+ public void getAttributes() throws Exception
+ {
+ String expected = "value";
+
+ EasyMock.expect(this.server.getAttribute(this.name1,
"attribute")).andReturn(expected);
+
+ EasyMock.replay(this.server);
+
+ List<String> values = this.context1.getAttributes("attribute",
String.class);
+
+ EasyMock.verify(this.server);
+
+ Assert.assertEquals(1, values.size());
+ Assert.assertSame(expected, values.get(0));
+
+ EasyMock.reset(this.server);
+
+
+ String expected1 = "value1";
+ String expected2 = "value2";
+
+ EasyMock.expect(this.server.getAttribute(this.name1,
"attribute")).andReturn(expected1);
+ EasyMock.expect(this.server.getAttribute(this.name2,
"attribute")).andReturn(expected2);
+
+ EasyMock.replay(this.server);
+
+ values = this.context2.getAttributes("attribute", String.class);
+
+ EasyMock.verify(this.server);
+
+ Assert.assertEquals(2, values.size());
+ Assert.assertSame(expected1, values.get(0));
+ Assert.assertSame(expected2, values.get(1));
+
+ EasyMock.reset(this.server);
+ }
+
+ @Test
+ public void close()
+ {
+ EasyMock.replay(this.server);
+
+ this.context1.close();
+
+ EasyMock.verify(this.server);
+ EasyMock.reset(this.server);
+
+
+ EasyMock.replay(this.server);
+
+ this.context2.close();
+
+ EasyMock.verify(this.server);
+ EasyMock.reset(this.server);
+ }
+}
Added:
trunk/mod_cluster/src/test/java/org/jboss/modcluster/load/metric/MBeanQueryLoadMetricSourceTestCase.java
===================================================================
---
trunk/mod_cluster/src/test/java/org/jboss/modcluster/load/metric/MBeanQueryLoadMetricSourceTestCase.java
(rev 0)
+++
trunk/mod_cluster/src/test/java/org/jboss/modcluster/load/metric/MBeanQueryLoadMetricSourceTestCase.java 2008-10-21
15:20:30 UTC (rev 1965)
@@ -0,0 +1,104 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+package org.jboss.modcluster.load.metric;
+
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Iterator;
+
+import javax.management.MBeanServer;
+import javax.management.MalformedObjectNameException;
+import javax.management.ObjectName;
+import javax.management.QueryExp;
+
+import junit.framework.Assert;
+
+import org.easymock.Capture;
+import org.easymock.EasyMock;
+import org.jboss.modcluster.load.metric.impl.MBeanQueryLoadContext;
+import org.jboss.modcluster.load.metric.impl.MBeanQueryLoadMetricSource;
+import org.junit.Test;
+
+/**
+ * @author Paul Ferraro
+ *
+ */
+@SuppressWarnings("unchecked")
+public class MBeanQueryLoadMetricSourceTestCase
+{
+ private MBeanServer server = EasyMock.createStrictMock(MBeanServer.class);
+ private LoadMetric<MBeanQueryLoadContext> metric1 =
EasyMock.createStrictMock(LoadMetric.class);
+ private LoadMetric<MBeanQueryLoadContext> metric2 =
EasyMock.createStrictMock(LoadMetric.class);
+
+ private String pattern;
+ private LoadMetricSource<MBeanQueryLoadContext> source;
+
+ public MBeanQueryLoadMetricSourceTestCase() throws MalformedObjectNameException
+ {
+ this("domain:*");
+ }
+
+ protected MBeanQueryLoadMetricSourceTestCase(String pattern) throws
MalformedObjectNameException
+ {
+ this.pattern = pattern;
+ this.source = this.createSource(Arrays.asList(this.metric1, this.metric2),
this.server);
+ }
+
+ protected MBeanQueryLoadMetricSource
createSource(Collection<LoadMetric<MBeanQueryLoadContext>> metrics,
MBeanServer server) throws MalformedObjectNameException
+ {
+ return new MBeanQueryLoadMetricSource(metrics, this.pattern, server);
+ }
+
+ @Test
+ public void createContext()
+ {
+ Capture<ObjectName> capturedName = new Capture<ObjectName>();
+
+ EasyMock.expect(this.server.queryNames(EasyMock.capture(capturedName), (QueryExp)
EasyMock.isNull())).andReturn(Collections.emptySet());
+
+ EasyMock.replay(this.server);
+
+ MBeanQueryLoadContext context = this.source.createContext();
+
+ EasyMock.verify(this.server);
+
+ Assert.assertNotNull(context);
+
+ Assert.assertEquals(this.pattern, capturedName.getValue().toString());
+
+ EasyMock.reset(this.server);
+ }
+
+ @Test
+ public void getMetrics()
+ {
+ Collection<LoadMetric<MBeanQueryLoadContext>> metrics =
this.source.getMetrics();
+
+ Assert.assertEquals(2, metrics.size());
+
+ Iterator<LoadMetric<MBeanQueryLoadContext>> iterator =
metrics.iterator();
+
+ Assert.assertSame(this.metric1, iterator.next());
+ Assert.assertSame(this.metric2, iterator.next());
+ }
+}
Added:
trunk/mod_cluster/src/test/java/org/jboss/modcluster/load/metric/OperatingSystemLoadMetricSourceTestCase.java
===================================================================
---
trunk/mod_cluster/src/test/java/org/jboss/modcluster/load/metric/OperatingSystemLoadMetricSourceTestCase.java
(rev 0)
+++
trunk/mod_cluster/src/test/java/org/jboss/modcluster/load/metric/OperatingSystemLoadMetricSourceTestCase.java 2008-10-21
15:20:30 UTC (rev 1965)
@@ -0,0 +1,54 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+package org.jboss.modcluster.load.metric;
+
+import java.lang.management.ManagementFactory;
+import java.util.Collection;
+
+import javax.management.MBeanServer;
+import javax.management.MalformedObjectNameException;
+
+import org.jboss.modcluster.load.metric.impl.MBeanLoadContext;
+import org.jboss.modcluster.load.metric.impl.MBeanLoadMetricSource;
+import org.jboss.modcluster.load.metric.impl.OperatingSystemLoadMetricSource;
+
+/**
+ * @author Paul Ferraro
+ *
+ */
+public class OperatingSystemLoadMetricSourceTestCase extends
MBeanLoadMetricSourceTestCase
+{
+ public OperatingSystemLoadMetricSourceTestCase() throws MalformedObjectNameException
+ {
+ super(ManagementFactory.OPERATING_SYSTEM_MXBEAN_NAME);
+ }
+
+ /**
+ * @{inheritDoc}
+ * @see
org.jboss.modcluster.load.metric.MBeanLoadMetricSourceTestCase#createSource(java.util.Collection,
javax.management.MBeanServer)
+ */
+ @Override
+ protected MBeanLoadMetricSource
createSource(Collection<LoadMetric<MBeanLoadContext>> metrics, MBeanServer
server) throws MalformedObjectNameException
+ {
+ return new OperatingSystemLoadMetricSource(metrics, server);
+ }
+}
Modified:
trunk/mod_cluster/src/test/java/org/jboss/modcluster/load/metric/ReceiveTrafficLoadMetricTestCase.java
===================================================================
---
trunk/mod_cluster/src/test/java/org/jboss/modcluster/load/metric/ReceiveTrafficLoadMetricTestCase.java 2008-10-20
11:53:50 UTC (rev 1964)
+++
trunk/mod_cluster/src/test/java/org/jboss/modcluster/load/metric/ReceiveTrafficLoadMetricTestCase.java 2008-10-21
15:20:30 UTC (rev 1965)
@@ -21,147 +21,65 @@
*/
package org.jboss.modcluster.load.metric;
-import java.util.Arrays;
-import java.util.Collection;
-import java.util.Comparator;
-import java.util.Set;
-import java.util.TreeSet;
-
-import javax.management.MBeanServer;
-import javax.management.ObjectName;
-
-import junit.framework.TestCase;
-
-import org.easymock.Capture;
import org.easymock.EasyMock;
-import org.jboss.modcluster.load.metric.LoadMetric;
-import org.jboss.modcluster.load.metric.LoadMetricSource;
-import org.jboss.modcluster.load.metric.LoadMetricSourceRegistration;
import org.jboss.modcluster.load.metric.impl.ReceiveTrafficLoadMetric;
-import org.jboss.modcluster.load.metric.impl.RequestProcessorLoadMetricSource;
+import org.junit.Assert;
+import org.junit.Test;
/**
* @author Paul Ferraro
*
*/
-public class ReceiveTrafficLoadMetricTestCase extends TestCase
+@SuppressWarnings("boxing")
+public class ReceiveTrafficLoadMetricTestCase extends MBeanAttributeLoadMetricTestCase
{
- private final MBeanServer server = EasyMock.createStrictMock(MBeanServer.class);
-
- private LoadMetricSource source;
- private LoadMetric metric;
-
- /**
- * @{inheritDoc}
- * @see junit.framework.TestCase#setUp()
- */
- @Override
- protected void setUp() throws Exception
+ public ReceiveTrafficLoadMetricTestCase()
{
- LoadMetricSourceRegistration registration =
EasyMock.createStrictMock(LoadMetricSourceRegistration.class);
-
- Capture<LoadMetricSource> captured = new Capture<LoadMetricSource>();
-
- registration.add(EasyMock.capture(captured));
-
- EasyMock.replay(registration, this.server);
-
- RequestProcessorLoadMetricSource source = new
RequestProcessorLoadMetricSource(registration, this.server);
-
- EasyMock.verify(registration, this.server);
-
- assertSame(source, captured.getValue());
-
- EasyMock.reset(registration, this.server);
-
- this.source = source;
- this.metric = new ReceiveTrafficLoadMetric(source);
-
- Collection<? extends LoadMetric> metrics = this.source.getMetrics();
-
- assertNotNull(metrics);
- assertEquals(1, metrics.size());
- assertSame(this.metric, metrics.iterator().next());
-
- // Sleep for a sec to prevent infinite load
- Thread.sleep(500);
+ super(new ReceiveTrafficLoadMetric(), ReceiveTrafficLoadMetric.DEFAULT_ATTRIBUTE);
}
- public void testGetLoad() throws Exception
+ @Test
+ @Override
+ public void getLoad() throws Exception
{
- ObjectName pattern =
ObjectName.getInstance("jboss.web:type=GlobalRequestProcessor,*");
- ObjectName name1 =
ObjectName.getInstance("jboss.web:type=GlobalRequestProcessor,name=http-8080");
- ObjectName name2 =
ObjectName.getInstance("jboss.web:type=GlobalRequestProcessor,name=jk-8009");
+ EasyMock.expect(this.server.getAttribute(this.name1,
ReceiveTrafficLoadMetric.DEFAULT_ATTRIBUTE)).andReturn(0);
+ EasyMock.expect(this.server.getAttribute(this.name2,
ReceiveTrafficLoadMetric.DEFAULT_ATTRIBUTE)).andReturn(0);
- Comparator<ObjectName> comparator = new Comparator<ObjectName>()
- {
- public int compare(ObjectName name1, ObjectName name2)
- {
- return name1.getCanonicalName().compareTo(name2.getCanonicalName());
- }
- };
+ EasyMock.replay(this.server);
- Set<ObjectName> names = new TreeSet<ObjectName>(comparator);
- names.addAll(Arrays.asList(name1, name2));
+ double load = this.metric.getLoad(this.context);
- EasyMock.expect(this.server.queryNames(pattern, null)).andReturn(names);
-
- EasyMock.replay(this.server);
-
- this.source.prepare();
-
EasyMock.verify(this.server);
EasyMock.reset(this.server);
- EasyMock.expect(this.server.getAttribute(name1,
"bytesReceived")).andReturn(1000L);
- EasyMock.expect(this.server.getAttribute(name2,
"bytesReceived")).andReturn(2000L);
+ Thread.sleep(500);
+ EasyMock.expect(this.server.getAttribute(this.name1,
ReceiveTrafficLoadMetric.DEFAULT_ATTRIBUTE)).andReturn(1000L);
+ EasyMock.expect(this.server.getAttribute(this.name2,
ReceiveTrafficLoadMetric.DEFAULT_ATTRIBUTE)).andReturn(2000L);
+
EasyMock.replay(this.server);
- double load = this.metric.getLoad();
+ load = this.metric.getLoad(this.context);
EasyMock.verify(this.server);
- assertEquals(6, Math.round(load));
+ Assert.assertEquals(6.0, load, 0.5);
EasyMock.reset(this.server);
- EasyMock.replay(this.server);
-
- this.source.cleanup();
-
- EasyMock.verify(this.server);
- EasyMock.reset(this.server);
-
Thread.sleep(500);
- // Test incremental load
- EasyMock.expect(this.server.queryNames(pattern, null)).andReturn(names);
-
- EasyMock.replay(this.server);
- this.source.prepare();
-
- EasyMock.verify(this.server);
- EasyMock.reset(this.server);
+ EasyMock.expect(this.server.getAttribute(this.name1,
ReceiveTrafficLoadMetric.DEFAULT_ATTRIBUTE)).andReturn(2000L);
+ EasyMock.expect(this.server.getAttribute(this.name2,
ReceiveTrafficLoadMetric.DEFAULT_ATTRIBUTE)).andReturn(3000L);
- EasyMock.expect(this.server.getAttribute(name1,
"bytesReceived")).andReturn(2000L);
- EasyMock.expect(this.server.getAttribute(name2,
"bytesReceived")).andReturn(3000L);
-
EasyMock.replay(this.server);
- load = this.metric.getLoad();
+ load = this.metric.getLoad(this.context);
EasyMock.verify(this.server);
- assertEquals(4, Math.round(load));
+ Assert.assertEquals(4.0, load, 0.5);
EasyMock.reset(this.server);
-
- EasyMock.replay(this.server);
-
- this.source.cleanup();
-
- EasyMock.verify(this.server);
- EasyMock.reset(this.server);
}
}
Modified:
trunk/mod_cluster/src/test/java/org/jboss/modcluster/load/metric/RequestCountLoadMetricTestCase.java
===================================================================
---
trunk/mod_cluster/src/test/java/org/jboss/modcluster/load/metric/RequestCountLoadMetricTestCase.java 2008-10-20
11:53:50 UTC (rev 1964)
+++
trunk/mod_cluster/src/test/java/org/jboss/modcluster/load/metric/RequestCountLoadMetricTestCase.java 2008-10-21
15:20:30 UTC (rev 1965)
@@ -21,148 +21,66 @@
*/
package org.jboss.modcluster.load.metric;
-import java.util.Arrays;
-import java.util.Collection;
-import java.util.Comparator;
-import java.util.Set;
-import java.util.TreeSet;
-
-import javax.management.MBeanServer;
-import javax.management.ObjectName;
-
-import junit.framework.TestCase;
-
-import org.easymock.Capture;
import org.easymock.EasyMock;
-import org.jboss.modcluster.load.metric.LoadMetric;
-import org.jboss.modcluster.load.metric.LoadMetricSource;
-import org.jboss.modcluster.load.metric.LoadMetricSourceRegistration;
import org.jboss.modcluster.load.metric.impl.RequestCountLoadMetric;
-import org.jboss.modcluster.load.metric.impl.RequestProcessorLoadMetricSource;
+import org.junit.Assert;
+import org.junit.Test;
/**
* @author Paul Ferraro
*
*/
-public class RequestCountLoadMetricTestCase extends TestCase
+@SuppressWarnings("boxing")
+public class RequestCountLoadMetricTestCase extends MBeanAttributeLoadMetricTestCase
{
- private final MBeanServer server = EasyMock.createStrictMock(MBeanServer.class);
-
- private LoadMetricSource source;
- private LoadMetric metric;
-
- /**
- * @{inheritDoc}
- * @see junit.framework.TestCase#setUp()
- */
- @Override
- protected void setUp() throws Exception
+ public RequestCountLoadMetricTestCase()
{
- LoadMetricSourceRegistration registration =
EasyMock.createStrictMock(LoadMetricSourceRegistration.class);
-
- Capture<LoadMetricSource> captured = new Capture<LoadMetricSource>();
-
- registration.add(EasyMock.capture(captured));
-
- EasyMock.replay(registration, this.server);
-
- RequestProcessorLoadMetricSource source = new
RequestProcessorLoadMetricSource(registration, this.server);
-
- EasyMock.verify(registration, this.server);
-
- assertSame(source, captured.getValue());
-
- EasyMock.reset(registration, this.server);
-
- this.source = source;
- this.metric = new RequestCountLoadMetric(source);
-
- Collection<? extends LoadMetric> metrics = this.source.getMetrics();
-
- assertNotNull(metrics);
- assertEquals(1, metrics.size());
- assertSame(this.metric, metrics.iterator().next());
-
- // Sleep for a sec to avoid infinite load.
- Thread.sleep(500);
+ super(new RequestCountLoadMetric(), RequestCountLoadMetric.DEFAULT_ATTRIBUTE);
}
- public void testGetLoad() throws Exception
+ @Test
+ @Override
+ public void getLoad() throws Exception
{
- ObjectName pattern =
ObjectName.getInstance("jboss.web:type=GlobalRequestProcessor,*");
- ObjectName name1 =
ObjectName.getInstance("jboss.web:type=GlobalRequestProcessor,name=http-8080");
- ObjectName name2 =
ObjectName.getInstance("jboss.web:type=GlobalRequestProcessor,name=jk-8009");
+ EasyMock.expect(this.server.getAttribute(this.name1,
RequestCountLoadMetric.DEFAULT_ATTRIBUTE)).andReturn(0);
+ EasyMock.expect(this.server.getAttribute(this.name2,
RequestCountLoadMetric.DEFAULT_ATTRIBUTE)).andReturn(0);
- Comparator<ObjectName> comparator = new Comparator<ObjectName>()
- {
- public int compare(ObjectName name1, ObjectName name2)
- {
- return name1.getCanonicalName().compareTo(name2.getCanonicalName());
- }
- };
+ EasyMock.replay(this.server);
- Set<ObjectName> names = new TreeSet<ObjectName>(comparator);
- names.addAll(Arrays.asList(name1, name2));
+ double load = this.metric.getLoad(this.context);
- EasyMock.expect(this.server.queryNames(pattern, null)).andReturn(names);
-
- EasyMock.replay(this.server);
-
- this.source.prepare();
-
EasyMock.verify(this.server);
EasyMock.reset(this.server);
- EasyMock.expect(this.server.getAttribute(name1,
"requestCount")).andReturn(1);
- EasyMock.expect(this.server.getAttribute(name2,
"requestCount")).andReturn(2);
+ Thread.sleep(500);
+ EasyMock.expect(this.server.getAttribute(this.name1,
RequestCountLoadMetric.DEFAULT_ATTRIBUTE)).andReturn(1);
+ EasyMock.expect(this.server.getAttribute(this.name2,
RequestCountLoadMetric.DEFAULT_ATTRIBUTE)).andReturn(2);
+
EasyMock.replay(this.server);
- double load = this.metric.getLoad();
+ load = this.metric.getLoad(this.context);
EasyMock.verify(this.server);
- assertEquals(6, Math.round(load));
+ Assert.assertEquals(6, load, 0.5);
EasyMock.reset(this.server);
- EasyMock.replay(this.server);
-
- this.source.cleanup();
-
- EasyMock.verify(this.server);
- EasyMock.reset(this.server);
-
Thread.sleep(500);
// Test incremental load
- EasyMock.expect(this.server.queryNames(pattern, null)).andReturn(names);
+ EasyMock.expect(this.server.getAttribute(this.name1,
RequestCountLoadMetric.DEFAULT_ATTRIBUTE)).andReturn(2);
+ EasyMock.expect(this.server.getAttribute(this.name2,
RequestCountLoadMetric.DEFAULT_ATTRIBUTE)).andReturn(3);
EasyMock.replay(this.server);
-
- this.source.prepare();
-
- EasyMock.verify(this.server);
- EasyMock.reset(this.server);
- EasyMock.expect(this.server.getAttribute(name1,
"requestCount")).andReturn(2);
- EasyMock.expect(this.server.getAttribute(name2,
"requestCount")).andReturn(3);
+ load = this.metric.getLoad(this.context);
- EasyMock.replay(this.server);
-
- load = this.metric.getLoad();
-
EasyMock.verify(this.server);
- assertEquals(4, Math.round(load));
+ Assert.assertEquals(4, load, 0.5);
EasyMock.reset(this.server);
-
- EasyMock.replay(this.server);
-
- this.source.cleanup();
-
- EasyMock.verify(this.server);
- EasyMock.reset(this.server);
}
}
Added:
trunk/mod_cluster/src/test/java/org/jboss/modcluster/load/metric/RequestProcessorLoadMetricSourceTestCase.java
===================================================================
---
trunk/mod_cluster/src/test/java/org/jboss/modcluster/load/metric/RequestProcessorLoadMetricSourceTestCase.java
(rev 0)
+++
trunk/mod_cluster/src/test/java/org/jboss/modcluster/load/metric/RequestProcessorLoadMetricSourceTestCase.java 2008-10-21
15:20:30 UTC (rev 1965)
@@ -0,0 +1,49 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+package org.jboss.modcluster.load.metric;
+
+import java.util.Collection;
+
+import javax.management.MBeanServer;
+import javax.management.MalformedObjectNameException;
+
+import org.jboss.modcluster.load.metric.impl.MBeanQueryLoadContext;
+import org.jboss.modcluster.load.metric.impl.MBeanQueryLoadMetricSource;
+import org.jboss.modcluster.load.metric.impl.RequestProcessorLoadMetricSource;
+
+/**
+ * @author Paul Ferraro
+ *
+ */
+public class RequestProcessorLoadMetricSourceTestCase extends
MBeanQueryLoadMetricSourceTestCase
+{
+ public RequestProcessorLoadMetricSourceTestCase() throws MalformedObjectNameException
+ {
+ super(RequestProcessorLoadMetricSource.DEFAULT_PATTERN);
+ }
+
+ @Override
+ protected MBeanQueryLoadMetricSource
createSource(Collection<LoadMetric<MBeanQueryLoadContext>> metrics,
MBeanServer server) throws MalformedObjectNameException
+ {
+ return new RequestProcessorLoadMetricSource(metrics, server);
+ }
+}
Modified:
trunk/mod_cluster/src/test/java/org/jboss/modcluster/load/metric/SendTrafficLoadMetricTestCase.java
===================================================================
---
trunk/mod_cluster/src/test/java/org/jboss/modcluster/load/metric/SendTrafficLoadMetricTestCase.java 2008-10-20
11:53:50 UTC (rev 1964)
+++
trunk/mod_cluster/src/test/java/org/jboss/modcluster/load/metric/SendTrafficLoadMetricTestCase.java 2008-10-21
15:20:30 UTC (rev 1965)
@@ -21,148 +21,65 @@
*/
package org.jboss.modcluster.load.metric;
-import java.util.Arrays;
-import java.util.Collection;
-import java.util.Comparator;
-import java.util.Set;
-import java.util.TreeSet;
-
-import javax.management.MBeanServer;
-import javax.management.ObjectName;
-
-import junit.framework.TestCase;
-
-import org.easymock.Capture;
import org.easymock.EasyMock;
-import org.jboss.modcluster.load.metric.LoadMetric;
-import org.jboss.modcluster.load.metric.LoadMetricSource;
-import org.jboss.modcluster.load.metric.LoadMetricSourceRegistration;
-import org.jboss.modcluster.load.metric.impl.RequestProcessorLoadMetricSource;
import org.jboss.modcluster.load.metric.impl.SendTrafficLoadMetric;
+import org.junit.Assert;
+import org.junit.Test;
/**
* @author Paul Ferraro
*
*/
-public class SendTrafficLoadMetricTestCase extends TestCase
+@SuppressWarnings("boxing")
+public class SendTrafficLoadMetricTestCase extends MBeanAttributeLoadMetricTestCase
{
- private final MBeanServer server = EasyMock.createStrictMock(MBeanServer.class);
-
- private LoadMetricSource source;
- private LoadMetric metric;
-
- /**
- * @{inheritDoc}
- * @see junit.framework.TestCase#setUp()
- */
- @Override
- protected void setUp() throws Exception
+ public SendTrafficLoadMetricTestCase()
{
- LoadMetricSourceRegistration registration =
EasyMock.createStrictMock(LoadMetricSourceRegistration.class);
-
- Capture<LoadMetricSource> captured = new Capture<LoadMetricSource>();
-
- registration.add(EasyMock.capture(captured));
-
- EasyMock.replay(registration, this.server);
-
- RequestProcessorLoadMetricSource source = new
RequestProcessorLoadMetricSource(registration, this.server);
-
- EasyMock.verify(registration, this.server);
-
- assertSame(source, captured.getValue());
-
- EasyMock.reset(registration, this.server);
-
- this.source = source;
- this.metric = new SendTrafficLoadMetric(source);
-
- Collection<? extends LoadMetric> metrics = this.source.getMetrics();
-
- assertNotNull(metrics);
- assertEquals(1, metrics.size());
- assertSame(this.metric, metrics.iterator().next());
-
- // Sleep for a bit to avoid infinite load
- Thread.sleep(500);
+ super(new SendTrafficLoadMetric(), SendTrafficLoadMetric.DEFAULT_ATTRIBUTE);
}
- public void testGetLoad() throws Exception
+ @Test
+ @Override
+ public void getLoad() throws Exception
{
- ObjectName pattern =
ObjectName.getInstance("jboss.web:type=GlobalRequestProcessor,*");
- ObjectName name1 =
ObjectName.getInstance("jboss.web:type=GlobalRequestProcessor,name=http-8080");
- ObjectName name2 =
ObjectName.getInstance("jboss.web:type=GlobalRequestProcessor,name=jk-8009");
+ EasyMock.expect(this.server.getAttribute(this.name1,
SendTrafficLoadMetric.DEFAULT_ATTRIBUTE)).andReturn(0);
+ EasyMock.expect(this.server.getAttribute(this.name2,
SendTrafficLoadMetric.DEFAULT_ATTRIBUTE)).andReturn(0);
- Comparator<ObjectName> comparator = new Comparator<ObjectName>()
- {
- public int compare(ObjectName name1, ObjectName name2)
- {
- return name1.getCanonicalName().compareTo(name2.getCanonicalName());
- }
- };
+ EasyMock.replay(this.server);
- Set<ObjectName> names = new TreeSet<ObjectName>(comparator);
- names.addAll(Arrays.asList(name1, name2));
+ double load = this.metric.getLoad(this.context);
- EasyMock.expect(this.server.queryNames(pattern, null)).andReturn(names);
-
- EasyMock.replay(this.server);
-
- this.source.prepare();
-
EasyMock.verify(this.server);
EasyMock.reset(this.server);
- EasyMock.expect(this.server.getAttribute(name1,
"bytesSent")).andReturn(1000L);
- EasyMock.expect(this.server.getAttribute(name2,
"bytesSent")).andReturn(2000L);
+ Thread.sleep(500);
+ EasyMock.expect(this.server.getAttribute(this.name1,
SendTrafficLoadMetric.DEFAULT_ATTRIBUTE)).andReturn(1000L);
+ EasyMock.expect(this.server.getAttribute(this.name2,
SendTrafficLoadMetric.DEFAULT_ATTRIBUTE)).andReturn(2000L);
+
EasyMock.replay(this.server);
- double load = this.metric.getLoad();
+ load = this.metric.getLoad(this.context);
EasyMock.verify(this.server);
+
+ Assert.assertEquals(6.0, load, 0.5);
- assertEquals(6, Math.round(load));
-
EasyMock.reset(this.server);
- EasyMock.replay(this.server);
-
- this.source.cleanup();
-
- EasyMock.verify(this.server);
- EasyMock.reset(this.server);
-
Thread.sleep(500);
-
- // Test incremental load
- EasyMock.expect(this.server.queryNames(pattern, null)).andReturn(names);
-
- EasyMock.replay(this.server);
- this.source.prepare();
-
- EasyMock.verify(this.server);
- EasyMock.reset(this.server);
+ EasyMock.expect(this.server.getAttribute(this.name1,
SendTrafficLoadMetric.DEFAULT_ATTRIBUTE)).andReturn(2000L);
+ EasyMock.expect(this.server.getAttribute(this.name2,
SendTrafficLoadMetric.DEFAULT_ATTRIBUTE)).andReturn(3000L);
- EasyMock.expect(this.server.getAttribute(name1,
"bytesSent")).andReturn(2000L);
- EasyMock.expect(this.server.getAttribute(name2,
"bytesSent")).andReturn(3000L);
-
EasyMock.replay(this.server);
- load = this.metric.getLoad();
+ load = this.metric.getLoad(this.context);
EasyMock.verify(this.server);
- assertEquals(4, Math.round(load));
+ Assert.assertEquals(4.0, load, 0.5);
EasyMock.reset(this.server);
-
- EasyMock.replay(this.server);
-
- this.source.cleanup();
-
- EasyMock.verify(this.server);
- EasyMock.reset(this.server);
}
}
Added:
trunk/mod_cluster/src/test/java/org/jboss/modcluster/load/metric/SessionLoadMetricSourceTestCase.java
===================================================================
---
trunk/mod_cluster/src/test/java/org/jboss/modcluster/load/metric/SessionLoadMetricSourceTestCase.java
(rev 0)
+++
trunk/mod_cluster/src/test/java/org/jboss/modcluster/load/metric/SessionLoadMetricSourceTestCase.java 2008-10-21
15:20:30 UTC (rev 1965)
@@ -0,0 +1,49 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+package org.jboss.modcluster.load.metric;
+
+import java.util.Collection;
+
+import javax.management.MBeanServer;
+import javax.management.MalformedObjectNameException;
+
+import org.jboss.modcluster.load.metric.impl.MBeanQueryLoadContext;
+import org.jboss.modcluster.load.metric.impl.MBeanQueryLoadMetricSource;
+import org.jboss.modcluster.load.metric.impl.SessionLoadMetricSource;
+
+/**
+ * @author Paul Ferraro
+ *
+ */
+public class SessionLoadMetricSourceTestCase extends MBeanQueryLoadMetricSourceTestCase
+{
+ public SessionLoadMetricSourceTestCase() throws MalformedObjectNameException
+ {
+ super(SessionLoadMetricSource.DEFAULT_PATTERN);
+ }
+
+ @Override
+ protected MBeanQueryLoadMetricSource
createSource(Collection<LoadMetric<MBeanQueryLoadContext>> metrics,
MBeanServer server) throws MalformedObjectNameException
+ {
+ return new SessionLoadMetricSource(metrics, server);
+ }
+}
Modified:
trunk/mod_cluster/src/test/java/org/jboss/modcluster/load/metric/SystemMemoryUsageLoadMetricTestCase.java
===================================================================
---
trunk/mod_cluster/src/test/java/org/jboss/modcluster/load/metric/SystemMemoryUsageLoadMetricTestCase.java 2008-10-20
11:53:50 UTC (rev 1964)
+++
trunk/mod_cluster/src/test/java/org/jboss/modcluster/load/metric/SystemMemoryUsageLoadMetricTestCase.java 2008-10-21
15:20:30 UTC (rev 1965)
@@ -22,134 +22,67 @@
package org.jboss.modcluster.load.metric;
import java.lang.management.ManagementFactory;
-import java.util.Collection;
-import javax.management.MBeanAttributeInfo;
-import javax.management.MBeanInfo;
+import javax.management.AttributeNotFoundException;
import javax.management.MBeanServer;
+import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
-import junit.framework.TestCase;
-
-import org.easymock.Capture;
import org.easymock.EasyMock;
-import org.jboss.modcluster.load.metric.LoadMetric;
-import org.jboss.modcluster.load.metric.LoadMetricSource;
-import org.jboss.modcluster.load.metric.LoadMetricSourceRegistration;
-import org.jboss.modcluster.load.metric.impl.AverageSystemLoadMetric;
-import org.jboss.modcluster.load.metric.impl.OperatingSystemLoadMetricSource;
+import org.jboss.modcluster.load.metric.impl.MBeanLoadContext;
import org.jboss.modcluster.load.metric.impl.SystemMemoryUsageLoadMetric;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
-import com.sun.management.OperatingSystemMXBean;
-
/**
* @author Paul Ferraro
*
*/
-public class SystemMemoryUsageLoadMetricTestCase extends TestCase
+public class SystemMemoryUsageLoadMetricTestCase
{
private final MBeanServer server = EasyMock.createStrictMock(MBeanServer.class);
- private LoadMetricSource source;
- private LoadMetric metric;
+ private LoadMetric<MBeanLoadContext> metric = new
SystemMemoryUsageLoadMetric();
- /**
- * @{inheritDoc}
- * @see junit.framework.TestCase#setUp()
- */
- @Override
- protected void setUp() throws Exception
+ private ObjectName name;
+ private MBeanLoadContext context;
+
+ @Before
+ public void prepare() throws MalformedObjectNameException
{
- LoadMetricSourceRegistration registration =
EasyMock.createStrictMock(LoadMetricSourceRegistration.class);
+ this.name =
ObjectName.getInstance(ManagementFactory.OPERATING_SYSTEM_MXBEAN_NAME);
+ this.context = new MBeanLoadContext(this.server, this.name);
+ }
+
+ @SuppressWarnings("boxing")
+ @Test
+ public void getLoad() throws Exception
+ {
+ EasyMock.expect(this.server.getAttribute(this.name,
SystemMemoryUsageLoadMetric.FREE_MEMORY)).andReturn(256);
+ EasyMock.expect(this.server.getAttribute(this.name,
SystemMemoryUsageLoadMetric.TOTAL_MEMORY)).andReturn(1024L);
- Capture<LoadMetricSource> captured = new Capture<LoadMetricSource>();
-
- registration.add(EasyMock.capture(captured));
-
- EasyMock.replay(registration, this.server);
-
- OperatingSystemLoadMetricSource source = new
OperatingSystemLoadMetricSource(registration, this.server);
-
- EasyMock.verify(registration, this.server);
-
- assertSame(source, captured.getValue());
-
- EasyMock.reset(registration, this.server);
-
- this.source = source;
-
- ObjectName name =
ObjectName.getInstance(ManagementFactory.OPERATING_SYSTEM_MXBEAN_NAME);
-
- // Test non-com.sun.management.OperatingSystemMXBean case
- MBeanInfo info = new MBeanInfo(OperatingSystemMXBean.class.getName(), null, null,
null, null, null);
-
- EasyMock.expect(this.server.getMBeanInfo(name)).andReturn(info);
-
EasyMock.replay(this.server);
- this.metric = new AverageSystemLoadMetric(source);
+ double load = this.metric.getLoad(this.context);
EasyMock.verify(this.server);
- Collection<? extends LoadMetric> metrics = this.source.getMetrics();
-
- assertNotNull(metrics);
- assertTrue(metrics.isEmpty());
+ Assert.assertEquals(0.25, load, 0.0);
EasyMock.reset(this.server);
-
- // Test com.sun.management.OperatingSystemMXBean case
- MBeanAttributeInfo attribute1 = new
MBeanAttributeInfo("FreePhysicalMemorySize", Long.TYPE.getName(), null, true,
false, false);
- MBeanAttributeInfo attribute2 = new
MBeanAttributeInfo("TotalPhysicalMemorySize", Long.TYPE.getName(), null, true,
false, false);
-
- info = new MBeanInfo(OperatingSystemMXBean.class.getName(), "", new
MBeanAttributeInfo[] { attribute1, attribute2 }, null, null, null);
-
- EasyMock.expect(this.server.getMBeanInfo(name)).andReturn(info);
-
- EasyMock.replay(this.server);
-
- this.metric = new SystemMemoryUsageLoadMetric(source);
-
- EasyMock.verify(this.server);
-
- metrics = this.source.getMetrics();
-
- assertNotNull(metrics);
- assertEquals(1, metrics.size());
- assertSame(this.metric, metrics.iterator().next());
-
- EasyMock.reset(this.server);
- }
- public void testGetLoad() throws Exception
- {
- ObjectName name =
ObjectName.getInstance(ManagementFactory.OPERATING_SYSTEM_MXBEAN_NAME);
- EasyMock.replay(this.server);
-
- this.source.prepare();
-
- EasyMock.verify(this.server);
- EasyMock.reset(this.server);
+ // Test Java 1.5 behavior
+ EasyMock.expect(this.server.getAttribute(this.name,
SystemMemoryUsageLoadMetric.FREE_MEMORY)).andThrow(new AttributeNotFoundException());
- EasyMock.expect(this.server.getAttribute(name,
"FreePhysicalMemorySize")).andReturn(1000L);
- EasyMock.expect(this.server.getAttribute(name,
"TotalPhysicalMemorySize")).andReturn(5000L);
-
EasyMock.replay(this.server);
- double load = this.metric.getLoad();
+ load = this.metric.getLoad(this.context);
EasyMock.verify(this.server);
- assertEquals(0.2, load);
-
- EasyMock.reset(this.server);
-
- EasyMock.replay(this.server);
-
- this.source.cleanup();
-
- EasyMock.verify(this.server);
- EasyMock.reset(this.server);
+ Assert.assertEquals(0.0, load, 0.0);
+ Assert.assertEquals(0, this.metric.getWeight());
}
}
Modified:
trunk/mod_cluster/src/test/java/org/jboss/modcluster/load/metric/ThreadCountLoadMetricTestCase.java
===================================================================
---
trunk/mod_cluster/src/test/java/org/jboss/modcluster/load/metric/ThreadCountLoadMetricTestCase.java 2008-10-20
11:53:50 UTC (rev 1964)
+++
trunk/mod_cluster/src/test/java/org/jboss/modcluster/load/metric/ThreadCountLoadMetricTestCase.java 2008-10-21
15:20:30 UTC (rev 1965)
@@ -22,87 +22,38 @@
package org.jboss.modcluster.load.metric;
import java.lang.management.ThreadMXBean;
-import java.util.Collection;
-import junit.framework.TestCase;
-
-import org.easymock.Capture;
import org.easymock.EasyMock;
-import org.jboss.modcluster.load.metric.LoadMetric;
-import org.jboss.modcluster.load.metric.LoadMetricSource;
-import org.jboss.modcluster.load.metric.LoadMetricSourceRegistration;
import org.jboss.modcluster.load.metric.impl.ThreadCountLoadMetric;
+import org.junit.Assert;
+import org.junit.Test;
/**
* @author Paul Ferraro
*
*/
-public class ThreadCountLoadMetricTestCase extends TestCase
+@SuppressWarnings("boxing")
+public class ThreadCountLoadMetricTestCase
{
- private final ThreadMXBean bean = EasyMock.createStrictMock(ThreadMXBean.class);
+ private ThreadMXBean bean = EasyMock.createStrictMock(ThreadMXBean.class);
- private LoadMetricSource source;
- private LoadMetric metric;
-
- /**
- * @{inheritDoc}
- * @see junit.framework.TestCase#setUp()
- */
- @Override
- protected void setUp() throws Exception
- {
- LoadMetricSourceRegistration registration =
EasyMock.createStrictMock(LoadMetricSourceRegistration.class);
-
- Capture<LoadMetricSource> captured = new Capture<LoadMetricSource>();
-
- registration.add(EasyMock.capture(captured));
-
- EasyMock.replay(registration, this.bean);
-
- ThreadCountLoadMetric source = new ThreadCountLoadMetric(registration, this.bean);
-
- EasyMock.verify(registration, this.bean);
+ private LoadMetric<LoadContext> metric = new ThreadCountLoadMetric(this.bean);
- assertSame(source, captured.getValue());
-
- EasyMock.reset(registration, this.bean);
-
- this.source = source;
- this.metric = source;
-
- Collection<? extends LoadMetric> metrics = this.source.getMetrics();
-
- assertNotNull(metrics);
- assertEquals(1, metrics.size());
- assertSame(this.metric, metrics.iterator().next());
- }
-
- public void testGetLoad() throws Exception
+ @Test
+ public void getLoad() throws Exception
{
- EasyMock.replay(this.bean);
-
- this.source.prepare();
-
- EasyMock.verify(this.bean);
- EasyMock.reset(this.bean);
+ LoadContext context = EasyMock.createMock(LoadContext.class);
EasyMock.expect(this.bean.getThreadCount()).andReturn(50);
EasyMock.replay(this.bean);
- double load = this.metric.getLoad();
+ double load = this.metric.getLoad(context);
EasyMock.verify(this.bean);
- assertEquals(50.0, load);
+ Assert.assertEquals(50.0, load, 0);
EasyMock.reset(this.bean);
-
- EasyMock.replay(this.bean);
-
- this.source.cleanup();
-
- EasyMock.verify(this.bean);
- EasyMock.reset(this.bean);
}
}
Added:
trunk/mod_cluster/src/test/java/org/jboss/modcluster/load/metric/ThreadPoolLoadMetricSourceTestCase.java
===================================================================
---
trunk/mod_cluster/src/test/java/org/jboss/modcluster/load/metric/ThreadPoolLoadMetricSourceTestCase.java
(rev 0)
+++
trunk/mod_cluster/src/test/java/org/jboss/modcluster/load/metric/ThreadPoolLoadMetricSourceTestCase.java 2008-10-21
15:20:30 UTC (rev 1965)
@@ -0,0 +1,49 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+package org.jboss.modcluster.load.metric;
+
+import java.util.Collection;
+
+import javax.management.MBeanServer;
+import javax.management.MalformedObjectNameException;
+
+import org.jboss.modcluster.load.metric.impl.MBeanQueryLoadContext;
+import org.jboss.modcluster.load.metric.impl.MBeanQueryLoadMetricSource;
+import org.jboss.modcluster.load.metric.impl.ThreadPoolLoadMetricSource;
+
+/**
+ * @author Paul Ferraro
+ *
+ */
+public class ThreadPoolLoadMetricSourceTestCase extends
MBeanQueryLoadMetricSourceTestCase
+{
+ public ThreadPoolLoadMetricSourceTestCase() throws MalformedObjectNameException
+ {
+ super(ThreadPoolLoadMetricSource.DEFAULT_PATTERN);
+ }
+
+ @Override
+ protected MBeanQueryLoadMetricSource
createSource(Collection<LoadMetric<MBeanQueryLoadContext>> metrics,
MBeanServer server) throws MalformedObjectNameException
+ {
+ return new ThreadPoolLoadMetricSource(metrics, server);
+ }
+}