/* JMeter Pacing Timer v2
 * Dit script zorgt ervoor dat iteraties (loops) elke keer na dezelfde tijd starten (ongeacht hoelang de transacties (samplers) duren).
 * Dus als er iedere 10 min een iteratie moet starten en het script duurt 6 minuten dan zorgt dit script automatisch voor een dynamische wachttijd van 4 min)
 * 
 * Om dit script te gebruiken moet men in JMeter twee keer een JSR223 timer aanmaken (die hangen onder een test action - pause).
 * De eerste om de starttijd op te nemen en de tweede om de variabele wachttijd te implementeren.
 *
 * Bij de eerste keer timer geef je een argument mee. Bij de tweede keer (dus voor de wachttijd) geef je drie variabelen mee.
 * De werking en welke parameters/argumenten staat verder hieronder in de code beschreven.
 *
 * Author: S. Koot and A. Oost
 */


rc = 0L //dit is de uiteindelijke return waarde (van long datatype) die JMeter vereist om de vertraging te laten regelen door een script

if (args.size() == 3) {
	// Use this form for a JSR223 Timer to implement a relative timer
	// jmeter_timer <startTimeVarName> <totalTimeInMsec> <floatPartOfThisTotal>
	// 0.00 <= floatPartOfThisTotal <= 1.00
	currentTime = System.currentTimeMillis()
	StartTimeOfLoop = vars.get(args[0])
	
	if (StartTimeOfLoop == null) {
      log.info("Recording current time now  (arg=3)")
	  StartTimeOfLoop = Long.toString(currentTime)
	  vars.put(args[0], StartTimeOfLoop)
	}
	
	sessionTime = Long.parseLong(args[1]) //sessietijd
	percentage = Float.parseFloat(args[2])//hiermee kun je de sessietijd verlengen/verkorten , 0.5 zorgt dat de sessietijd gehalveerd wordt
	deltaT = currentTime - Long.parseLong(StartTimeOfLoop) //verschil tussen eindtijd en starttijd in ms, hoe lang duurde deze loop/iteratie
	
	rc = (Long)(percentage * (Float)sessionTime) - (deltaT % sessionTime)
	//de variabele wachttijd wordt sessietijd - tijdsduur loop
	//dus wanneer sessietijd = 10 sec en script duurde 3 sec, dan wachten we 7 sec.
	//de sessietijd wordt hier verlengd of verkort
	//er wordt gebruik gemaakt van % (modulo/remainder), dit zorgt ervoor dat, wanneer een loop langer duurt dan de totaal ingestelde sessietijd, er nog een ronde wordt gewacht
	//dus wanneer sessietijd 10 sec is maar script duur 12 sec dan wachten we nog 8 sec.
	
	// if < 0; we've missed this timer; skip this session
	if (rc < 0) {
		log.error(Long.toString(rc))
		rc += sessionTime
	}
} else if (args.size() == 2 && !prev.isSuccessful()) {
	// Use this form for a JSR223 Assertion to sleep the full totalTimeInMsec since startTimeVarName value ico a sampler error
	// jmeter_timer <startTimeVarName> <totalTimeInMsec>
	currentTime = System.currentTimeMillis()
	lastRecordedTime = vars.get(args[0])
	if (lastRecordedTime == null) {
	  log.info("Recording current time now (arg=2)")
	  lastRecordedTime = Long.toString(currentTime)
	  vars.put(args[0], lastRecordedTime)
	}
	sessionTime = Long.parseLong(args[1])
	deltaT = currentTime - Long.parseLong(lastRecordedTime)
	rc = sessionTime - (deltaT % sessionTime)
	if (rc > 0L) sleep(rc)
	return
}
Integer wachttijdInSec = rc/1000;
//Integer wachttijdInSec = 123;
log.info("De wachttijd voor thread "+ctx.getThreadNum()+" bedraagt: "+wachttijdInSec+"s")
if (rc > 0L) return rc
else return 0L 

Groovy online compiler

Write, Run & Share Groovy code online using OneCompiler's Groovy online compiler for free. It's one of the robust, feature-rich online compilers for Groovy language, running the latest Groovy version 2.6. Getting started with the OneCompiler's Groovy editor is easy and fast. The editor shows sample boilerplate code when you choose language as Groovy and start coding.

Read inputs from stdin

OneCompiler's Groovy online editor supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample Groovy program which takes name as input and prints hello message with your name.

def name = System.in.newReader().readLine()
println "Hello " + name

About Groovy

Groovy is an object-oriented programming language based on java. Apache Groovy is a dynamic and agile language which is similar to Python, Ruby, Smalltalk etc.

Key Features

  • It's not a replacement for java but it's an enhancer to Java with extra features like DSL support, dynamic typing, closures etc.
  • Accepts Java code as it extends JDK
  • Greater flexibility
  • Concise and much simpler compared to Java
  • Can be used as both programming language and scripting language.

Syntax help

Data Types

Data typeDescriptionRange
StringTo represent text literalsNA
charTo represent single character literalNA
intTo represent whole numbers-2,147,483,648 to 2,147,483,647
shortTo represent short numbers-32,768 to 32,767
longTo represent long numbers-9,223,372,036,854,775,808 to +9,223,372,036,854,775,807
doubleTo represent 64 bit floating point numbers4.94065645841246544e-324d to 1.79769313486231570e+308d
floatTo represent 32 bit floating point numbers1.40129846432481707e-45 to 3.40282346638528860e+38
byteTo represent byte value-128 to 127
booleanTo represent boolean values either true or falseTrue or False

Variables

You can define variables in two ways

Syntax:

data-type variable-name;

[or]

def variable-name;

Loops

0.upto(n) {println "$it"}

or

n.times{println "$it"}

where n is the number of loops and 0 specifies the starting index

Decision-Making

1. If / Nested-If / If-Else:

When ever you want to perform a set of operations based on a condition or set of conditions, then If / Nested-If / If-Else is used.

if(conditional-expression) {
  // code
} else {
  // code
}

2. Switch:

Switch is an alternative to If-Else-If ladder and to select one among many blocks of code.

switch(conditional-expression) {    
case value1:    
 // code    
 break;  // optional  
case value2:    
 // code    
 break;  // optional  
...    
    
default:     
 //code to be executed when all the above cases are not matched;    
} 

List

List allows you to store ordered collection of data values.

Example:

def mylist = [1,2,3,4,5];
List MethodsDescription
size()To find size of elements
sort()To sort the elements
add()To append new value at the end
contains()Returns true if this List contains requested value.
get()Returns the element of the list at the definite position
pop()To remove the last item from the List
isEmpty()Returns true if List contains no elements
minus()This allows you to exclude few specified elements from the elements of the original
plus()This allows you to add few specified elements to the elements of the original
remove()To remove the element present at the specific position
reverse()To reverse the elements of the original List and creates new list