Groovy library
Create a custom response time measurement from a plan
The idea here is to create and insert directly a custom response time measurement using Groovy from the plan itself. A response time custom measurement is represented by a map of String and Object and requires below fields fulfilled into the map to be inserted :
- begin : the beginning of the measurement point as a Unix Epoch value (ms) as an Integer
- value : the measurement point duration in ms as an Integer
- name : the measurement name as a String
- eId : the test plan execution ID as a String
You can also add any custom attribute to your map which could give more details about the measurement.
You can find below an example of the Groovy code used to create the measurement as well as an example plan.
// Get the measurement accessor instance and create the measurement map
measurementAccessor = org.rtm.commons.MeasurementAccessor.getInstance()
Map<String, Object> map = new HashMap();
// Get current time
startTime = System.System.currentTimeMillis()
// Execute the instruction(s) you want to measure...
myCode.executeMyInstructions()
// Instructions executed
// Get current time
endTime = System.currentTimeMillis()
// Insert required fields and an additional custom attribute to the measurement map
map.put('begin', startTime );
map.put('eId', report.executionID);
map.put('value', endTime - startTime);
map.put('name', 'MyCustomTransaction');
map.put('myCustomAttribute', 'myCustomValue');
// Insert the measurement in database
measurementAccessor.sendStructuredMeasurement(map);
Select For Update (atomic transaction)
Below an example of Groovy script used to open an sql connection, update a column of the first row of a table, then close the connection.
sql = groovy.sql.Sql.newInstance('jdbc:mysql://localhost/step','step','init')
sql.execute('begin')
sql.firstRow('select myColumn from myTable for update')[0]
sql.execute('update myTable set myColumn = myColumn + 1');
sql.execute('commit')
sql.close()
Copy a file from a path to another
java.nio.file.Files.copy(java.nio.file.Paths.get(file), java.nio.file.Paths.get(file+'2'))
REST call with JSON response
response = new URL("http://localhost:8080/rest/grid/agent").text
json = new com.fasterxml.jackson.databind.ObjectMapper().readValue(response, Class.forName('java.util.List'))