While I like groovy, it might be that other users have other JSR223 languages as their favourites.

Should we make the init script mechanism a little bit more flexible by using the files ending to determine the language/engine? That way a user could setup a jsr223.init.file=my_suberb_init.js and JMeter would try to get a JSR 223 engine for "js", or jsr223.init.file=some_python.py to run it with an engine for "py".

What do you think?

Felix


Am 09.09.2018 um 15:09 schrieb [email protected]:
Author: pmouawad
Date: Sun Sep  9 13:09:21 2018
New Revision: 1840406

URL: http://svn.apache.org/viewvc?rev=1840406&view=rev
Log:
Bug 62700 - Introduce groovy.init.file to allow calling a groovy script on 
JMeter startup
Bugzilla Id: 62700

Modified:
     jmeter/trunk/bin/jmeter.properties
     jmeter/trunk/src/core/org/apache/jmeter/JMeter.java
     jmeter/trunk/xdocs/changes.xml
     jmeter/trunk/xdocs/usermanual/properties_reference.xml

Modified: jmeter/trunk/bin/jmeter.properties
URL: 
http://svn.apache.org/viewvc/jmeter/trunk/bin/jmeter.properties?rev=1840406&r1=1840405&r2=1840406&view=diff
==============================================================================
--- jmeter/trunk/bin/jmeter.properties (original)
+++ jmeter/trunk/bin/jmeter.properties Sun Sep  9 13:09:21 2018
@@ -919,6 +919,13 @@ beanshell.server.file=../extras/startup.
  # Groovy function
  #---------------------------------------------------------------------------
+# Path to Groovy file containing script to call on JMeter startup
+# This script can use pre-defined variables:
+# log : Logger to log any message
+# props : JMeter Property
+# OUT : System.OUT
+#groovy.init.file=
+
  #Path to Groovy file containing utility functions to make available to 
__groovy function
  #groovy.utilities=
Modified: jmeter/trunk/src/core/org/apache/jmeter/JMeter.java
URL: 
http://svn.apache.org/viewvc/jmeter/trunk/src/core/org/apache/jmeter/JMeter.java?rev=1840406&r1=1840405&r2=1840406&view=diff
==============================================================================
--- jmeter/trunk/src/core/org/apache/jmeter/JMeter.java (original)
+++ jmeter/trunk/src/core/org/apache/jmeter/JMeter.java Sun Sep  9 13:09:21 2018
@@ -22,6 +22,7 @@ import java.awt.event.ActionEvent;
  import java.io.File;
  import java.io.FileInputStream;
  import java.io.FileNotFoundException;
+import java.io.FileReader;
  import java.io.IOException;
  import java.io.InputStream;
  import java.net.Authenticator;
@@ -44,6 +45,10 @@ import java.util.StringTokenizer;
  import java.util.concurrent.TimeUnit;
  import java.util.concurrent.atomic.AtomicInteger;
+import javax.script.Bindings;
+import javax.script.ScriptEngine;
+import javax.script.ScriptEngineManager;
+import javax.script.ScriptException;
  import javax.swing.JTree;
  import javax.swing.UIManager;
  import javax.swing.tree.TreePath;
@@ -634,23 +639,7 @@ public class JMeter implements JMeterPlu
              t.run(); // NOSONAR we just evaluate some code here
          }
- // Should we run a beanshell script on startup?
-        String bshinit = JMeterUtils.getProperty("beanshell.init.file");// 
$NON-NLS-1$
-        if (bshinit != null){
-            log.info("Run Beanshell on file: {}", bshinit);
-            try {
-                BeanShellInterpreter bsi = new BeanShellInterpreter();
-                bsi.source(bshinit);
-            } catch (ClassNotFoundException e) {
-                if (log.isWarnEnabled()) {
-                    log.warn("Could not start Beanshell: {}", 
e.getLocalizedMessage());
-                }
-            } catch (JMeterException e) {
-                if (log.isWarnEnabled()) {
-                    log.warn("Could not process Beanshell file: {}", 
e.getLocalizedMessage());
-                }
-            }
-        }
+        runInitScripts();
int mirrorPort=JMeterUtils.getPropDefault("mirror.server.port", 0);// $NON-NLS-1$
          if (mirrorPort > 0){
@@ -665,6 +654,51 @@ public class JMeter implements JMeterPlu
              }
          }
      }
+
+
+    /**
+     * Runs user configured init scripts
+     */
+    void runInitScripts() {
+        // Should we run a beanshell script on startup?
+        String bshinit = JMeterUtils.getProperty("beanshell.init.file");// 
$NON-NLS-1$
+        if (bshinit != null){
+            log.info("Running Beanshell on file: {}", bshinit);
+            try {
+                BeanShellInterpreter bsi = new BeanShellInterpreter();
+                bsi.source(bshinit);
+            } catch (ClassNotFoundException|JMeterException e) {
+                if (log.isWarnEnabled()) {
+                    log.warn("Could not process Beanshell file: {}", 
e.getMessage());
+                }
+            }
+        }
+
+        // Should we run a Groovy script on startup?
+        String jsr223Init = JMeterUtils.getProperty("groovy.init.file");// 
$NON-NLS-1$
+        if (jsr223Init != null){
+            log.info("Running Groovy init script in file: {}", jsr223Init);
+            File file = new File(jsr223Init);
+            if(file.exists() && file.canRead()) {
+                try (FileReader reader = new FileReader(file)) {
+                    ScriptEngineManager scriptEngineManager = new 
ScriptEngineManager();
+                    ScriptEngine engine = 
scriptEngineManager.getEngineByName("Groovy");
+                    Bindings bindings = engine.createBindings();
+                    final Logger logger = 
LoggerFactory.getLogger("groovy.init.file");
+                    bindings.put("log", logger); // $NON-NLS-1$ (this name is 
fixed)
+                    Properties props = JMeterUtils.getJMeterProperties();
+                    bindings.put("props", props); // $NON-NLS-1$ (this name is 
fixed)
+                    // For use in debugging:
+                    bindings.put("OUT", System.out); // NOSONAR $NON-NLS-1$ 
(this name is fixed)
+                    engine.eval(reader, bindings);
+                } catch (IOException | ScriptException ex) {
+                    log.error("Error running init script referenced by property 
{}", jsr223Init, ex);
+                }
+            } else {
+                log.error("Script {} referenced by property {} is not readable or 
does not exists", file.getAbsolutePath(), jsr223Init);
+            }
+        }
+    }
/**
       * Sets a proxy server for the JVM if the command line arguments are

Modified: jmeter/trunk/xdocs/changes.xml
URL: 
http://svn.apache.org/viewvc/jmeter/trunk/xdocs/changes.xml?rev=1840406&r1=1840405&r2=1840406&view=diff
==============================================================================
--- jmeter/trunk/xdocs/changes.xml [utf-8] (original)
+++ jmeter/trunk/xdocs/changes.xml [utf-8] Sun Sep  9 13:09:21 2018
@@ -160,6 +160,7 @@ this behaviour, set <code>httpclient.res
    <li><bug>62470</bug>CSV Output : Enable logging of sub results when 
<code>jmeter.save.saveservice.subresults=true</code>. Contributed by Ubik Load Pack (support at 
ubikloadpack.com)</li>
    <li><bug>62473</bug>Setting "<code>saveservice_properties</code>" has counter 
intuitive behaviour</li>
    <li><bug>62354</bug>Correct calculation and usage of units for second per user 
(reported by jffagot05 at gmail.com)</li>
+  <li><bug>62700</bug>Introduce groovy.init.file to allow calling a groovy script on 
JMeter startup</li>
    <li><bug>62128</bug>Try to guess <code>JMETER_HOME</code> correctly, when 
<code>jmeter.bat</code> is called from a batch file in another directory. Contributed by logox01 (logox01 at 
gmx.at)</li>
    <li><pr>386</pr>Add parameter support for RMI keystore creation scripts. 
Contributed by Logan Mauzaize (t524467 at airfrance.fr)</li>
    <li><bug>62065</bug>Use Maven artifact for JAF Module instead of embedded 
module</li>

Modified: jmeter/trunk/xdocs/usermanual/properties_reference.xml
URL: 
http://svn.apache.org/viewvc/jmeter/trunk/xdocs/usermanual/properties_reference.xml?rev=1840406&r1=1840405&r2=1840406&view=diff
==============================================================================
--- jmeter/trunk/xdocs/usermanual/properties_reference.xml (original)
+++ jmeter/trunk/xdocs/usermanual/properties_reference.xml Sun Sep  9 13:09:21 
2018
@@ -1873,6 +1873,26 @@ JMETER-SERVER</source>
      </property>
  </properties>
  </section>
+
+<section name="&sect-num;.43 Advanced Groovy Scripting configuration" 
anchor="groovy">
+<description>Advanced properties for configuration of scripting in 
Grooyv</description>
+<properties>
+    <property name="groovy.init.file">
+    Path to Groovy file containing script to call on JMeter startup. <br/>
+    This script can use pre-defined variables:
+    <ul>
+        <li><code>log</code>: Logger to log any message, uses SLF4J 
library</li>
+        <li><code>props</code>: JMeter Properties</li>
+        <li><code>OUT</code>: System.OUT, useful to write in the console</li>
+    </ul>
+    No script is defined by default.
+    </property>
+    <property name="groovy.utilities">
+    Path to Groovy file containing utility functions to make available to __groovy 
function. <br/>
+    Defaults to <code>bin/utility.groovy</code>
+    </property>
+</properties>
+</section>
  <!--
  <section name="&sect-num;.10 Reports" anchor="Reports">
  <description>



Reply via email to