Forum: JRuby Jruby-JSR223 Sampler(JMeter) Not able to get variables or properties exposed by Jmeter

Posted by Muthu K. (muthu_k)
on 2013-02-17 12:56
Hi all,

When i was using below Jruby code in JSR223 Sampler(Jmeter), getting
below exception for multiple concurrent requests


Jmeter Variable Defined:
InputFilePath="somevalue"

Jruby Code used :
begin
sprovider_url=$vars.get("InputFilePath")
puts " args sprovider_url is #{sprovider_url} \n"

store_Obj=StoreRetrieve.new
rescue Exception =>e
puts " backtrace is #{e.backtrace}"
puts " Exception is #{e.message}"
end


Here InputFilePath is the  User Variable defined in Jmeter with
somevalue as stated above...

I ran with 5 threads

Output:
   [jmeter]  args sprovider_url is /path/filename.txt
   [jmeter]  backtrace is ["<script>:1:in `(root)'"]
   [jmeter]  backtrace is ["<script>:1:n `(root)'"]
   [jmeter]  Exception is undefined method `[]' for nil:NilClass
   [jmeter]  Exception is undefined method `[]' for nil:NilClass
   [jmeter]  backtrace is ["<script>:1:in `(root)'"]
   [jmeter]  Exception is undefined method `[]' for nil:NilClass
   [jmeter]  backtrace is ["<script>:1:in `(root)'"]
   [jmeter]  Exception is undefined method `[]' for nil:NilClass


Could you guys please help me here?  How to get $vars consistently in
Jruby...if i use any other scripting lanuages ..like  Javascript , Jexl
i am not getting this failure...
Posted by Robin McKay (robin2)
on 2013-02-17 15:22
Hi,
This is a response to your question in my JRonA topic...

Sorry, I haven't used JSR223.

It's not clear from your post if you are running 5 threads at the same 
time and expecting them all to access a common value.

Is the common value being defined in each thread, or is it defined 
somewhere else before any of the threads is started?

Are the threads running in the JRuby code or the Java code?

It might be helpful to describe what you want to achieve (but maybe that 
is obvious to people familiar with jmeter).

Best wishes ...R


>Muthu K. wrote in post #1097386:
> Hi all,
>
> When i was using below Jruby code in JSR223 Sampler(Jmeter), getting
> below exception for multiple concurrent requests
Posted by Muthu K. (muthu_k)
on 2013-02-17 15:39
Robin McKay wrote in post #1097403:
> Hi,
> This is a response to your question in my JRonA topic...
>
> Sorry, I haven't used JSR223.
>
> It's not clear from your post if you are running 5 threads at the same
> time and expecting them all to access a common value.
>
> Is the common value being defined in each thread, or is it defined
> somewhere else before any of the threads is started?
>
> Are the threads running in the JRuby code or the Java code?
>
> It might be helpful to describe what you want to achieve (but maybe that
> is obvious to people familiar with jmeter).
>
> Best wishes ...R
>
>
>>Muthu K. wrote in post #1097386:
>> Hi all,
>>
>> When i was using below Jruby code in JSR223 Sampler(Jmeter), getting
>> below exception for multiple concurrent requests

Ok thx a lot for ur quick reply on this

It is actually Jmeter Global Variables exposed by it to use in different 
Samplers (like JSR223 andd BSF Samplers etcc) ...

threads are invoked by Java code (JMeter)...
Posted by Robin McKay (robin2)
on 2013-02-17 17:00
Perhaps I should have been clearer ...

I know much less about jmeter than I do about JSR223
Sorry ...

...R


>Muthu K. wrote in post #1097405:

below exception for multiple concurrent requests
>
> Ok thx a lot for ur quick reply on this
>
> It is actually Jmeter Global Variables exposed by it to use in different
> Samplers (like JSR223 andd BSF Samplers etcc) ...
>
> threads are invoked by Java code (JMeter)...
Posted by Muthu K. (muthu_k)
on 2013-02-17 18:22
Robin McKay wrote in post #1097419:
> Perhaps I should have been clearer ...
>
> I know much less about jmeter than I do about JSR223
> Sorry ...
>
> ...R
>
>
>>Muthu K. wrote in post #1097405:
>
> below exception for multiple concurrent requests
>>
>> Ok thx a lot for ur quick reply on this
>>
>> It is actually Jmeter Global Variables exposed by it to use in different
>> Samplers (like JSR223 andd BSF Samplers etcc) ...
>>
>> threads are invoked by Java code (JMeter)...

ok thank u ..any way np..

Basically my problem is that

JMeter is sharing Global Variables and Properties of it to different 
samplers(like JSR223 and BSF ..etc...) But when i access those variables 
through Jruby(JSR223 sampler) it is returning Nil value as above....

anyone had any suggestions/idea on the same ...
Posted by Muthu K. (muthu_k)
on 2013-02-17 18:44
Muthu K. wrote in post #1097428:
> Robin McKay wrote in post #1097419:
>> Perhaps I should have been clearer ...
>>
>> I know much less about jmeter than I do about JSR223
>> Sorry ...
>>
>> ...R
>>
>>
>>>Muthu K. wrote in post #1097405:
>>
>> below exception for multiple concurrent requests
>>>
>>> Ok thx a lot for ur quick reply on this
>>>
>>> It is actually Jmeter Global Variables exposed by it to use in different
>>> Samplers (like JSR223 andd BSF Samplers etcc) ...
>>>
>>> threads are invoked by Java code (JMeter)...
>
> ok thank u ..any way np..
>
> Basically my problem is that
>
> JMeter is sharing Global Variables and Properties of it to different
> samplers(like JSR223 and BSF ..etc...) But when i access those variables
> through Jruby(JSR223 sampler) it is returning Nil value as above....
>
> anyone had any suggestions/idea on the same ...



below is the JSR223 Jruby Code



require 'java'
java_import 'java.lang.System'
java_import 'javax.script.ScriptContext'
java_import 'javax.script.SimpleScriptContext'
#System.setProperty("org.jruby.embed.localvariable.behavior", 
"transient")
#System.setProperty("org.jruby.embed.localcontext.scope", "threadsafe")
#System.setProperty("org.jruby.embed.termination", "true")
mgr = javax.script.ScriptEngineManager.new
jruby_engine = mgr.getEngineByName("jruby")

jruby_engine.eval("puts $vars.get('inputFilePath')")


C:\\Pbvt\\6KB.txt
C:\\Pbvt\\6KB.txt
NoMethodError: undefined method `get' for nil:NilClass
  (root) at <script>:1
  (root) at <script>:13
C:\\Pbvt\\6KB.txtNoMethodError: undefined method `get' for nil:NilClass

you can see tat 3 threads got file path correctly.. .remaining 2 are 
throwing Nil error....
Posted by Yoko Harada (Guest)
on 2013-02-17 19:33
(Received via mailing list)
Hi,

Have you tried singlethread model?

System.setProperty("org.jruby.embed.localcontext.scope", "singlethread")

In general, the code which uses JSR223 assumes that it is singlethread
and no concurrency is there.
If not yet, it is worth to give a try.

-Yoko
Posted by Muthu K. (muthu_k)
on 2013-02-18 06:02
Yoko Harada wrote in post #1097438:
> Hi,
>
> Have you tried singlethread model?
>
> System.setProperty("org.jruby.embed.localcontext.scope", "singlethread")
>
> In general, the code which uses JSR223 assumes that it is singlethread
> and no concurrency is there.
> If not yet, it is worth to give a try.
>
> -Yoko


hi,

I tried as you suggested like below

require 'java'
java_import 'java.lang.System'
java_import 'javax.script.ScriptContext'
java_import 'javax.script.SimpleScriptContext'

#System.setProperty("org.jruby.embed.localvariable.behavior", 
"transient")
#System.setProperty("org.jruby.embed.localcontext.scope", "threadsafe")
System.setProperty("org.jruby.embed.localcontext.scope", "singlethread")
#System.setProperty("org.jruby.embed.termination", "true")
mgr = javax.script.ScriptEngineManager.new
jruby_engine = mgr.getEngineByName("jruby")
jruby_engine.eval("puts $vars.get('inputFilePath')")


but still getting same error as above.... Jmeter is giving global 
variables correctly(as other scripting languages like javascript 
...etc.. are working fine for the same scenario)....But JRUBY alone i am 
getting this problem ...
Posted by Muthu K. (muthu_k)
on 2013-02-18 10:35
Muthu K. wrote in post #1097517:
> Yoko Harada wrote in post #1097438:
>> Hi,
>>
>> Have you tried singlethread model?
>>
>> System.setProperty("org.jruby.embed.localcontext.scope", "singlethread")
>>
>> In general, the code which uses JSR223 assumes that it is singlethread
>> and no concurrency is there.
>> If not yet, it is worth to give a try.
>>
>> -Yoko
>
>
> hi,
>
> I tried as you suggested like below
>
> require 'java'
> java_import 'java.lang.System'
> java_import 'javax.script.ScriptContext'
> java_import 'javax.script.SimpleScriptContext'
>
> #System.setProperty("org.jruby.embed.localvariable.behavior",
> "transient")
> #System.setProperty("org.jruby.embed.localcontext.scope", "threadsafe")
> System.setProperty("org.jruby.embed.localcontext.scope", "singlethread")
> #System.setProperty("org.jruby.embed.termination", "true")
> mgr = javax.script.ScriptEngineManager.new
> jruby_engine = mgr.getEngineByName("jruby")
> jruby_engine.eval("puts $vars.get('inputFilePath')")
>
>
> but still getting same error as above.... Jmeter is giving global
> variables correctly(as other scripting languages like javascript
> ...etc.. are working fine for the same scenario)....But JRUBY alone i am
> getting this problem ...


below is the full exception i got it from Jmeter.log ... if someone knew 
about this problem .. please reply on this post...


2013/02/18 15:03:56 ERROR - jmeter.protocol.java.sampler.JSR223Sampler: 
Problem in JSR223 script javax.script.ScriptException: 
org.jruby.embed.EvalFailedException: (NoMethodError) undefined method 
`get' for nil:NilClass javax.script.ScriptException: 
org.jruby.embed.EvalFailedException: (NoMethodError) undefined method 
`get' for nil:NilClass
  at 
org.jruby.embed.jsr223.JRubyEngine.wrapException(JRubyEngine.java:104)
  at org.jruby.embed.jsr223.JRubyEngine.eval(JRubyEngine.java:93)
  at org.jruby.embed.jsr223.JRubyEngine.eval(JRubyEngine.java:133)
  at 
org.apache.jmeter.util.JSR223TestElement.processFileOrScript(JSR223TestElement.java:202)
  at 
org.apache.jmeter.protocol.java.sampler.JSR223Sampler.sample(JSR223Sampler.java:70)
  at 
org.apache.jmeter.threads.JMeterThread.process_sampler(JMeterThread.java:428)
  at org.apache.jmeter.threads.JMeterThread.run(JMeterThread.java:256)
  at java.lang.Thread.run(Unknown Source)
Caused by: org.jruby.embed.EvalFailedException: (NoMethodError) 
undefined method `get' for nil:NilClass
  at 
org.jruby.embed.internal.EmbedEvalUnitImpl.run(EmbedEvalUnitImpl.java:133)
  at org.jruby.embed.jsr223.JRubyEngine.eval(JRubyEngine.java:90)
  ... 6 more
Caused by: org.jruby.exceptions.RaiseException: (NoMethodError) 
undefined method `get' for nil:NilClass
  at RUBY.(root)(<script>:1)
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.