Skip to content

How to dynamically create samples #5733

Closed

Description

@frgirard

The documentation URL

https://jmeter.apache.org/usermanual/component_reference.html#JSR223_Sampler

Feedback

I need a JSR223 groovy script to be able to dynamically create samples. This script receives kafka records which have headers including processing times. I'd like to be able to create samples using these timestamp info.
In my script, I create sampleresults :

def sr = new org.apache.jmeter.samplers.SampleResult(t_in, t_out - t_in)

But there's no way I know to add this sampleresult to JMeter results (.jtl, listeners...).
How could I do that ?
Thanks a lot !

Activity

  1. FSchumacher commented on Nov 16, 2022

    @FSchumacher
    Contributor

    Have you thought about adding those samples as sub results into your given SampleResult?

    https://jmeter.apache.org/api/org/apache/jmeter/samplers/SampleResult.html#addSubResult(org.apache.jmeter.samplers.SampleResult)

  2. frgirard commented on Nov 16, 2022

    @frgirard
    Author

    Yes I did, but there are 3 issues with this method :

    • I cannot setIgnore() the groovy script generating the subresults, hence the jtl file is "polluted" with these samples
    • The subresults are not considered by some listeners like summary report
    • subresults have different names (-0, -1, -2 ... is appended to the sampler label) while I'm trying to aggregate them
  3. FSchumacher commented on Nov 16, 2022

    @FSchumacher
    Contributor

    First point might be annoying, but it is critical? Report could ignore those sample, could not it?
    For the second point. Do you think those listeners should be enhanced to support subsamples?

    Another way of implementing creation of samples would be by setting an array like object in the sampler and using a while/loop controller afterwards to iterate over the values of the array, using those values to fill in samplers.

  4. frgirard commented on Nov 16, 2022

    @frgirard
    Author

    First point is quite critical because I won't be able to postprocess the jtl file (blazemeter)

    About the alternate way, do you suggest I use a loop controler then alter each sample created with the values I want ? I'm not sure I can alter the sample start time for instance.

  5. FSchumacher commented on Nov 16, 2022

    @FSchumacher
    Contributor

    In Groovy space, you can do quite a lot. I would try it out. But it should be possible to use SampleResult#setStartTime(long) on the current sample.

  6. frgirard commented on Nov 17, 2022

    @frgirard
    Author

    Ok it seems to work, but I had to put the loop in a teardown threadgroup.
    I'm using SampleResult.setStartTime() and SampleResult.setEndTime().

    However, it rises an exception on every loop :

    2022-11-17 12:02:39,362 ERROR o.a.j.s.SampleResult: sampleEnd called twice java.lang.Throwable: Invalid call sequence at org.apache.jmeter.samplers.SampleResult.sampleEnd(SampleResult.java:1147) ~[ApacheJMeter_core.jar:5.5] at org.apache.jmeter.protocol.java.sampler.JSR223Sampler.sample(JSR223Sampler.java:82) ~[ApacheJMeter_java.jar:5.5] at org.apache.jmeter.threads.JMeterThread.doSampling(JMeterThread.java:651) ~[ApacheJMeter_core.jar:?] at org.apache.jmeter.threads.JMeterThread.executeSamplePackage(JMeterThread.java:570) ~[ApacheJMeter_core.jar:?] at org.apache.jmeter.threads.JMeterThread.processSampler(JMeterThread.java:501) ~[ApacheJMeter_core.jar:?] at org.apache.jmeter.threads.JMeterThread.run(JMeterThread.java:268) ~[ApacheJMeter_core.jar:?] at java.lang.Thread.run(Thread.java:750) ~[?:1.8.0_345]
    Any idea on how I could fix that ?

  7. FSchumacher commented on Nov 18, 2022

    @FSchumacher
    Contributor

    Yes, the exception comes from JSR223Sampler calling SampleResult#sampleEnd() without checking, if the sampler has set a timestamp (not equal to 0) first. Should probably fixed.

  8. frgirard commented on Nov 30, 2022

    @frgirard
    Author

    Thanks a lot for your help, @FSchumacher !

  9. added a commit that references this issue on Mar 24, 2024
    2da0eb5
  10. PavanMudigondaTR commented on Apr 24, 2024

    @PavanMudigondaTR

    @frgirard Could you please provide a sample on how you were able to do this "How to dynamically create samples
    "?

    I opened a new issue but no one responsed #6273

  11. frgirard commented on Apr 25, 2024

    @frgirard
    Author

    I'm using a dedicated JSR223 sampler into a loop controller, to create the samples I need. The values (start time, end time) are pushed by the main sampler to a java.util.concurrent.ConcurrentLinkedQueue and used by this sampler. All other samplers are setIgnore(). The main sampler is multithreaded, so it is important to use data structures supporting concurrent access.
    It looks like that :

    ConcurrentHashMap<String, String> trep = props.get "map.trep"
    ConcurrentLinkedQueue<String> rq = props.get "reception.queue"
    AtomicInteger skutraites = props.get "sku.count"
    
    String key = rq.poll()
    
    if (key)
    {
      String[] val = trep.get(key).split(",")
      trep.remove(key)
      t_in = Long.parseLong(val[0])
      t_out = Long.parseLong(val[1])
      taille = Integer.parseInt(val[2])
      type = val[3]
    
      skutraites.addAndGet(1)
    
      //log.info "Creating sample ${key}, ${t_in}, ${t_out}, ${taille}"
    
      SampleResult.setSampleLabel(type + "_aggreg")
      SampleResult.setStartTime(t_in)
      SampleResult.setEndTime(t_out)
      //SampleResult.setLatency(latency)
      SampleResult.setBytes(taille as long)
      SampleResult.setResponseMessage(key)
    }
    else
    {
      SampleResult.setIgnore()
      sleep 100
    }
    
  12. PavanMudigondaTR commented on Apr 25, 2024

    @PavanMudigondaTR

    @frgirard thank you so much for your quick response. if its not too much to ask could you please provide a example JMX script with above script so i can better understand how main JSR223 is working with this logic together to produce dynamic labels ?

  13. PavanMudigondaTR commented on May 20, 2024

    @PavanMudigondaTR

    @frgirard just wanted to check again if you could help me with above request ! I have a bit of hard time doing this ! any guidance is appreciated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions