bloritsch 02/03/07 12:27:20
Modified:
examples/altprofile/src/java/org/apache/avalon/examples/altprofile
DefaultExampleProfilable.java Main.java
Log:
add some concurrency to make very large lists quicker to test.
Revision Changes Path
1.2 +22 -22
jakarta-avalon-excalibur/examples/altprofile/src/java/org/apache/avalon/examples/altprofile/DefaultExampleProfilable.java
Index: DefaultExampleProfilable.java
===================================================================
RCS file:
/home/cvs/jakarta-avalon-excalibur/examples/altprofile/src/java/org/apache/avalon/examples/altprofile/DefaultExampleProfilable.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- DefaultExampleProfilable.java 4 Mar 2002 09:02:10 -0000 1.1
+++ DefaultExampleProfilable.java 7 Mar 2002 20:27:20 -0000 1.2
@@ -28,7 +28,7 @@
* Note, this code ignores exceptions to keep the code simple.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Leif Mortenson</a>
- * @version CVS $Revision: 1.1 $ $Date: 2002/03/04 09:02:10 $
+ * @version CVS $Revision: 1.2 $ $Date: 2002/03/07 20:27:20 $
* @since 4.1
*/
public class DefaultExampleProfilable
@@ -42,34 +42,34 @@
public static final String PROFILE_POINT_COUNTER_SLOW_NAME =
"counter-slow";
public static final String PROFILE_POINT_COUNTER_RANDOM_NAME =
"counter-random";
public static final String PROFILE_POINT_DOACTION_NAME =
"doaction-counter";
-
+
/** Profilable Name assigned to this Profilable */
private String m_profilableName;
-
+
/** ProfilePoint used to profile random values with lots of updates. */
private ValueProfilePoint m_randomQuickProfilePoint;
-
+
/** ProfilePoint used to profile random values with few of updates. */
private ValueProfilePoint m_randomSlowProfilePoint;
-
+
/** ProfilePoint used to profile random values with updates at a random
rate. */
private ValueProfilePoint m_randomRandomProfilePoint;
-
+
/** ProfilePoint used to profile random actions with lots of updates. */
private CounterProfilePoint m_counterQuickProfilePoint;
-
+
/** ProfilePoint used to profile random actions with few of updates. */
private CounterProfilePoint m_counterSlowProfilePoint;
-
+
/** ProfilePoint used to profile random actions with updates at a random
rate. */
private CounterProfilePoint m_counterRandomProfilePoint;
-
+
/** ProfilePoint used to count the number of times that doAction is
called. */
private CounterProfilePoint m_doActionProfilePoint;
-
+
/** Thread which is used to send profile data to the random profile
points. */
private Thread m_runner;
-
+
/*---------------------------------------------------------------
* Constructors
*-------------------------------------------------------------*/
@@ -84,7 +84,7 @@
m_counterRandomProfilePoint = new CounterProfilePoint(
PROFILE_POINT_COUNTER_RANDOM_NAME );
m_doActionProfilePoint = new CounterProfilePoint(
PROFILE_POINT_DOACTION_NAME );
}
-
+
/*---------------------------------------------------------------
* ExampleProfilable Methods
*-------------------------------------------------------------*/
@@ -94,11 +94,11 @@
public void doAction()
{
getLogger().info( "ExampleProfilable.doAction() called." );
-
+
// Notify the profiler.
m_doActionProfilePoint.increment();
}
-
+
/*---------------------------------------------------------------
* Startable Methods
*-------------------------------------------------------------*/
@@ -113,7 +113,7 @@
m_runner.start();
}
}
-
+
/**
* Stop the component.
*/
@@ -125,7 +125,7 @@
m_runner = null;
}
}
-
+
/*---------------------------------------------------------------
* Runnable Methods
*-------------------------------------------------------------*/
@@ -150,11 +150,11 @@
return;
}
}
-
+
// Handle the quick Profile Points
m_randomQuickProfilePoint.setValue( (int)(Math.random() * 100) );
m_counterQuickProfilePoint.increment();
-
+
// Handle the slow Profile Points
counter++;
if ( counter >= 20 )
@@ -163,7 +163,7 @@
m_counterSlowProfilePoint.increment();
counter = 0;
}
-
+
// Handle the random Profile Points. Fire 10% of the time.
if ( 100 * Math.random() < 10 )
{
@@ -172,7 +172,7 @@
}
}
}
-
+
/*---------------------------------------------------------------
* Profilable Methods
*-------------------------------------------------------------*/
@@ -193,12 +193,12 @@
{
m_profilableName = name;
}
-
+
/**
* Gets the name of the Profilable. The Profilable Name is used to
* uniquely identify the Profilable during the configuration of the
* Profiler and to gain access to a ProfilableDescriptor through a
- * ProfilerManager. The value should be a string which does not
+ * ProfilerManager. The value should be a string which does not
* contain spaces or periods.
*
* @return The name used to identify a Profilable.
1.4 +75 -24
jakarta-avalon-excalibur/examples/altprofile/src/java/org/apache/avalon/examples/altprofile/Main.java
Index: Main.java
===================================================================
RCS file:
/home/cvs/jakarta-avalon-excalibur/examples/altprofile/src/java/org/apache/avalon/examples/altprofile/Main.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- Main.java 5 Mar 2002 13:28:25 -0000 1.3
+++ Main.java 7 Mar 2002 20:27:20 -0000 1.4
@@ -16,6 +16,7 @@
import org.apache.avalon.excalibur.altprofile.profiler.gui.ProfilerFrame;
import org.apache.avalon.excalibur.component.DefaultRoleManager;
import org.apache.avalon.excalibur.logger.DefaultLogKitManager;
+import org.apache.avalon.excalibur.concurrent.ThreadBarrier;
import org.apache.avalon.framework.configuration.Configuration;
import org.apache.avalon.framework.configuration.ConfigurationException;
@@ -25,6 +26,7 @@
import org.apache.log.Hierarchy;
import org.apache.log.Logger;
+import org.apache.avalon.excalibur.concurrent.*;
import org.apache.log.Priority;
/**
@@ -40,7 +42,7 @@
* Note, this code ignores exceptions to keep the code simple.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Leif Mortenson</a>
- * @version CVS $Revision: 1.3 $ $Date: 2002/03/05 13:28:25 $
+ * @version CVS $Revision: 1.4 $ $Date: 2002/03/07 20:27:20 $
* @since 4.1
*/
public class Main
@@ -48,12 +50,12 @@
private static ProfilerComponentManager m_componentManager;
private static DefaultProfilerManager m_profilerManager;
private static ProfilerFrame m_profilerFrame;
-
+
/*---------------------------------------------------------------
* Constructors
*-------------------------------------------------------------*/
private Main() {}
-
+
/*---------------------------------------------------------------
* Methods
*-------------------------------------------------------------*/
@@ -67,40 +69,40 @@
DefaultContext context = new DefaultContext();
// Add any context variables here.
context.makeReadOnly();
-
+
// Create a ConfigurationBuilder to parse the config files.
DefaultConfigurationBuilder builder = new
DefaultConfigurationBuilder();
-
+
// Load in the configuration files
Configuration logKitConfig = builder.build( "../conf/logkit.xml"
);
Configuration profilerConfig = builder.build(
"../conf/profiler.xml" );
Configuration rolesConfig = builder.build( "../conf/roles.xml"
);
Configuration componentsConfig = builder.build(
"../conf/components.xml" );
-
+
// Setup the LogKitManager
DefaultLogKitManager logManager = new DefaultLogKitManager();
Logger lmLogger = Hierarchy.getDefaultHierarchy().
getLoggerFor( logKitConfig.getAttribute( "logger", "lm" ) );
- lmLogger.setPriority(
+ lmLogger.setPriority(
Priority.getPriorityForName( logKitConfig.getAttribute(
"log-level", "INFO" ) ) );
logManager.setLogger( lmLogger );
logManager.configure( logKitConfig );
-
+
// Set up the Profiler Manager
m_profilerManager = new DefaultProfilerManager();
m_profilerManager.enableLogging( new LogKitLogger(
logManager.getLogger( "pm" ) ) );
m_profilerManager.configure( profilerConfig );
m_profilerManager.initialize();
-
+
// Setup the RoleManager
DefaultRoleManager roleManager = new DefaultRoleManager();
- roleManager.setLogger(
+ roleManager.setLogger(
logManager.getLogger( rolesConfig.getAttribute( "logger", "rm" )
) );
roleManager.configure( rolesConfig );
-
+
// Set up the ComponentManager
m_componentManager = new ProfilerComponentManager();
- m_componentManager.setLogger(
+ m_componentManager.setLogger(
logManager.getLogger( componentsConfig.getAttribute( "logger",
"cm" ) ) );
m_componentManager.setLogKitManager( logManager );
m_componentManager.contextualize( context );
@@ -108,11 +110,11 @@
m_componentManager.setRoleManager( roleManager );
m_componentManager.configure( componentsConfig );
m_componentManager.initialize();
-
+
// Set up the ProfilerFrame
m_profilerFrame = new ProfilerFrame( m_profilerManager, "Example
Profiler" );
m_profilerFrame.setVisible( true );
-
+
try
{
File desktopFile = new File( "../conf/altprofile.desktop" );
@@ -123,7 +125,7 @@
System.out.println( "Unable to load desktop file: " + e );
}
}
-
+
/*---------------------------------------------------------------
* Main method
*-------------------------------------------------------------*/
@@ -134,10 +136,10 @@
throws Exception
{
System.out.println( "Running the AltProfile Example Application" );
-
+
// Create the ComponentManager
createComponentManager();
-
+
// Get a reference to the example component.
ExampleProfilable profilable =
(ExampleProfilable)m_componentManager.lookup(
ExampleProfilable.ROLE );
@@ -150,20 +152,41 @@
BufferedReader in = new BufferedReader( new
InputStreamReader( System.in ) );
System.out.print( " : " );
String cntStr = in.readLine();
-
+
// Can get a null if CTRL-C is hit.
if ( ( cntStr == null ) || ( cntStr.equalsIgnoreCase( "q" )
) )
{
quit = true;
}
+ else if ( ( cntStr.equalsIgnoreCase( "gc" ) ) )
+ {
+ System.gc();
+ }
else
{
try
{
+ int concurrent = 100;
+ ThreadBarrier barrier = new ThreadBarrier(
concurrent );
int cnt = Integer.parseInt( cntStr );
- for ( int i = 0; i < cnt; i++ )
+ int average = Math.max(cnt / concurrent, 1);
+
+ while (cnt > 0)
{
- profilable.doAction();
+ Thread t = new Thread( new ActionRunner(
profilable,
+ Math.min(average,
cnt),
+ barrier) );
+ t.start();
+
+ if (cnt > 0)
+ {
+ cnt -= average;
+ }
+
+ if (cnt < 0)
+ {
+ cnt = 0;
+ }
}
}
catch ( NumberFormatException e ) {}
@@ -175,24 +198,52 @@
// Release the component
m_componentManager.release( profilable );
profilable = null;
-
+
// Hide the frame
m_profilerFrame.setVisible( false );
m_profilerFrame.dispose();
m_profilerFrame = null;
-
+
// Dispose the ComponentManager
m_componentManager.dispose();
m_componentManager = null;
-
+
// Dispose the ProfilerManager
m_profilerManager.dispose();
m_profilerManager = null;
}
-
+
System.out.println();
System.out.println( "Exiting..." );
System.exit(0);
+ }
+
+ private final static class ActionRunner implements Runnable
+ {
+ private final int m_numIterations;
+ private final ExampleProfilable m_profilable;
+ private final ThreadBarrier m_barrier;
+
+ protected ActionRunner( ExampleProfilable profilable, int
numIterations, ThreadBarrier barrier )
+ {
+ m_numIterations = numIterations;
+ m_profilable = profilable;
+ m_barrier = barrier;
+ }
+
+ public void run()
+ {
+ for ( int i = 0; i < m_numIterations; i++ )
+ {
+ m_profilable.doAction();
+ }
+
+ try
+ {
+ m_barrier.barrierSynchronize();
+ }
+ catch (Exception e) {}
+ }
}
}
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>