1

I'm trying to create a test plan for rate-limiting behavior. I set a rule that blocks after X requests per minute, and I want to check that I get response code 200 until I reached the X requests, and from then, to get 429. I created a counter that shared between all the threads, but it seems to be a mess because it's not a thread-safe.

This is my beanshell "once only controller":

String props_pre_fix = ${section_id} + "-" + ${START.HMS};
props.remove("props_pre_fix" + ${section_id}, props_pre_fix);
props.put("props_pre_fix" + ${section_id}, props_pre_fix);

props.put(props_pre_fix + "_last_response_code", "200");
props.put(props_pre_fix + "_my_counter", "0");

and this is the beanshell assertion:

String props_pre_fix = props.get("props_pre_fix" + ${section_id});
//log.info("props_pre_fix " + props_pre_fix);

//extract my counter from props
int my_counter = Integer.parseInt(props.get(props_pre_fix + "_my_counter"));

//extract last response code
String last_response_code = props.get(props_pre_fix + "_last_response_code");
log.info("last_response_code " + last_response_code);

//if last seconds is greater than current seconds it means we are in a new minute - set counter to zero
if(last_response_code.equals("429") && ResponseCode.equals("200")){
    log.info("we moved to a new minute - my_counter should be zero");
    my_counter = 0;
}

//increase counter
my_counter++;
log.info("set counter with value: " + my_counter);
//save counter
props.put(props_pre_fix + "_my_counter", my_counter + "");
log.info("counter has set with value: " + my_counter);

if (ResponseCode.equals("200")) {
    props.put(props_pre_fix + "_last_response_code", "200");
    if(my_counter <= ${current_limit}){
        Failure = false;
    } 
    else {
        Failure = true;
        FailureMessage = "leakage of " + (my_counter - ${current_limit}) + " requests";
    }
} 
else if (ResponseCode.equals("429")) {
    props.put(props_pre_fix + "_last_response_code", "429");
     if(my_counter > ${current_limit}){
            Failure = false;
    } 
}

I'm using props to share the counter, but I obviously feel that this is not the right way to do it. Can you suggest me how to do that?

Yair Cohen
  • 417
  • 4
  • 16
  • 1
    see https://stackoverflow.com/questions/51384973/jmeter-what-is-difference-between-use-or-vars-get-to-get-value-of-variable/51385152#51385152 – Ori Marko Jul 21 '20 at 07:03

1 Answers1

0

I don't think that it is possible to automatically test this requirement using JMeter Assertions because you don't have access to the current throughput so I would rather recommend considering cross-checking Response Codes per Second and Transactions per Second charts (can be installed using JMeter Plugins Manager)

All the 200 and 429 responses can be marked as successful using Response Assertion configured like:

enter image description here

If for some reason you still want to do this programmatically you might want to take a look at Summariser class source which is used for displaying current throughput in the STDOUT.

Also be informed that starting from JMeter 3.1 you should be using JSR223 Test Elements and Groovy language for scripting.

Dmitri T
  • 159,985
  • 5
  • 83
  • 133