jmeter-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sebb <seb...@gmail.com>
Subject Re: svn commit: r1706624 - in /jmeter/trunk: src/functions/org/apache/jmeter/functions/JavaScript.java xdocs/changes.xml
Date Sun, 04 Oct 2015 10:32:56 GMT
On 3 October 2015 at 21:29,  <pmouawad@apache.org> wrote:
> Author: pmouawad
> Date: Sat Oct  3 20:29:27 2015
> New Revision: 1706624
>
> URL: http://svn.apache.org/viewvc?rev=1706624&view=rev
> Log:
> Bug 58477 - __javaScript function : Allow use of Nashorn engine for Java8 and upper versions
> Bugzilla Id: 58477
>
> Modified:
>     jmeter/trunk/src/functions/org/apache/jmeter/functions/JavaScript.java
>     jmeter/trunk/xdocs/changes.xml
>
> Modified: jmeter/trunk/src/functions/org/apache/jmeter/functions/JavaScript.java
> URL: http://svn.apache.org/viewvc/jmeter/trunk/src/functions/org/apache/jmeter/functions/JavaScript.java?rev=1706624&r1=1706623&r2=1706624&view=diff
> ==============================================================================
> --- jmeter/trunk/src/functions/org/apache/jmeter/functions/JavaScript.java (original)
> +++ jmeter/trunk/src/functions/org/apache/jmeter/functions/JavaScript.java Sat Oct  3
20:29:27 2015
> @@ -22,6 +22,12 @@ import java.util.Collection;
>  import java.util.LinkedList;
>  import java.util.List;
>
> +import javax.script.Bindings;
> +import javax.script.ScriptContext;
> +import javax.script.ScriptEngine;
> +import javax.script.ScriptEngineManager;
> +import javax.script.SimpleScriptContext;
> +
>  import org.apache.jmeter.engine.util.CompoundVariable;
>  import org.apache.jmeter.samplers.SampleResult;
>  import org.apache.jmeter.samplers.Sampler;
> @@ -40,7 +46,27 @@ import org.mozilla.javascript.Scriptable
>   * @since 1.9
>   */
>  public class JavaScript extends AbstractFunction {
> +    private static final String NASHORN_ENGINE_NAME = "nashorn"; //$NON-NLS-1$
> +
> +    private static final String USE_RHINO_ENGINE_PROPERTY = "javascript.use_rhino";
//$NON-NLS-1$
>
> +    /**
> +     * Initialization On Demand Holder pattern
> +     */
> +    private static class LazyHolder {
> +        public static final ScriptEngineManager INSTANCE = new ScriptEngineManager();
> +    }
> +
> +    private static final boolean USE_RHINO_ENGINE =
> +            getInstance().getEngineByName(JavaScript.NASHORN_ENGINE_NAME) == null ||
//$NON-NLS-1$
> +            JMeterUtils.getPropDefault(USE_RHINO_ENGINE_PROPERTY, true) ;

I think the property should be checked first.
As it stands, the ScriptEngineManager will always be loaded, and
Nashorn will always be instantiated on Java8+ even if the property is
true.

> +
> +    /**
> +     * @return ScriptEngineManager singleton
> +     */
> +    public static ScriptEngineManager getInstance() {

Why is this public?

> +            return LazyHolder.INSTANCE;
> +    }
>      private static final List<String> desc = new LinkedList<>();
>
>      private static final String KEY = "__javaScript"; //$NON-NLS-1$
> @@ -70,7 +96,77 @@ public class JavaScript extends Abstract
>          String varName = values.length < 2 ? null : ((CompoundVariable) values[1]).execute().trim();
>          String resultStr = "";
>
> +        if(USE_RHINO_ENGINE) {
> +            resultStr = executeWithRhino(previousResult, currentSampler, jmctx,
> +                vars, script, varName);
> +        } else {
> +            resultStr = executeWithNashorn(previousResult, currentSampler, jmctx,
> +                    vars, script, varName);
> +        }
> +
> +        return resultStr;
> +
> +    }
> +
> +    /**
> +     *
> +     * @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 executeWithNashorn(SampleResult previousResult,
> +            Sampler currentSampler, JMeterContext jmctx, JMeterVariables vars,
> +            String script, String varName)
> +            throws InvalidVariableException {
> +        String resultStr = null;
> +        try {
> +            ScriptContext newContext = new SimpleScriptContext();
> +            ScriptEngine engine = getInstance().getEngineByName(JavaScript.NASHORN_ENGINE_NAME);
> +            Bindings bindings = engine.createBindings();
> +
> +            // Set up some objects for the script to play with
> +            bindings.put("log", log); //$NON-NLS-1$
> +            bindings.put("ctx", jmctx); //$NON-NLS-1$
> +            bindings.put("vars", vars); //$NON-NLS-1$
> +            bindings.put("props", JMeterUtils.getJMeterProperties()); //$NON-NLS-1$
> +
> +            bindings.put("threadName", Thread.currentThread().getName()); //$NON-NLS-1$
> +            bindings.put("sampler", currentSampler); //$NON-NLS-1$
> +            bindings.put("sampleResult", previousResult); //$NON-NLS-1$
> +            newContext.setBindings(bindings, ScriptContext.ENGINE_SCOPE);
> +            Object result = engine.eval(script, newContext);
> +            resultStr = result.toString();
> +            if (varName != null && vars != null) {// vars can be null if run
from TestPlan
> +                vars.put(varName, resultStr);
> +            }
> +        } catch (Exception e) {
> +            log.error("Error processing Javascript: [" + script + "]\n", e);
> +            throw new InvalidVariableException("Error processing Javascript: [" + script
+ "]", e);
> +        }
> +        return resultStr;
> +    }
> +
> +    /**
> +     * @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);
> @@ -91,16 +187,13 @@ public class JavaScript extends Abstract
>              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 + "]\n", e);
>              throw new InvalidVariableException("Error processing Javascript: [" + script
+ "]", e);
>          } finally {
>              Context.exit();
>          }
> -
>          return resultStr;
> -
>      }
>
>      /** {@inheritDoc} */
>
> Modified: jmeter/trunk/xdocs/changes.xml
> URL: http://svn.apache.org/viewvc/jmeter/trunk/xdocs/changes.xml?rev=1706624&r1=1706623&r2=1706624&view=diff
> ==============================================================================
> --- jmeter/trunk/xdocs/changes.xml (original)
> +++ jmeter/trunk/xdocs/changes.xml Sat Oct  3 20:29:27 2015
> @@ -113,6 +113,7 @@ Summary
>
>  <h3>Functions</h3>
>  <ul>
> +    <li><bug>58477</bug> __javaScript function : Allow use of Nashorn
engine for Java8 and upper versions</li>
>  </ul>
>
>  <h3>I18N</h3>
>
>

Mime
View raw message