Am 28.02.21 um 12:02 schrieb Felix Schumacher:
> Am 25.02.21 um 20:15 schrieb Nikola Aleksic:
>> Thank you for trying to help, Glinus.
>>
>> When I put your code in JSR223 Listener, subsampler is not created in
>> each Passed and Failed samplers as it should be. Also,
>> SamplerProperties and JMeterVariables are not sorted in ascending
>> order like it is in Debug PostProcessor. Check an image in the
>> attachment for more details.
>>
>> At least you managed to create a subsampler which is a step forward :)
>> I'm still looking for a complete solution.
> I haven't understood completely , what you are missing or what you want
> to achieve. Can you share your test plan and describe, what you want to
> see in the tree result viewer?
>
> I tried to do a simple listener, that logs all samples and their
> subresults with the variables at the time the listener is called:
>
> import org.apache.jorphan.util.AlphaNumericKeyComparator
>
> def logSample(result, level=0) {
>     log.info(("  " * level) + result + " => " + result.success)
>     result.subResults.each(r -> logSample(r, level + 1))
> }
>
> def logVars() {
>     def s = new ArrayList(vars.entrySet())
>     s.sort(AlphaNumericKeyComparator.INSTANCE)
>     log.info("  vars: " + s)
> }
>
> logSample(sampleResult)
> logVars()
>
> Is this logging everything you want?

After re-reading your first mail and integrating the work from glinius,
I came up with the following code for the listener:

import org.apache.jorphan.util.AlphaNumericKeyComparator
import org.apache.jmeter.samplers.SampleResult

newline = System.getProperty('line.separator')

def sortedList(entries) {
    def result = new ArrayList(entries)
    result.sort(AlphaNumericKeyComparator.INSTANCE)
    return result
}

def appendEntries(entries, sb) {
    sortedList(entries).each(entry ->
        sb.append(entry.key).append('=').append(entry.value).append(newline)
    )
}

if (!prev.successful) {
    log.info("add debug-samplers: " + sampleResult + " : " +
sampleResult.subResults )
    def debugResult = new SampleResult()
    debugResult.successful = true
    def responseBody = new StringBuilder()

    responseBody.append('# SamplerProperties:').append(newline)
    appendEntries(sampler.properties.entrySet(), responseBody)

    responseBody.append(newline).append('#
JMeterVariables:').append(newline)
    appendEntries(vars.entrySet(), responseBody)

    debugResult.setResponseData(responseBody as String, 'UTF-8')
    sampleResult.subResults.each(r -> r.addSubResult(debugResult))
}

With this, you get a sub-sampler in each failed sampler. But note, that
the listener is called only twice and thus the variables and properties
are always the same.

The listener will be called once for the "Transaction Controller Failed"
(which has "Generate parent sample" enabled and where the sub-results
are actually added) and once for "Transaction Controller Main Failed"
where no sub-results are added.

I think it would be better to change the last line in the listener to

    sampleResult.addSubResult(debugResult)

That way, you get one sub-sample per (the two mentioned above)
Controller, but it is more accurate to the state of variables and
properties.

If you want to have the listener called for each Sampler, you will have
to un-check the "Generate parent sample"

Felix

>
> Felix
>
>> On Thu, 25 Feb 2021 at 15:53, [email protected]
>> <mailto:[email protected]> <[email protected] <mailto:[email protected]>>
>> wrote:
>>
>>     Here is example code you could use in the  JSR223 Listener
>>     
>> <https://jmeter.apache.org/usermanual/component_reference.html#JSR223_Listener
>>     
>> <https://jmeter.apache.org/usermanual/component_reference.html#JSR223_Listener>>
>>
>>     , it produces more or less the same output as the Debug PostProcessor:
>>
>>
>>     > def subResult = new org.apache.jmeter.samplers.SampleResult()
>>     > subResult.setSuccessful(true)
>>     > def responseBody = new StringBuilder()
>>     > def newline = System.getProperty('line.separator')
>>     >
>>     > responseBody.append('SamplerProperties:').append(newline)
>>     >
>>     > sampler.getProperties().each { property ->
>>     >   
>>     >
>>     
>> responseBody.append(property.getKey()).append('=').append(property.getValue()).append(newline)
>>     > }
>>     >
>>     > responseBody.append('JMeterVariables:').append(newline)
>>     > vars.entrySet().each { var ->
>>     >   
>>     >
>>     
>> responseBody.append(var.getKey()).append('=').append(var.getValue()).append(newline)
>>     > }
>>     >
>>     > subResult.setResponseData(responseBody as String, 'UTF-8')
>>     >
>>     > prev.addSubResult(subResult)
>>
>>     In the above code:
>>
>>     *prev* - stands for parent  SampleReuslt
>>     
>> <https://jmeter.apache.org/api/org/apache/jmeter/samplers/SampleResult.html
>>     
>> <https://jmeter.apache.org/api/org/apache/jmeter/samplers/SampleResult.html>>
>>  
>>
>>     *vars* - stands for  JMeterVariables
>>     
>> <https://jmeter.apache.org/api/org/apache/jmeter/threads/JMeterVariables.html
>>     
>> <https://jmeter.apache.org/api/org/apache/jmeter/threads/JMeterVariables.html>>
>>
>>     class instance
>>
>>     See  Top 8 JMeter Java Classes You Should Be Using with Groovy
>>     
>> <https://www.blazemeter.com/blog/top-8-jmeter-java-classes-you-should-be-using-with-groovy
>>     
>> <https://www.blazemeter.com/blog/top-8-jmeter-java-classes-you-should-be-using-with-groovy>>
>>  
>>
>>     article to learn more about the above and other JMeter API shorthands
>>     available for the JSR223 test elements .
>>
>>
>>
>>     --
>>     Sent from: http://www.jmeter-archive.org/JMeter-User-f512775.html
>>     <http://www.jmeter-archive.org/JMeter-User-f512775.html>
>>
>>     ---------------------------------------------------------------------
>>     To unsubscribe, e-mail: [email protected]
>>     <mailto:[email protected]>
>>     For additional commands, e-mail: [email protected]
>>     <mailto:[email protected]>
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: [email protected]
>> For additional commands, e-mail: [email protected]

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to