You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@jmeter.apache.org by Felix Schumacher <fe...@internetallee.de> on 2021/03/02 20:49:21 UTC

Re: Debug only after each failed sampler - Debug PostProcessor limitation

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, glinius@live.com
>> <ma...@live.com> <glinius@live.com <ma...@live.com>>
>> 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: user-unsubscribe@jmeter.apache.org
>>     <ma...@jmeter.apache.org>
>>     For additional commands, e-mail: user-help@jmeter.apache.org
>>     <ma...@jmeter.apache.org>
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@jmeter.apache.org
>> For additional commands, e-mail: user-help@jmeter.apache.org

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@jmeter.apache.org
For additional commands, e-mail: user-help@jmeter.apache.org


Re: Debug only after each failed sampler - Debug PostProcessor limitation

Posted by Nikola Aleksic <na...@gmail.com>.
Hi,

First of all sorry for the late reply.

Thank you for the answer and provided code, Felix. In the attached test
plan I put your code in JSR223Listener, I created Debug post processor with
almost complete behavior in JSR223Listener and JSR223 PostProcessor with
the combination of DebugPostProcessor.java, Glinius, and your code for the
sorting part.

If "JSR223 Listener - Debug PostProcessor behavior" is enabled only among
all JSR223 elements, it won't work as expected if Generate parent sampler
is checked in Transaction controller. As you already said - 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. If Generate parent sampler is not checked in
Transaction controller, I get almost what I wanted but I have a need in my
test plan to have Generate parent sampler checked, which, unfortunately,
doesn't work :/

If "JSR223 Listener - Felix's code" is enabled only among all JSR223
elements, with the last line "sampleResult.subResults.each(r ->
r.addSubResult(debugResult))" active, the output is desired one, but as you
said, "the listener is called only twice and thus the variables and
properties are always the same.". Changing the last line to
"sampleResult.addSubResult(debugResult)" is "more accurate to the state of
variables and properties." Conclusion is that if "Generate parent sample"
is checked, it is not possible to get desired output, if not checked, it is
possible.

I was curious how the same code will work in JSR223 PostProcessor. If
"JSR223 PostProcessor - Debug PostProcessor behavior" is enabled only among
all JSR223 elements, Debug PostProcessor behavior is reproduced. It works
in all cases like Debug PostProcessor itself - puts subsampler in each
child Passed and Failed subsampler. If part "if (prev.isSuccessful())" is
changed to "if (!prev.isSuccessful())" it won't work - no subsamplers were
created.

> If it makes sense to you, then open an enhancement issue and maybe
someone will add such a listener.

Like you said, the listener is called only twice. Since that is the
Listener's behavior and it won't work for all possible cases there is no
point to open it, I'm afraid.

Thank you both of you guys for your time and will to help. I really
appreciate it! :)

P.S. Latest nightly is required to run the test successfully.

Regards,
Nikola

On Tue, 2 Mar 2021 at 21:49, Felix Schumacher <
felix.schumacher@internetallee.de> wrote:

>
> 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, glinius@live.com
> >> <ma...@live.com> <glinius@live.com <ma...@live.com>>
> >> 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: user-unsubscribe@jmeter.apache.org
> >>     <ma...@jmeter.apache.org>
> >>     For additional commands, e-mail: user-help@jmeter.apache.org
> >>     <ma...@jmeter.apache.org>
> >>
> >>
> >> ---------------------------------------------------------------------
> >> To unsubscribe, e-mail: user-unsubscribe@jmeter.apache.org
> >> For additional commands, e-mail: user-help@jmeter.apache.org
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@jmeter.apache.org
> For additional commands, e-mail: user-help@jmeter.apache.org
>
>