Skip to content

Commit

Permalink
add access to loadGeneratorBuilder within the groovy script
Browse files Browse the repository at this point in the history
Signed-off-by: olivier lamy <[email protected]>
  • Loading branch information
olamy committed Jul 27, 2017
1 parent dc10ce0 commit ddcba4b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import groovy.lang.Binding;
import groovy.lang.GroovyShell;
import org.codehaus.groovy.control.CompilerConfiguration;
import org.eclipse.jetty.client.api.Request;
Expand All @@ -41,6 +42,8 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;

Expand Down Expand Up @@ -69,19 +72,20 @@ public void run()
throws Exception
{

Resource resourceProfile = getResource();

LoadGenerator.Builder loadGeneratorBuilder = new LoadGenerator.Builder() //
.host( starterArgs.getHost() ) //
.iterationsPerThread( starterArgs.getRunIteration() ) //
.usersPerThread( starterArgs.getUsers() ) //
.resourceRate( starterArgs.getTransactionRate() ) //
.httpClientTransportBuilder( httpClientTransportBuilder() ) //
.sslContextFactory( sslContextFactory() ) //
.resource( resourceProfile ) //
.warmupIterationsPerThread( starterArgs.getWarmupNumber() ) //
.scheme( starterArgs.getScheme() ); //

Resource resourceProfile = getResource( loadGeneratorBuilder );

loadGeneratorBuilder.resource( resourceProfile );

if ( starterArgs.getPort() > 0 )
{
loadGeneratorBuilder.port( starterArgs.getPort() );
Expand Down Expand Up @@ -195,7 +199,7 @@ public AbstractLoadGeneratorStarter executorService( ExecutorService executor )
return this;
}

public Resource getResource()
public Resource getResource( LoadGenerator.Builder loadGeneratorBuilder )
throws Exception
{
if ( resource != null )
Expand Down Expand Up @@ -225,7 +229,9 @@ public Resource getResource()

try (Reader reader = Files.newBufferedReader( profilePath ))
{
return resource = (Resource) evaluateScript( reader );
Map<String, Object> context = new HashMap<>( );
context.put( "loadGeneratorBuilder", loadGeneratorBuilder );
return resource = (Resource) evaluateScript( reader, context );
}
}

Expand All @@ -251,14 +257,16 @@ protected static String writeAsJsonTmp( Resource resource )
return tmpPath.toString();
}

protected static Object evaluateScript( Reader script )
protected static Object evaluateScript( Reader script, Map<String, Object> context )
throws Exception
{
CompilerConfiguration config = new CompilerConfiguration( CompilerConfiguration.DEFAULT );
config.setDebug( true );
config.setVerbose( true );
Binding binding = new Binding( context );

GroovyShell interpreter = new GroovyShell(binding, config );

GroovyShell interpreter = new GroovyShell( config );

return interpreter.evaluate( script );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.atomic.AtomicInteger;
Expand Down Expand Up @@ -202,7 +203,7 @@ public void json_serial_deserial_from_groovy()
{
try (Reader reader = Files.newBufferedReader( Paths.get( "src/test/resources/loadgenerator_profile.groovy" ) ))
{
Resource resource = (Resource) AbstractLoadGeneratorStarter.evaluateScript( reader );
Resource resource = (Resource) AbstractLoadGeneratorStarter.evaluateScript( reader, Collections.emptyMap() );
String path = AbstractLoadGeneratorStarter.writeAsJsonTmp( resource );
Resource fromJson = AbstractLoadGeneratorStarter.evaluateJson( Paths.get( path ) );
Assert.assertEquals( resource.descendantCount(), fromJson.descendantCount() );
Expand Down

0 comments on commit ddcba4b

Please sign in to comment.