Forum Discussion

JoostDG's avatar
JoostDG
Frequent Contributor
7 years ago

Script Assertion to report on performance of a GET request to file

Hi,

I'd like to share a piece of groovy script I created.

 

Use case:  Every time we run a GET request test step we want to keep track of the response timings and log them to a file.

 

 

 

import com.eviware.soapui.support.JsonUtil
String environment = messageExchange.getRequestHeaders()"Host"
String response = messageExchange.getResponseHeaders()"#status#"
TimeZone.setDefault(TimeZone.getTimeZone('CET'))
def timestamp = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())

if (response == "[HTTP/1.1 200 OK]")
{
def x = messageExchange.response.getContentAsString()
def y = JsonUtil.parseTrimmedText(x)

// define the log file path (tip: use projectpath dynamically) and seperate log files depending on the environment
def projectpath = context.expand( '${#Project#projectpath}' )
def LogFilePath = projectpath + "/logs/"
def fileName

if (environment.contains("test"))
{fileName = LogFilePath+ 'TEST - Get Notifications.txt'}
else 
if (environment.contains("beta"))
{filename = LogFilePath+ 'BETA environment - GetNotifications.txt'}

def inputFile = new File(fileName)
inputFile.append("\n" +" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
inputFile.append("\n" +"Timestamp CET:" + timestamp)
inputFile.append("\n"+environment+" - GetNotifications")
inputFile.append("\n"+"Response size :" + messageExchange.responseContent.size())
inputFile.append("\n"+"Time :" + messageExchange.getTimeTaken() +" ms")
inputFile.append("\n"+"Number of notifications: " +y.size())
inputFile.append("\n"+"Time averaged for 1 notification in ms: " + (Math.round(((messageExchange.getTimeTaken())/y.size())*1)/1 )+" ms")
}

 

Example from output:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Timestamp CET:2018-03-13 15:05:41
[notifications-api-test-XXXXXXX] - Get Notifications
Response size :1195436
Time :1603 ms
Number of notifications: 24
Time averaged for 1 notification in ms: 67 ms

No RepliesBe the first to reply