@Grapes([
@Grab(group='org.codehaus.gpars', module='gpars', version='1.2.1'),
@Grab(group='com.google.guava', module='guava', version='18.0'),
@GrabExclude('commons-logging:commons-logging')])
def cli = new CliBuilder(usage:'groovy loadTest.groovy ')
cli.p(longOpt:'parallelism', args: 1, argName: 'parallelism', 'The number of producers to execute in parallel. Defaults to 10.')
cli.d(longOpt:'duration', args: 1, argName: 'duration', 'The duration of the test in minutes. Defaults to 1 minute.')
cli.r(longOpt:'target-rate', args: 1, argName: 'targetRate', 'The target aggregate rate of messages to be produced per minute. Defaults to 60.')
cli.h(longOpt:'help', 'Displays the usage information for this script.')
def run = { ->
// Do the actual work here (e.g. publish a message, etc)
}
def setup = { ->
// Do any pre-test setup here (e.g. retrieving data, configuration, etc)
}
def options = cli.parse(args)
if(options?.h) {
cli.usage()
} else {
def duration = options?.d ? TimeUnit.MINUTES.toMillis(options.d as long) : TimeUnit.MINUTES.toMillis(1l)
def parallelism = options?.p ?: 10
def targetRate = options?.r ?: 60.0
println '***********************************************'
println 'Load Test'
println "Test will run for ${TimeUnit.MILLISECONDS.toMinutes(duration)} minute(s) using ${parallelism} producers with a target rate of ${targetRate} messages per minute."
setup()
println 'Starting the test....'
def rateLimiter = RateLimiter.create((targetRate as double)/TimeUnit.MINUTES.toSeconds(1))
def startTime = System.currentTimeMillis()
def total = 0
while((System.currentTimeMillis() - startTime) < duration) {
GParsExecutorsPool.withPool(parallelism as int) { ExecutorService service ->
(parallelism as int).times {
rateLimiter.acquire()
service.submit({ run() } as Runnable)
}
total += parallelism as int
}
println "Test iteration complete. Total sent so far: ${total}, ${TimeUnit.MILLISECONDS.toSeconds(duration - (System.currentTimeMillis() - startTime))} second(s) remaining."
}
println 'Test complete.'
System.exit(0)
}