Skip to content

Commit

Permalink
build Callable first before invoke fix #8
Browse files Browse the repository at this point in the history
Signed-off-by: olivier lamy <[email protected]>
  • Loading branch information
olamy committed Feb 21, 2017
1 parent 188a078 commit e94746e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
Expand Down Expand Up @@ -408,7 +409,9 @@ public void run( int transactionNumber )
{
HttpClientTransport httpClientTransport = getHttpClientTransport();

List<Future<?>> futures = new ArrayList<Future<?>>( getUsers() );


List<Callable<Void>> callables = new ArrayList<>( getUsers() );

for ( int i = getUsers(); i > 0; i-- )
{
Expand All @@ -427,7 +430,7 @@ public void run( int transactionNumber )
new LoadGeneratorRunner( httpClient, this, _loadGeneratorResultHandler, //
transactionNumber);

futures.add( this.runnersExecutorService.submit( loadGeneratorRunner ));
callables.add( loadGeneratorRunner );

}
catch ( Throwable e )
Expand All @@ -437,6 +440,8 @@ public void run( int transactionNumber )
}
}

List<Future<Void>> futures = this.runnersExecutorService.invokeAll( callables );

while ( !LoadGenerator.this.stop.get() && !futures.stream().allMatch( future -> future.isDone() ))
{
// wait until stopped
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,19 @@
import org.mortbay.jetty.load.generator.profile.Resource;

import java.net.HttpCookie;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

/**
*
*/
public class LoadGeneratorRunner
implements Runnable
implements Callable<Void>
{

private static final Logger LOGGER = Log.getLogger( LoadGeneratorRunner.class );
Expand Down Expand Up @@ -67,8 +70,9 @@ public LoadGeneratorRunner( HttpClient httpClient, LoadGenerator loadGenerator,
this.transactionNumber = transactionNumber;
}


@Override
public void run()
public Void call()
{
LOGGER.debug( "loadGenerator#run" );
try
Expand Down Expand Up @@ -118,6 +122,7 @@ public void run()
LOGGER.warn( "ignoring exception:" + e.getMessage(), e );
// TODO record error in generator report
}
return null;
}

private void handleResource( Resource resource )
Expand All @@ -139,25 +144,28 @@ private void handleResource( Resource resource )
// it's a group so we can request in parallel but wait all responses before next step
ExecutorService executorService = Executors.newWorkStealingPool();

List<Callable<Void>> callables = new ArrayList<>( resource.getResources().size() );


for ( Resource children : resource.getResources() )
{
executorService.execute( () ->
{
try
{
handleResource( children );
}
catch ( Exception e )
{
LOGGER.debug( e.getMessage(), e );
}
} );
callables.add( () ->
{
try
{
handleResource( children );
}
catch ( Exception e )
{
LOGGER.debug( e.getMessage(), e );
}
return null;
} );
}

executorService.shutdown();
List<Future<Void>> futures = executorService.invokeAll( callables, resource.getChildrenTimeout(), TimeUnit.MILLISECONDS );
boolean finished = futures.stream().allMatch( voidFuture -> voidFuture.isDone() );

// TODO make this configurable??
boolean finished = executorService.awaitTermination( resource.getChildrenTimeout(), TimeUnit.MILLISECONDS );
if ( !finished )
{
LOGGER.warn( "resourceGroup request not all completed for timeout " + resource.getChildrenTimeout() );
Expand Down

0 comments on commit e94746e

Please sign in to comment.