JMeter - Velocity JSR223 script can't use JMeter variables/environment

user7294900 :

I'm able to load Velocity as language to JSR223 in JMeter 4/5,

I added velocity-engine-core-2.0.jar and velocity-engine-scripting-2.0.jar which include velocity in JSR223 languages list,

(Tried also with latest velocity 2.1 versions)

The problem that any text given is ignored, even if I put invalid syntax it doesn't throw any exception (as in other languages), or use vars, log,...

It seems that script isn't executed, how can I execute velocity JSR223 script? do I need to add configurations or jars? Or is there some JMeter/Java restrictions for velocity?

Tried for example:

log.info("Hello Velocity");
vars.put("a","Hello Velocity");
System.out.println("Hello Velocity");
NoExists.out.println("Hello Velocity");

It seems that I have velocity runtime dependencies

slf4j-api 1.7.25 Yes Yes Yes you'll also need an slf4j binding at runtime

commons-lang 3.5 Yes Yes Yes

library contains slf4j-api-1.7.25.jar commons-lang3-3.8.1.jar and added slf4j-simple-1.7.25.jar to default JMeter library

Log:

2019-06-05 11:48:05,739 DEBUG o.a.j.c.GenericController: Calling next on: class org.apache.jmeter.control.GenericController
2019-06-05 11:48:05,739 DEBUG o.a.j.t.TestBeanHelper: Preparing class org.apache.jmeter.protocol.java.sampler.JSR223Sampler
2019-06-05 11:48:05,739 DEBUG o.a.j.t.TestBeanHelper: Setting scriptLanguage=velocity
2019-06-05 11:48:05,739 DEBUG o.a.j.t.TestBeanHelper: Setting parameters=
2019-06-05 11:48:05,739 DEBUG o.a.j.t.TestBeanHelper: Setting filename=
2019-06-05 11:48:05,739 DEBUG o.a.j.t.TestBeanHelper: Setting cacheKey=false
2019-06-05 11:48:05,739 DEBUG o.a.j.t.TestBeanHelper: Setting script=log.info("Hello Velocity");
vars.put("a","Hello Velocity");
System.out.println("Hello Velocity");
NoExists.out.println("Hello Velocity");
2019-06-05 11:48:05,739 DEBUG o.a.velocity: Initializing Velocity, Calling init()...
2019-06-05 11:48:05,739 TRACE o.a.velocity: *****************************
2019-06-05 11:48:05,739 DEBUG o.a.velocity: Starting Apache Velocity v2.0
2019-06-05 11:48:05,739 TRACE o.a.velocity: RuntimeInstance initializing.
2019-06-05 11:48:05,741 DEBUG o.a.velocity: Default Properties resource: org/apache/velocity/runtime/defaults/velocity.properties
2019-06-05 11:48:05,741 TRACE o.a.v.loader: ResourceManager initializing: class org.apache.velocity.runtime.resource.ResourceManagerImpl
2019-06-05 11:48:05,741 DEBUG o.a.velocity: ResourceLoader instantiated: org.apache.velocity.runtime.resource.loader.FileResourceLoader
2019-06-05 11:48:05,741 TRACE o.a.v.l.file: FileResourceLoader: initialization starting.
2019-06-05 11:48:05,741 DEBUG o.a.v.l.file: FileResourceLoader: adding path '.'
2019-06-05 11:48:05,741 TRACE o.a.v.l.file: FileResourceLoader: initialization complete.
2019-06-05 11:48:05,741 DEBUG o.a.velocity: initialized (class org.apache.velocity.runtime.resource.ResourceCacheImpl) with class java.util.Collections$SynchronizedMap cache map.
2019-06-05 11:48:05,741 TRACE o.a.v.loader: Default ResourceManager initialization complete.
2019-06-05 11:48:05,741 DEBUG o.a.velocity: Loaded System Directive: org.apache.velocity.runtime.directive.Stop
2019-06-05 11:48:05,742 DEBUG o.a.velocity: Loaded System Directive: org.apache.velocity.runtime.directive.Define
2019-06-05 11:48:05,742 DEBUG o.a.velocity: Loaded System Directive: org.apache.velocity.runtime.directive.Break
2019-06-05 11:48:05,742 DEBUG o.a.velocity: Loaded System Directive: org.apache.velocity.runtime.directive.Evaluate
2019-06-05 11:48:05,742 DEBUG o.a.velocity: Loaded System Directive: org.apache.velocity.runtime.directive.Macro
2019-06-05 11:48:05,742 DEBUG o.a.velocity: Loaded System Directive: org.apache.velocity.runtime.directive.Parse
2019-06-05 11:48:05,742 DEBUG o.a.velocity: Loaded System Directive: org.apache.velocity.runtime.directive.Include
2019-06-05 11:48:05,742 DEBUG o.a.velocity: Loaded System Directive: org.apache.velocity.runtime.directive.Foreach
2019-06-05 11:48:05,743 DEBUG o.a.v.parser: Created '20' parsers.
2019-06-05 11:48:05,743 TRACE o.a.v.macro: initialization starting.
2019-06-05 11:48:05,743 DEBUG o.a.v.macro: "velocimacro.library" is not set. Trying default library: VM_global_library.vm
2019-06-05 11:48:05,743 DEBUG o.a.v.macro: Default library not found.
2019-06-05 11:48:05,743 DEBUG o.a.v.macro: allowInline = true: VMs can be defined inline in templates
2019-06-05 11:48:05,743 DEBUG o.a.v.macro: allowInlineToOverride = false: VMs defined inline may NOT replace previous VM definitions
2019-06-05 11:48:05,743 DEBUG o.a.v.macro: allowInlineLocal = false: VMs defined inline will be global in scope if allowed.
2019-06-05 11:48:05,744 DEBUG o.a.v.macro: autoload off: VM system will not automatically reload global library macros
2019-06-05 11:48:05,744 TRACE o.a.v.macro: Velocimacro: initialization complete.
2019-06-05 11:48:05,744 TRACE o.a.velocity: RuntimeInstance successfully initialized.
2019-06-05 11:48:05,744 DEBUG o.a.j.c.GenericController: Calling next on: class org.apache.jmeter.control.GenericController

EDIT

It seems that Invalid velocity context can raise an error,

e.g. for calling not exists velocity variable $a

2019-06-05 12:39:28,804 DEBUG o.a.v.rendering: Null reference [template '<unknown>', line 2, column 7]: $a cannot be resolved.

But the problem remains that JMeter can't use the velocity variable in its context

EDIT

workaround is to add velocity LogTool (velocity-tools-generic-3.0.jar) and use $log

$log.debug( "Hello Velocity");

But can't use JMeter variable as

$log.debug( "Hello " + vars.get("a"));
user7294900 :

I found the issue, in Velocity you need to add $ in front of your context variable name,

So instead of vars use $vars:

$log.debug( "Hello " + $vars.get("a"));

There's an enhancement to add Velocity to JMeter as JSR223 language, You can vote

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=156315&siteId=1