Best practice of Jmeter: BeahShell Server

Posted by codects on Sat, 05 Mar 2022 02:44:06 +0100

1. Introduction

Here are the official documents

The BeanShell interpreter has a very useful feature - it can act as a server, which is accessible by telnet or http.

2. Use

Using BeahShell Server, you can dynamically change the running parameters, such as the number of threads and interval time, during the running of the test plan

Scenario example:

A test plan that needs to run for a long time and has been running for several hours. If you need to modify the parameters, you can terminate the execution, set the parameters and run again, but this is unacceptable. At this time, it is very necessary to modify the parameters dynamically

3. How to use

  1. Modify JMeter propertiesPort Description: 9000 is used for http protocol access and 9001 is used for Telnet protocol access
  2. Test whether the server is normal

    Modify jmeter After the properties file, start the jmeter client. You can see the information in the figure above on the console and the information in the figure below on the command line

    jemter provides remote in the extras # directory BSH file, can be used to test. In the bin directory, execute

    java -jar ../lib/bshclient.jar localhost 9000 ../extras/remote.bsh

    Command description: use the bsh client (bshclient.jar) to send the bsh file to the beah shell server for execution through the local 9000 portThe above results show that the beanshell server works normally

  3. Take litchi for example: in a long-time test plan, continuously increase the throughput control until the target value

  • Create a test plan, as shown in the figure below

    Arbitrarily configure a simple http request. In this plan, the JSR223 post processor can be ignored in order to deal with the problem of response coding. Configure a constant throughput timer for the http request and parameterize the parameters, that is, ${_P(throughput, 6)}

  • The thread group is configured as follows, and parameterization is also used

  • Write the bean shell script and save it in the extras directory with the file name param BSH, the script content is as follows

    printprop("param");
    
    
    curr = Integer.decode(args[0]); // current value
    inc = Integer.decode(args[1]);  // incresment
    end = Integer.decode(args[2]);  // final value
    secs = Integer.decode(args[3]);  // wait 
    
    
    while(curr <= end){
        setprop("throughput", curr.toString());
        Thread.sleep(secs*1000);
        curr += inc;
    }
    
    printprop("param");

    Description: the script contains a simple loop logic, which continuously sets the value of throughput. The methods used in the file, such as printprop(), setprop(), are startup Defined in BSH file, as shown in the following figure: startup Some contents of BSH file

    print("Startup script running");
    
    import org.apache.jmeter.util.JMeterUtils;
    
    getprop(p){// get a JMeter property
    return JMeterUtils.getPropDefault(p,"");
    }
    
    setprop(p,v){// set a JMeter property
    print("Setting property '"+p+"' to '"+v+"'.");
    JMeterUtils.getJMeterProperties().setProperty(p, v);
    }
    
    printprop(p){// print a JMeter property
    print(p + " = " + getprop(p));
    }
    
    loglevel(String priority, String category){
    // Previous implementation delegated the call to LoggingManager which
    // was deprecated since JMeter 3.2
    }
    
    logdebug(String text){
    loglevel("DEBUG",text);
    }
    
    loginfo(String text){
    loglevel("INFO",text);
    }
    
    // Define routines to stop the test or a thread
    stopEngine(){// Stop the JMeter test
    print("Stop Engine called");
    org.apache.jmeter.engine.StandardJMeterEngine.stopEngine();
    }
    
    stopEngineNow(){// Stop the JMeter test now
    print("Stop Engine NOW called");
    org.apache.jmeter.engine.StandardJMeterEngine.stopEngineNow();
    }
    
    stopThread(t){// Stop a JMeter thread
    print("Stop Thread "+t+" called");
    ok=org.apache.jmeter.engine.StandardJMeterEngine.stopThread(t);
    if (ok){print("Thread requested to stop");} else { print("Thread not found");}
    }
    
    stopThreadNow(t){// Stop a JMeter thread
    print("Stop Thread Now "+t+" called");
    ok=org.apache.jmeter.engine.StandardJMeterEngine.stopThreadNow(t);
    if (ok){print("Thread stopped");} else { print("Thread not found");}
    }
    
    getsysprop(p){// get a system property
    return System.getProperty(p,"");
    }
    
    setsysprop(p,v){// set a system property
    print("Setting property '"+p+"' to '"+v+"'.");
    System.setProperty(p, v);
    }
    
    printsysprop(p){// print a system property
    print(p + " = " + getsysprop(p));
    }
    
    print("Startup script completed");
  • Run the test plan in CLI mode and execute the command in bin directory (note to start jmeter in bin directory of jmeter, otherwise extras/startup.bsh may not be loaded, and the console will throw an exception in GUI mode: could source.. / extras/startup.bsh)

    jmeter -n -t demo0304.jmx -l report_demo0304001.jtl -Jthreads=4 -Jduration=1

    Here, the - l parameter can be omitted, and the - J setting of the number of threads and start-up time is also unnecessary, which means that the default value is set in the plan

  • By execution bsh file, dynamically modify the throughput parameter to control the throughput of http request. The following is the result of script operation

  • Write after: execute above The method of bsh file is as follows

    java -jar ../lib/bshclient.jar localhost 9000 ../extras/param.bsh 12 10 60 30

    Dynamic incoming parameters are used, i.e. parameters are received from the command line. Of course, it's entirely possible not to use this method to set param The parameters defined in BSH file are modified as follows. Using fixed parameters can have the same effect, but the way of dynamically passing in parameters is more flexible

    curr = 12; // current value
    inc = 10;  // incresment
    end = 60;  // final value
    secs = 60;  // wait 

Topics: Java Tomcat Stress testing test