Как использовать Jedis в создании JMeter тест плана?

247
22 июня 2017, 03:29

У меня есть два куска кода, я не знаю, как их соединить.

== JMeter code ==

    // Engine
    StandardJMeterEngine jm = new StandardJMeterEngine();
    // jmeter.properties
    JMeterUtils.loadJMeterProperties("c:/tmp/jmeter.properties");
    HashTree hashTree = new HashTree();     
    // HTTP Sampler
    HTTPSampler httpSampler = new HTTPSampler();
    httpSampler.setDomain("www.google.com");
    httpSampler.setPort(80);
    httpSampler.setPath("/");
    httpSampler.setMethod("GET");
    // Loop Controller
    TestElement loopCtrl = new LoopController();
    ((LoopController)loopCtrl).setLoops(1);
    ((LoopController)loopCtrl).addTestElement(httpSampler);
    ((LoopController)loopCtrl).setFirst(true);
    // Thread Group
    SetupThreadGroup threadGroup = new SetupThreadGroup();
    threadGroup.setNumThreads(1);
    threadGroup.setRampUp(1);
    threadGroup.setSamplerController((LoopController)loopCtrl);
    // Test plan
    TestPlan testPlan = new TestPlan("MY TEST PLAN");
    hashTree.add("testPlan", testPlan);
    hashTree.add("loopCtrl", loopCtrl);
    hashTree.add("threadGroup", threadGroup);
    hashTree.add("httpSampler", httpSampler);       
    jm.configure(hashTree);
    jm.run();

== Jedis code ==

private static final String LOCAL_HOST = "localhost";
private static final int PORT = 6379;
private final JedisPool m_jedisPool;
private Jedis m_jedis;
m_jedisPool = new JedisPool(new JedisPoolConfig(), LOCAL_HOST, PORT)
void test() {
        m_jedis = m_jedisPool.getResource();
        m_jedis.flushAll();
        // Run test
       Jedis jedis = getExistingJedisInstance();
       jedis.set(TEST_KEY, TEST_VALUE).equals("OK");
        // After test;
        m_jedis.close();
        m_jedisPool.destroy();
    }

Использование плагина Redis Data Set не подходит, потому что он не поддерживает set комманды. Помогите, плиз.

Answer 1

You can add a JSR223 Sampler and put your Jedis code there (just make sure you have all the necessary dependency .jars in the classpath)

The relevant code to add the sampler to the Test Plan programatically would look something like:

import java.io.File;
import java.io.FileOutputStream;
import java.util.UUID;
import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.config.gui.ArgumentsPanel;
import org.apache.jmeter.control.LoopController;
import org.apache.jmeter.control.gui.LoopControlPanel;
import org.apache.jmeter.control.gui.TestPlanGui;
import org.apache.jmeter.engine.StandardJMeterEngine;
import org.apache.jmeter.protocol.java.sampler.JSR223Sampler;
import org.apache.jmeter.reporters.ResultCollector;
import org.apache.jmeter.reporters.Summariser;
import org.apache.jmeter.save.SaveService;
import org.apache.jmeter.testbeans.gui.TestBeanGUI;
import org.apache.jmeter.testelement.TestElement;
import org.apache.jmeter.testelement.TestPlan;
import org.apache.jmeter.threads.ThreadGroup;
import org.apache.jmeter.threads.gui.ThreadGroupGui;
import org.apache.jmeter.util.JMeterUtils;
import org.apache.jmeter.util.ScriptingTestElement;
import org.apache.jorphan.collections.HashTree;

public class JedisExample {
    public static void main(String[] argv) throws Exception {
        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 Engine
        StandardJMeterEngine jmeter = new StandardJMeterEngine();
        //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();
        JSR223Sampler jedisSampler = new JSR223Sampler();
        jedisSampler.setName("Jedis Sampler");
        jedisSampler.setProperty("script", "def LOCAL_HOST = \"localhost\"\n"
                + "def PORT = 6379\n"
                + "\n"
                + "\n"
                + "def m_jedisPool = new JedisPool(new JedisPoolConfig(), LOCAL_HOST, PORT)\n"
                + "\n"
                + "\n"
                + "def m_jedis = m_jedisPool.getResource()\n"
                + "m_jedis.flushAll()\n"
                + "\n"
                + "// Run test\n"
                + "def jedis = getExistingJedisInstance()\n"
                + "jedis.set(TEST_KEY, TEST_VALUE).equals(\"OK\")\n"
                + "\n"
                + "// After test\n"
                + "m_jedis.close()\n"
                + "m_jedisPool.destroy()");
        jedisSampler.setProperty("scriptLanguage", ScriptingTestElement.DEFAULT_SCRIPT_LANGUAGE);
        jedisSampler.setProperty("cacheKey", UUID.randomUUID().toString());
        jedisSampler.setProperty(TestElement.TEST_CLASS, JSR223Sampler.class.getName());
        jedisSampler.setProperty(TestElement.GUI_CLASS, TestBeanGUI.class.getName());
        // Loop Controller
        LoopController loopController = new LoopController();
        loopController.setLoops(1);
        loopController.setFirst(true);
        loopController.setProperty(TestElement.TEST_CLASS, LoopController.class.getName());
        loopController.setProperty(TestElement.GUI_CLASS, LoopControlPanel.class.getName());
        loopController.initialize();
        // Thread Group
        ThreadGroup threadGroup = new ThreadGroup();
        threadGroup.setName("Jedis Thread Group");
        threadGroup.setNumThreads(1);
        threadGroup.setRampUp(1);
        threadGroup.setSamplerController(loopController);
        threadGroup.setProperty(TestElement.TEST_CLASS, ThreadGroup.class.getName());
        threadGroup.setProperty(TestElement.GUI_CLASS, ThreadGroupGui.class.getName());
        // Test Plan
        TestPlan testPlan = new TestPlan("Jedis Test Plan");
        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, threadGroup);
        threadGroupHashTree.add(jedisSampler);
        // save generated test plan to JMeter's .jmx file format
        SaveService.saveTree(testPlanTree, new FileOutputStream(jmeterHome + slash + "jedis.jmx"));
        //add Summarizer output to get test progress in stdout like:
        // summary =      2 in   1.3s =    1.5/s Avg:   631 Min:   290 Max:   973 Err:     0 (0.00%)
        Summariser summer = null;
        String summariserName = JMeterUtils.getPropDefault("summariser.name", "summary");
        if (summariserName.length() > 0) {
            summer = new Summariser(summariserName);
        }
        // Store execution results into a .jtl file
        String logFile = jmeterHome + slash + "jedis.jtl";
        ResultCollector logger = new ResultCollector(summer);
        logger.setFilename(logFile);
        testPlanTree.add(testPlanTree.getArray()[0], logger);
        // Run Test Plan
        jmeter.configure(testPlanTree);
        jmeter.run();
        System.out.println("Test completed. See " + jmeterHome + slash + "jedis.jtl file for results");
        System.out.println("JMeter .jmx script is available at " + jmeterHome + slash + "jedis.jmx");
        System.exit(0);

    }
}

References:

  • JSR223 Elements
  • Groovy Documentation
  • Five Ways To Launch a JMeter Test without Using the JMeter GUI
READ ALSO
Вывод иерархического списка(дерева)

Вывод иерархического списка(дерева)

Подтолкните на мысльКак в android приложении реализовать иерархический список, чтобы при клике на родителя он раскрывался, а ниже его выводились...

223
Странный ответ regexp

Странный ответ regexp

Есть метод

193
Круг Google Maps Android

Круг Google Maps Android

Как отрисовывать круг определенного радиуса (в метрах) вокруг текущей позиции в Google Maps API? Позиция круга, соответственно, должна обновляться...

192
Размытие в input по клику

Размытие в input по клику

Приветствую

229