Search in sources :

Example 86 with JMeterContext

use of org.apache.jmeter.threads.JMeterContext in project jmeter by apache.

the class FunctionProperty method getStringValue.

/**
 * Executes the function (and caches the value for the duration of the test
 * iteration) if the property is a running version. Otherwise, the raw
 * string representation of the function is provided.
 *
 * @see JMeterProperty#getStringValue()
 */
@Override
public String getStringValue() {
    // Expensive, so
    JMeterContext ctx = JMeterContextService.getContext();
    // once
    if (!isRunningVersion()) /*|| !ctx.isSamplingStarted()*/
    {
        log.debug("Not running version, return raw function string");
        return function.getRawParameters();
    }
    if (!ctx.isSamplingStarted()) {
        return function.execute();
    }
    log.debug("Running version, executing function");
    int iter = ctx.getVariables() != null ? ctx.getVariables().getIteration() : -1;
    if (iter < testIteration) {
        testIteration = -1;
    }
    if (iter > testIteration || cacheValue == null) {
        testIteration = iter;
        cacheValue = function.execute();
    }
    return cacheValue;
}
Also used : JMeterContext(org.apache.jmeter.threads.JMeterContext)

Example 87 with JMeterContext

use of org.apache.jmeter.threads.JMeterContext in project jmeter by apache.

the class CookieManager method add.

/**
 * Add a cookie.
 *
 * @param c cookie to be added
 */
public void add(Cookie c) {
    String cv = c.getValue();
    String cn = c.getName();
    // Can't have two matching cookies
    removeMatchingCookies(c);
    if (DELETE_NULL_COOKIES && (null == cv || cv.length() == 0)) {
        if (log.isDebugEnabled()) {
            log.debug("Dropping cookie with null value {}", c.toString());
        }
    } else {
        if (log.isDebugEnabled()) {
            log.debug("Add cookie to store {}", c.toString());
        }
        getCookies().addItem(c);
        if (SAVE_COOKIES) {
            JMeterContext context = getThreadContext();
            if (context.isSamplingStarted()) {
                context.getVariables().put(COOKIE_NAME_PREFIX + cn, cv);
            }
        }
    }
}
Also used : JMeterContext(org.apache.jmeter.threads.JMeterContext)

Example 88 with JMeterContext

use of org.apache.jmeter.threads.JMeterContext in project jmeter by apache.

the class TestSwitchController method testFunction.

/*
     * N.B. Requires ApacheJMeter_functions.jar to be on the classpath,
     * otherwise the function cannot be resolved.
     */
@Test
public void testFunction() throws Exception {
    JMeterContext jmctx = JMeterContextService.getContext();
    Map<String, String> variables = new HashMap<>();
    ReplaceStringWithFunctions transformer = new ReplaceStringWithFunctions(new CompoundVariable(), variables);
    jmctx.setVariables(new JMeterVariables());
    JMeterVariables jmvars = jmctx.getVariables();
    jmvars.put("VAR", "100");
    StringProperty prop = new StringProperty(SwitchController.SWITCH_VALUE, "${__counter(TRUE,VAR)}");
    JMeterProperty newProp = transformer.transformValue(prop);
    newProp.setRunningVersion(true);
    GenericController controller = new GenericController();
    SwitchController switch_cont = new SwitchController();
    switch_cont.setProperty(newProp);
    controller.addTestElement(new TestSampler("before"));
    controller.addTestElement(switch_cont);
    switch_cont.addTestElement(new TestSampler("0"));
    switch_cont.addTestElement(new TestSampler("1"));
    switch_cont.addTestElement(new TestSampler("2"));
    switch_cont.addTestElement(new TestSampler("3"));
    controller.addTestElement(new TestSampler("after"));
    controller.initialize();
    assertEquals("100", jmvars.get("VAR"));
    for (int i = 1; i <= 3; i++) {
        assertEquals("Loop " + i, "before", nextName(controller));
        assertEquals("Loop " + i, "" + i, nextName(controller));
        assertEquals("Loop " + i, "" + i, jmvars.get("VAR"));
        assertEquals("Loop " + i, "after", nextName(controller));
        assertNull(nextName(controller));
    }
    int i = 4;
    assertEquals("Loop " + i, "before", nextName(controller));
    assertEquals("Loop " + i, "0", nextName(controller));
    assertEquals("Loop " + i, "" + i, jmvars.get("VAR"));
    assertEquals("Loop " + i, "after", nextName(controller));
    assertNull(nextName(controller));
    assertEquals("4", jmvars.get("VAR"));
}
Also used : CompoundVariable(org.apache.jmeter.engine.util.CompoundVariable) ReplaceStringWithFunctions(org.apache.jmeter.engine.util.ReplaceStringWithFunctions) JMeterProperty(org.apache.jmeter.testelement.property.JMeterProperty) HashMap(java.util.HashMap) StringProperty(org.apache.jmeter.testelement.property.StringProperty) JMeterVariables(org.apache.jmeter.threads.JMeterVariables) JMeterContext(org.apache.jmeter.threads.JMeterContext) TestSampler(org.apache.jmeter.junit.stubs.TestSampler) Test(org.junit.jupiter.api.Test)

Example 89 with JMeterContext

use of org.apache.jmeter.threads.JMeterContext in project jmeter by apache.

the class JavaScript method executeWithRhino.

/**
 * @param previousResult {@link SampleResult}
 * @param currentSampler {@link Sampler}
 * @param jmctx {@link JMeterContext}
 * @param vars {@link JMeterVariables}
 * @param script Javascript code
 * @param varName variable name
 * @return result as String
 * @throws InvalidVariableException
 */
private String executeWithRhino(SampleResult previousResult, Sampler currentSampler, JMeterContext jmctx, JMeterVariables vars, String script, String varName) throws InvalidVariableException {
    Context cx = Context.enter();
    String resultStr = null;
    try {
        Scriptable scope = cx.initStandardObjects(null);
        // Set up some objects for the script to play with
        // $NON-NLS-1$
        scope.put("log", scope, log);
        // $NON-NLS-1$
        scope.put("ctx", scope, jmctx);
        // $NON-NLS-1$
        scope.put("vars", scope, vars);
        // $NON-NLS-1$
        scope.put("props", scope, JMeterUtils.getJMeterProperties());
        // Previously mis-spelt as theadName
        // $NON-NLS-1$
        scope.put("threadName", scope, Thread.currentThread().getName());
        // $NON-NLS-1$
        scope.put("sampler", scope, currentSampler);
        // $NON-NLS-1$
        scope.put("sampleResult", scope, previousResult);
        // $NON-NLS-1$
        Object result = cx.evaluateString(scope, script, "<cmd>", 1, null);
        resultStr = Context.toString(result);
        if (varName != null && vars != null) {
            // vars can be null if run from TestPlan
            vars.put(varName, resultStr);
        }
    } catch (RhinoException e) {
        log.error("Error processing Javascript: [{}]", script, e);
        throw new InvalidVariableException("Error processing Javascript: [" + script + "]", e);
    } finally {
        Context.exit();
    }
    return resultStr;
}
Also used : Context(org.mozilla.javascript.Context) SimpleScriptContext(javax.script.SimpleScriptContext) ScriptContext(javax.script.ScriptContext) JMeterContext(org.apache.jmeter.threads.JMeterContext) RhinoException(org.mozilla.javascript.RhinoException) Scriptable(org.mozilla.javascript.Scriptable)

Example 90 with JMeterContext

use of org.apache.jmeter.threads.JMeterContext in project jmeter by apache.

the class Jexl3Function method execute.

/**
 * {@inheritDoc}
 */
@Override
public String execute(SampleResult previousResult, Sampler currentSampler) throws InvalidVariableException {
    // $NON-NLS-1$
    String str = "";
    CompoundVariable var = (CompoundVariable) values[0];
    String exp = var.execute();
    // $NON-NLS-1$
    String varName = "";
    if (values.length > 1) {
        varName = ((CompoundVariable) values[1]).execute().trim();
    }
    JMeterContext jmctx = JMeterContextService.getContext();
    JMeterVariables vars = jmctx.getVariables();
    try {
        JexlContext jc = new MapContext();
        // $NON-NLS-1$
        jc.set("log", log);
        // $NON-NLS-1$
        jc.set("ctx", jmctx);
        // $NON-NLS-1$
        jc.set("vars", vars);
        // $NON-NLS-1$
        jc.set("props", JMeterUtils.getJMeterProperties());
        // Previously mis-spelt as theadName
        // $NON-NLS-1$
        jc.set("threadName", Thread.currentThread().getName());
        // $NON-NLS-1$ (may be null)
        jc.set("sampler", currentSampler);
        // $NON-NLS-1$ (may be null)
        jc.set("sampleResult", previousResult);
        // $NON-NLS-1$
        jc.set("OUT", System.out);
        // Now evaluate the script, getting the result
        JexlScript e = threadLocalJexl.get().createScript(exp);
        Object o = e.execute(jc);
        if (o != null) {
            str = o.toString();
        }
        if (vars != null && varName.length() > 0) {
            // vars will be null on TestPlan
            vars.put(varName, str);
        }
    } catch (Exception e) {
        log.error("An error occurred while evaluating the expression \"{}\"\n", exp, e);
    }
    return str;
}
Also used : CompoundVariable(org.apache.jmeter.engine.util.CompoundVariable) JMeterVariables(org.apache.jmeter.threads.JMeterVariables) JMeterContext(org.apache.jmeter.threads.JMeterContext) JexlContext(org.apache.commons.jexl3.JexlContext) JexlScript(org.apache.commons.jexl3.JexlScript) MapContext(org.apache.commons.jexl3.MapContext)

Aggregations

JMeterContext (org.apache.jmeter.threads.JMeterContext)98 JMeterVariables (org.apache.jmeter.threads.JMeterVariables)64 SampleResult (org.apache.jmeter.samplers.SampleResult)47 Test (org.junit.Test)19 Test (org.junit.jupiter.api.Test)18 BeforeEach (org.junit.jupiter.api.BeforeEach)13 JSONPostProcessor (org.apache.jmeter.extractor.json.jsonpath.JSONPostProcessor)11 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)11 CompoundVariable (org.apache.jmeter.engine.util.CompoundVariable)10 Sampler (org.apache.jmeter.samplers.Sampler)9 TestSampler (org.apache.jmeter.junit.stubs.TestSampler)8 ArrayList (java.util.ArrayList)6 IOException (java.io.IOException)4 Properties (java.util.Properties)3 JMeterProperty (org.apache.jmeter.testelement.property.JMeterProperty)3 JMeterException (org.apache.jorphan.util.JMeterException)3 HashMap (java.util.HashMap)2 Bindings (javax.script.Bindings)2 Arguments (org.apache.jmeter.config.Arguments)2 ReplaceStringWithFunctions (org.apache.jmeter.engine.util.ReplaceStringWithFunctions)2