Skip to content
Advertisement

How to use Throughput Shaping Timer from JMETER API using Java code?

enter image description here

1: https://i.stack.imgur.com/sRx3n.jpg**strong text**##


I am trying to write the Java program to get the input something like this for multiple rows and process it by creating concurrency thread group which can generate number of threads from tstfeedback function and complete the execution.enter image description here

Advertisement

Answer

Here is an example of creating an empty Test Plan with the Throughput Shaping Timer configured like at your first image via JMeter API:

enter image description here

import kg.apc.jmeter.JMeterPluginsUtils;
import kg.apc.jmeter.timers.VariableThroughputTimer;
import kg.apc.jmeter.timers.VariableThroughputTimerGui;
import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.config.gui.ArgumentsPanel;
import org.apache.jmeter.control.gui.TestPlanGui;
import org.apache.jmeter.gui.util.PowerTableModel;
import org.apache.jmeter.save.SaveService;
import org.apache.jmeter.testelement.TestElement;
import org.apache.jmeter.testelement.TestPlan;
import org.apache.jmeter.testelement.property.CollectionProperty;
import org.apache.jmeter.util.JMeterUtils;
import org.apache.jorphan.collections.HashTree;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

public class ThroughputShapingTImer {

    public static void main(String[] args) throws IOException {


        File jmeterHome = new File("c:/apps/jmeter");
        String slash = System.getProperty("file.separator");
        File jmeterProperties = new File(jmeterHome.getPath() + slash + "bin" + slash + "jmeter.properties");

        //JMeter initialization (properties, log levels, locale, etc)
        JMeterUtils.setJMeterHome(jmeterHome.getPath());
        JMeterUtils.loadJMeterProperties(jmeterProperties.getPath());
        JMeterUtils.initLocale();

        // JMeter Test Plan, basically JOrphan HashTree
        HashTree testPlanTree = new HashTree();


        //Throughput Shaping Timer
        VariableThroughputTimer throughputShapingTimer = new VariableThroughputTimer();
        throughputShapingTimer.setName("Timer");

        PowerTableModel load_profile = new PowerTableModel(new String[]{"Start RPS", "End RPS", "Duration, sec"}, new Class[]{String.class, String.class, String.class});
        load_profile.addRow(new Integer[]{0, 10, 20});
        load_profile.addRow(new Integer[]{10, 10, 20});
        CollectionProperty data = JMeterPluginsUtils.tableModelRowsToCollectionProperty(load_profile, VariableThroughputTimer.DATA_PROPERTY);
        throughputShapingTimer.setData(data);

        throughputShapingTimer.setProperty(TestElement.TEST_CLASS, VariableThroughputTimer.class.getName());
        throughputShapingTimer.setProperty(TestElement.GUI_CLASS, VariableThroughputTimerGui.class.getName());


        // Test Plan
        TestPlan testPlan = new TestPlan("Create JMeter Script From Java Code");
        testPlan.setProperty(TestElement.TEST_CLASS, TestPlan.class.getName());
        testPlan.setProperty(TestElement.GUI_CLASS, TestPlanGui.class.getName());
        testPlan.setUserDefinedVariables((Arguments) new ArgumentsPanel().createTestElement());

        // Construct Test Plan from previously initialized elements
        testPlanTree.add(testPlan);
        HashTree threadGroupHashTree = testPlanTree.add(testPlan);
        threadGroupHashTree.add(throughputShapingTimer);


        // save generated test plan to JMeter's .jmx file format
        SaveService.saveTree(testPlanTree, Files.newOutputStream(Paths.get("example.jmx")));
    }
}

The resulting .jmx test plan will be stored in the current folder.

More information: Five Ways To Launch a JMeter Test without Using the JMeter GUI

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement