user17071521
user17071521

Reputation:

How to print the response time in JMeter

Problem Statement: I want to print the the response time (time taken for a request) on the console using a post processer in JMeter, How to do it ?

I have tried with JSR223 post processer, using the following code.

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.util.EntityUtils;
import org.apache.jmeter.samplers.SampleResult;


log.info("--------------->",sampleResult.getTime())

enter image description here And I am getting

2021-11-09 15:56:55,632 ERROR o.a.j.e.JSR223PostProcessor: Problem in JSR223 script, JSR223 PostProcessor
javax.script.ScriptException: groovy.lang.MissingPropertyException: No such property: sampleResult for class: Script2
    at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:320) ~[groovy-jsr223-3.0.3.jar:3.0.3]
    at org.codehaus.groovy.jsr223.GroovyCompiledScript.eval(GroovyCompiledScript.java:71) ~[groovy-jsr223-3.0.3.jar:3.0.3]
    at javax.script.CompiledScript.eval(Unknown Source) ~[?:1.8.0_251]
    at org.apache.jmeter.util.JSR223TestElement.processFileOrScript(JSR223TestElement.java:222) ~[ApacheJMeter_core.jar:5.3]
    at org.apache.jmeter.extractor.JSR223PostProcessor.process(JSR223PostProcessor.java:45) [ApacheJMeter_components.jar:5.3]
    at org.apache.jmeter.threads.JMeterThread.runPostProcessors(JMeterThread.java:940) [ApacheJMeter_core.jar:5.3]
    at org.apache.jmeter.threads.JMeterThread.executeSamplePackage(JMeterThread.java:572) [ApacheJMeter_core.jar:5.3]
    at org.apache.jmeter.threads.JMeterThread.processSampler(JMeterThread.java:489) [ApacheJMeter_core.jar:5.3]
    at org.apache.jmeter.threads.JMeterThread.run(JMeterThread.java:256) [ApacheJMeter_core.jar:5.3]
    at java.lang.Thread.run(Unknown Source) [?:1.8.0_251]
Caused by: groovy.lang.MissingPropertyException: No such property: sampleResult for class: Script2
    at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:65) ~[groovy-3.0.3.jar:3.0.3]
    at org.codehaus.groovy.runtime.callsite.PogoGetPropertySite.getProperty(PogoGetPropertySite.java:51) ~[groovy-3.0.3.jar:3.0.3]
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGroovyObjectGetProperty(AbstractCallSite.java:341) ~[groovy-3.0.3.jar:3.0.3]
    at Script2.run(Script2.groovy:10) ~[?:?]
    at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:317) ~[groovy-jsr223-3.0.3.jar:3.0.3]
    ... 9 more

Upvotes: 0

Views: 1324

Answers (1)

Dmitri T
Dmitri T

Reputation: 168197

  1. You don't have sampleResult object in JSR223 PostProcessor
  2. If you want to print the response time of the previous sampler - you need to use prev shorthand for the SampleResult class instance
  3. You need to use string concatenation

So remove all the imports, they're not needed and amend your code to look like:

log.info("---------------> " + prev.getTime())

More information: Top 8 JMeter Java Classes You Should Be Using with Groovy

Upvotes: 1

Related Questions