org.apache.jmeter.samplers.Sampler Java Examples

The following examples show how to use org.apache.jmeter.samplers.Sampler. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: If.java    From jmeter-plugins with Apache License 2.0 6 votes vote down vote up
@Override
public synchronized String execute(SampleResult previousResult, Sampler currentSampler)
        throws InvalidVariableException {

    String actual = getParameter(0);
    String expected = getParameter(1);

    String result = null;
    if (actual.equals(expected)) {
        result = getParameter(2).toString();
    } else {
        result = getParameter(3).toString();
    }

    JMeterVariables vars = getVariables();
    if (vars != null && values.length > 4) {
        String varName = getParameter(4).trim();
        vars.put(varName, result);
    }

    return result;
}
 
Example #2
Source File: VirtualUserController.java    From jmeter-plugins with Apache License 2.0 6 votes vote down vote up
@Override
public Sampler next() {
    if (owner.isLimitReached()) {
        setDone(true);
    } else if (!hasArrived) {
        if (owner.isLimitReached()) {
            throw new IllegalStateException("Should not have more iterations");
        }
        hasArrived = true;
        iterationNo++;
        if (owner instanceof ArrivalsThreadGroup) {
            getOwnerAsArrivals().arrivalFact(JMeterContextService.getContext().getThread(), iterationNo);
            if (!owner.isRunning()) {
                setDone(true);
                return null;
            }
        }
    }

    return super.next();
}
 
Example #3
Source File: FifoSizeTest.java    From jmeter-plugins with Apache License 2.0 6 votes vote down vote up
/**
 * Test of execute method, of class FifoSize.
 */
@Test
public void testExecute() throws Exception {
    System.out.println("execute");
    SampleResult previousResult = null;
    Sampler currentSampler = null;
    LinkedList<CompoundVariable> list;
    list = new LinkedList<>();
    list.add(new CompoundVariable("test"));
    list.add(new CompoundVariable("test"));
    FifoSize instance = new FifoSize();
    instance.setParameters(list);
    String expResult = "0";
    String result = instance.execute(null, null);
    Assert.assertEquals(expResult, result);
}
 
Example #4
Source File: StrReplaceRegex.java    From jmeter-plugins with Apache License 2.0 6 votes vote down vote up
@Override
public synchronized String execute(SampleResult previousResult, Sampler currentSampler)
        throws InvalidVariableException {

    String totalString = getParameter(0).replaceAll(getParameter(1), getParameter(2));

    JMeterVariables vars = getVariables();

    if (values.length > 3) {
        String varName = getParameter(3);
        if (vars != null && varName != null && varName.length() > 0) {// vars will be null on TestPlan
            vars.put(varName, totalString);
        }
    }

    return totalString;
}
 
Example #5
Source File: FifoPopTest.java    From jmeter-plugins with Apache License 2.0 6 votes vote down vote up
/**
 * Test of execute method, of class FifoPop.
 */
@Test
public void testExecute() throws Exception {
    System.out.println("execute");
    SampleResult previousResult = null;
    Sampler currentSampler = null;
    FifoPop instance = new FifoPop();
    LinkedList<CompoundVariable> list = new LinkedList<>();
    list.add(new CompoundVariable("FifoPoptest"));
    list.add(new CompoundVariable("FifoPoptest"));
    instance.setParameters(list);
    FifoMap.getInstance().put("FifoPoptest", "FifoPoptest");
    String expResult = "FifoPoptest";
    String result = instance.execute(null, null);
    Assert.assertEquals(expResult, result);
}
 
Example #6
Source File: FifoPop.java    From jmeter-plugins with Apache License 2.0 6 votes vote down vote up
@Override
public synchronized String execute(SampleResult previousResult, Sampler currentSampler)
        throws InvalidVariableException {
    String fifoName = ((CompoundVariable) values[0]).execute();

    String value = null;
    try {
        Object valueObj = FifoMap.getInstance().pop(fifoName, timeout);
        if (valueObj != null) {
            value = valueObj.toString();
        }
    } catch (InterruptedException ex) {
        log.warn("Interrupted pop from queue " + fifoName);
        value = "INTERRUPTED";
    }

    JMeterVariables vars = getVariables();
    if (vars != null && values.length > 1) {
        String varName = ((CompoundVariable) values[1]).execute().trim();
        vars.put(varName, value);
    }

    return value;
}
 
Example #7
Source File: MD5.java    From jmeter-plugins with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public synchronized String execute(SampleResult previousResult, Sampler currentSampler)
        throws InvalidVariableException {
    JMeterVariables vars = getVariables();
    String str = ((CompoundVariable) values[0]).execute();
    MessageDigest digest;
    try {
        digest = MessageDigest.getInstance("md5");
    } catch (NoSuchAlgorithmException ex) {
        return "Error creating digest: " + ex;
    }

    String res = JOrphanUtils.baToHexString(digest.digest(str.getBytes()));

    if (vars != null && values.length > 1) {
        String varName = ((CompoundVariable) values[1]).execute().trim();
        vars.put(varName, res);
    }

    return res;
}
 
Example #8
Source File: FifoGet.java    From jmeter-plugins with Apache License 2.0 6 votes vote down vote up
@Override
public synchronized String execute(SampleResult previousResult, Sampler currentSampler)
        throws InvalidVariableException {
    String fifoName = ((CompoundVariable) values[0]).execute();

    Object valueObj = FifoMap.getInstance().get(fifoName);
    String value = null;
    if (valueObj != null) {
        value = valueObj.toString();
    }

    JMeterVariables vars = getVariables();
    if (vars != null && values.length > 1) {
        String varName = ((CompoundVariable) values[1]).execute().trim();
        vars.put(varName, value);
    }

    return value;
}
 
Example #9
Source File: FifoSize.java    From jmeter-plugins with Apache License 2.0 6 votes vote down vote up
@Override
public synchronized String execute(SampleResult previousResult, Sampler currentSampler)
        throws InvalidVariableException {
    String fifoName = ((CompoundVariable) values[0]).execute();

    int size = FifoMap.getInstance().length(fifoName);
    String value = Integer.toString(size);

    JMeterVariables vars = getVariables();
    if (vars != null && values.length > 1) {
        String varName = ((CompoundVariable) values[1]).execute().trim();
        vars.put(varName, value);
    }

    return value;
}
 
Example #10
Source File: Env.java    From jmeter-plugins with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public synchronized String execute(SampleResult previousResult, Sampler currentSampler)
        throws InvalidVariableException {
    String propertyName = values[0].execute();
    String propertyDefault = propertyName;
    if (values.length > 2) { // We have a 3rd parameter
        propertyDefault = values[2].execute();
    }
    String propertyValue = JMeterPluginsUtils.getEnvDefault(propertyName, propertyDefault);
    if (values.length > 1) {
        String variableName = values[1].execute();
        if (variableName.length() > 0) {// Allow for empty name
            final JMeterVariables variables = getVariables();
            if (variables != null) {
                variables.put(variableName, propertyValue);
            }
        }
    }
    return propertyValue;
}
 
Example #11
Source File: UpperCaseTest.java    From jmeter-plugins with Apache License 2.0 6 votes vote down vote up
/**
 * Test of execute method, of class UpperCase.
 */
@Test
public void testExecute() throws Exception {
    System.out.println("execute");
    Collection<CompoundVariable> parameters = new ArrayList<>();
    parameters.add(new CompoundVariable("abc"));
    parameters.add(new CompoundVariable("var"));

    SampleResult previousResult = null;
    Sampler currentSampler = null;
    UpperCase instance = new UpperCase();
    instance.setParameters(parameters);
    String expResult = "ABC";
    String result = instance.execute(null, null);
    Assert.assertEquals(expResult, result);
    Assert.assertEquals(expResult, JMeterContextService.getContext().getVariables().get("var"));
}
 
Example #12
Source File: MD5Test.java    From jmeter-plugins with Apache License 2.0 6 votes vote down vote up
/**
 * Test of execute method, of class MD5.
 */
@Test
public void testExecute() throws Exception {
    System.out.println("execute");
    Collection<CompoundVariable> parameters = new ArrayList<CompoundVariable>();
    parameters.add(new CompoundVariable("public void testExecute() throws Exception {}"));
    parameters.add(new CompoundVariable("var"));

    SampleResult previousResult = null;
    Sampler currentSampler = null;
    MD5 instance = new MD5();
    instance.setParameters(parameters);
    String expResult = "cb5b1ca8504cb5a7772f219109e05ccf";
    String result = instance.execute(previousResult, currentSampler);
    Assert.assertEquals(expResult, result);
    Assert.assertEquals(expResult, JMeterContextService.getContext().getVariables().get("var"));
}
 
Example #13
Source File: StrReplace.java    From jmeter-plugins with Apache License 2.0 6 votes vote down vote up
@Override
public synchronized String execute(SampleResult previousResult, Sampler currentSampler)
        throws InvalidVariableException {

    String totalString = getParameter(0).replace(getParameter(1), getParameter(2));

    JMeterVariables vars = getVariables();

    if (values.length > 3) {
        String varName = getParameter(3);
        if (vars != null && varName != null && varName.length() > 0) {// vars will be null on TestPlan
            vars.put(varName, totalString);
        }
    }

    return totalString;
}
 
Example #14
Source File: EvaluatePanel.java    From jmeter-debugger with Apache License 2.0 6 votes vote down vote up
@Override
public void actionPerformed(ActionEvent actionEvent) {
    result.clear();
    if (exprField.getText().isEmpty()) {
        return;
    }

    CompoundVariable masterFunction = new CompoundVariable(exprField.getText());
    SampleResult previousResult = context.getPreviousResult();
    Sampler currentSampler = context.getCurrentSampler();
    try {
        result.setText(masterFunction.execute(previousResult, currentSampler));
    } catch (Throwable e) {
        ByteArrayOutputStream text = new ByteArrayOutputStream(1024);
        e.printStackTrace(new PrintStream(text));
        result.setText(text.toString());
        result.scrollToTop();
    }
}
 
Example #15
Source File: IsDefined.java    From jmeter-plugins with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public synchronized String execute(SampleResult previousResult, Sampler currentSampler)
        throws InvalidVariableException {
    JMeterVariables vars = getVariables();
    String res = ((CompoundVariable) values[0]).execute().trim();

    if (vars != null) {
        Object var = vars.getObject(res);
        if (var != null) {
            return "1";
        }
    }

    return "0";

}
 
Example #16
Source File: ChooseRandomTest.java    From jmeter-plugins with Apache License 2.0 6 votes vote down vote up
@Test
public void testExecute() throws Exception {
    System.out.println("execute");
    SampleResult previousResult = null;
    Sampler currentSampler = null;
    Collection<CompoundVariable> parameters = new ArrayList<>();
    for (int i = 0; i < 1000; i++) {
        parameters.add(new CompoundVariable(String.valueOf(i)));

    }
    parameters.add(new CompoundVariable("4.3346"));
    parameters.add(new CompoundVariable("5.3346"));
    ChooseRandom instance = new ChooseRandom();
    instance.setParameters(parameters);
    String result1 = instance.execute(null, null);
    String result2 = instance.execute(null, null);
    Assert.assertTrue(!result1.equals(result2));
}
 
Example #17
Source File: CaseFormat.java    From jmeter-plugins with Apache License 2.0 6 votes vote down vote up
@Override
public String execute(SampleResult previousResult, Sampler currentSampler) throws InvalidVariableException {
	String originalString = values[0].execute();
	String mode = null; // default
	if (values.length > 1) {
		mode = values[1].execute();
	}
	if (StringUtils.isEmpty(mode)) {
		mode = CaseFormatMode.LOWER_CAMEL_CASE.getName(); // default
	}
	String targetString = changeCase(originalString, mode);
	if (values.length > 2) {
		addVariableValue(targetString, values[2].execute().trim());
	}
	return targetString;
}
 
Example #18
Source File: EnvTest.java    From jmeter-plugins with Apache License 2.0 6 votes vote down vote up
@Test
public void testExecute_1() throws Exception {
    System.out.println("execute 1");
    SampleResult previousResult = null;
    Sampler currentSampler = null;

    Assert.assertNull(JMeterContextService.getContext().getVariables().get("toto"));
    Collection<CompoundVariable> parameters = new ArrayList<>();
    parameters.add(new CompoundVariable(key));
    parameters.add(new CompoundVariable("toto"));
    Env instance = new Env();
    instance.setParameters(parameters);
    String result = instance.execute(null, null);
    Assert.assertEquals(value, result);
    Assert.assertNotNull(JMeterContextService.getContext().getVariables().remove("toto"));
}
 
Example #19
Source File: Substring.java    From jmeter-plugins with Apache License 2.0 6 votes vote down vote up
@Override
public synchronized String execute(SampleResult previousResult, Sampler currentSampler)
        throws InvalidVariableException {

    String str = getParameter(0);

    int begin = Integer.parseInt(getParameter(1).trim());
    int len = Integer.parseInt(getParameter(2).trim());

    String totalString = str.substring(begin, len);

    JMeterVariables vars = getVariables();

    if (values.length > 3) {
        String varName = getParameter(3);
        if (vars != null && varName != null && varName.length() > 0) {// vars will be null on TestPlan
            vars.put(varName, totalString);
        }
    }

    return totalString;
}
 
Example #20
Source File: WeightedSwitchControllerTest.java    From jmeter-bzm-plugins with Apache License 2.0 6 votes vote down vote up
@Test
public void testGeneric() {
    WeightedSwitchController obj = new WeightedSwitchController();
    obj.addTestElement(getSampler("0"));
    obj.addTestElement(getSampler("1"));
    obj.addTestElement(getSampler("2"));
    obj.addTestElement(getSampler("5"));
    PowerTableModel mdl = new PowerTableModel(new String[]{"name", WeightedSwitchController.WEIGHTS}, new Class[]{String.class, String.class});
    mdl.addRow(new String[]{"0", "0"});
    mdl.addRow(new String[]{"1", "1"});
    mdl.addRow(new String[]{"2", "2"});
    mdl.addRow(new String[]{"5", "5"});
    obj.setData(mdl);

    for (int n = 0; n < 800; n++) {
        Sampler s = obj.next();
        log.info("Sampler: " + s.getName());
        assertNotNull(s);
        assertNull(obj.next());
        assertNotEquals(s.getName(), "0");
    }
}
 
Example #21
Source File: WeightedSwitchControllerGui.java    From jmeter-bzm-plugins with Apache License 2.0 6 votes vote down vote up
private Map<JMeterTreeNode, Boolean> getChildItems(JMeterTreeNode root, WeightedSwitchController element) {
    Map<JMeterTreeNode, Boolean> result = new LinkedHashMap<>();
    for (int i = 0; i < root.getChildCount(); i++) {
        JMeterTreeNode child = (JMeterTreeNode) root.getChildAt(i);

        TestElement te = child.getTestElement();
        if (element != root.getTestElement()) {
            result.putAll(getChildItems(child, element));
        } else {
            if (te instanceof Sampler || te instanceof Controller) {
                result.put(child, te.isEnabled());
            }
        }
    }
    return result;
}
 
Example #22
Source File: WeightedSwitchController.java    From jmeter-bzm-plugins with Apache License 2.0 6 votes vote down vote up
@Override
public Sampler next() {
    if (chosen) {
        Sampler result = super.next();

        if (result == null || currentCopy != current) {
            reset();
            for (TestElement element : super.getSubControllers()) {
                if (element instanceof Controller) {
                    resetController((Controller) element);
                }
            }
            return null;
        }
        return result;
    } else {
        chosen = true;
        choose();
        return super.next();
    }
}
 
Example #23
Source File: EnvTest.java    From jmeter-plugins with Apache License 2.0 6 votes vote down vote up
@Test
public void testExecute_3() throws Exception {
    System.out.println("execute 1");
    SampleResult previousResult = null;
    Sampler currentSampler = null;
    Assert.assertNull(JMeterContextService.getContext().getVariables().get("toto"));
    Collection<CompoundVariable> parameters = new ArrayList<>();
    String overrideKey = key + "testExecute_3";
    String defaultValue = "defaultValue";
    parameters.add(new CompoundVariable(overrideKey));
    parameters.add(new CompoundVariable(""));
    parameters.add(new CompoundVariable(defaultValue));
    Env instance = new Env();
    instance.setParameters(parameters);
    String result = instance.execute(null, null);
    Assert.assertEquals(defaultValue, result);
    Assert.assertNull(JMeterContextService.getContext().getVariables().get("toto"));
}
 
Example #24
Source File: LowerCaseTest.java    From jmeter-plugins with Apache License 2.0 6 votes vote down vote up
/**
 * Test of execute method, of class LowerCase.
 */
@Test
public void testExecute() throws Exception {
    System.out.println("execute");
    Collection<CompoundVariable> parameters = new ArrayList<CompoundVariable>();
    parameters.add(new CompoundVariable("ABC"));
    parameters.add(new CompoundVariable("var"));

    SampleResult previousResult = null;
    Sampler currentSampler = null;
    LowerCase instance = new LowerCase();
    instance.setParameters(parameters);
    String expResult = "abc";
    String result = instance.execute(previousResult, currentSampler);
    Assert.assertEquals(expResult, result);
    Assert.assertEquals(expResult, JMeterContextService.getContext().getVariables().get("var"));
}
 
Example #25
Source File: DoubleSumTest.java    From jmeter-plugins with Apache License 2.0 6 votes vote down vote up
/**
 * Test of execute method, of class DoubleSum.
 */
@Test
public void testExecute() throws Exception {
    System.out.println("execute");
    SampleResult previousResult = null;
    Sampler currentSampler = null;

    Collection<CompoundVariable> parameters = new ArrayList<>();
    parameters.add(new CompoundVariable("1.256"));
    parameters.add(new CompoundVariable("4.3346"));

    DoubleSum instance = new DoubleSum();
    instance.setParameters(parameters);
    String expResult = "5.5906";
    String result = instance.execute(null, null);
    Assert.assertEquals(expResult, result);
}
 
Example #26
Source File: Base64Decode.java    From jmeter-plugins with Apache License 2.0 6 votes vote down vote up
@Override
public synchronized String execute(SampleResult previousResult, Sampler currentSampler)
        throws InvalidVariableException {
    String sourceString = values[0].execute();

    String decodedValue = new String(Base64.decodeBase64(sourceString));
    if (values.length > 1) {
        String variableName = values[1].execute();
        if (variableName.length() > 0) {// Allow for empty name
            final JMeterVariables variables = getVariables();
            if (variables != null) {
                variables.put(variableName, decodedValue);
            }
        }
    }
    return decodedValue;
}
 
Example #27
Source File: Base64Encode.java    From jmeter-plugins with Apache License 2.0 6 votes vote down vote up
@Override
public synchronized String execute(SampleResult previousResult, Sampler currentSampler)
        throws InvalidVariableException {
    String sourceString = values[0].execute();

    String decodedValue = new String(Base64.encodeBase64(sourceString.getBytes()));
    if (values.length > 1) {
        String variableName = values[1].execute();
        if (variableName.length() > 0) {// Allow for empty name
            final JMeterVariables variables = getVariables();
            if (variables != null) {
                variables.put(variableName, decodedValue);
            }
        }
    }
    return decodedValue;
}
 
Example #28
Source File: UpperCase.java    From jmeter-plugins with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public synchronized String execute(SampleResult previousResult, Sampler currentSampler)
        throws InvalidVariableException {
    JMeterVariables vars = getVariables();
    String res = ((CompoundVariable) values[0]).execute().toUpperCase();

    if (vars != null && values.length > 1) {
        String varName = ((CompoundVariable) values[1]).execute().trim();
        vars.put(varName, res);
    }

    return res;

}
 
Example #29
Source File: StrLen.java    From jmeter-plugins with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public synchronized String execute(SampleResult previousResult, Sampler currentSampler)
        throws InvalidVariableException {
    JMeterVariables vars = getVariables();
    Integer len = ((CompoundVariable) values[0]).execute().length();

    if (vars != null && values.length > 1) {
        String varName = ((CompoundVariable) values[1]).execute().trim();
        vars.put(varName, len.toString());
    }

    return len.toString();

}
 
Example #30
Source File: LowerCase.java    From jmeter-plugins with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public synchronized String execute(SampleResult previousResult, Sampler currentSampler)
        throws InvalidVariableException {
    JMeterVariables vars = getVariables();
    String res = ((CompoundVariable) values[0]).execute().toLowerCase();

    if (vars != null && values.length > 1) {
        String varName = ((CompoundVariable) values[1]).execute().trim();
        vars.put(varName, res);
    }

    return res;

}