2

I can't use Java stream inside JSR223 script in JMeter,

I tried with Java stream

List<String> myList =
    Arrays.asList("a1", "a2", "b1", "c2", "c1");

myList
    .stream()
    .filter(s -> s.startsWith("c"))
    .map(String::toUpperCase)
    .sorted()
    .forEach(System.out::println);

With exception

2019-04-22 13:44:49,808 ERROR o.a.j.p.j.s.JSR223Sampler: Problem in JSR223 script JSR223 Sampler, message: javax.script.ScriptException: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Script9.groovy: 6: unexpected token: -> @ line 6, column 15.
       .filter(s -> s.startsWith("c"))
                 ^

1 error

javax.script.ScriptException: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Script9.groovy: 6: unexpected token: -> @ line 6, column 15.
       .filter(s -> s.startsWith("c"))
                 ^

1 error

    at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.compile(GroovyScriptEngineImpl.java:187) ~[groovy-all-2.4.16.jar:2.4.16]
    at org.apache.jmeter.util.JSR223TestElement.processFileOrScript(JSR223TestElement.java:217) ~[ApacheJMeter_core.jar:5.1.1 r1855137]
    at org.apache.jmeter.protocol.java.sampler.JSR223Sampler.sample(JSR223Sampler.java:71) [ApacheJMeter_java.jar:5.1.1 r1855137]
    at org.apache.jmeter.threads.JMeterThread.doSampling(JMeterThread.java:622) [ApacheJMeter_core.jar:5.1.1 r1855137]
    at org.apache.jmeter.threads.JMeterThread.executeSamplePackage(JMeterThread.java:546) [ApacheJMeter_core.jar:5.1.1 r1855137]
    at org.apache.jmeter.threads.JMeterThread.processSampler(JMeterThread.java:486) [ApacheJMeter_core.jar:5.1.1 r1855137]
    at org.apache.jmeter.threads.JMeterThread.run(JMeterThread.java:253) [ApacheJMeter_core.jar:5.1.1 r1855137]

I tried with groovy stream

def sample = ['Groovy', 'Gradle', 'Grails', 'Spock'] as String[]

def result = sample.stream()  // Use stream() on array objects
               .filter { s -> s.startsWith('Gr') }
               .map { s -> s.toUpperCase() }
               .toList()  // toList() added to Stream by Groovy

With exception:

019-04-22 13:42:40,345 ERROR o.a.j.p.j.s.JSR223Sampler: Problem in JSR223 script JSR223 Sampler, message: javax.script.ScriptException: groovy.lang.MissingMethodException: No signature of method: [Ljava.lang.String;.stream() is applicable for argument types: () values: []
Possible solutions: grep(), size(), sort(), sum(), grep(), head()
javax.script.ScriptException: groovy.lang.MissingMethodException: No signature of method: [Ljava.lang.String;.stream() is applicable for argument types: () values: []
Possible solutions: grep(), size(), sort(), sum(), grep(), head()
    at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:324) ~[groovy-all-2.4.16.jar:2.4.16]
    at org.codehaus.groovy.jsr223.GroovyCompiledScript.eval(GroovyCompiledScript.java:72) ~[groovy-all-2.4.16.jar:2.4.16]
    at javax.script.CompiledScript.eval(Unknown Source) ~[?:1.8.0_191]

And beanshell isn't supporting streams

Lambda expressions still not supported in beanshell

Ori Marko
  • 56,308
  • 23
  • 131
  • 233

1 Answers1

1

Groovy does not support lambda however you can replace lambda with Closure like below:

​List<String> myList = Arrays.asList("a1", "a2", "b1", "c2", "c1");  
myList.stream()
       .filter{s -> s.startsWith("c")}
       .map{s-> s.toUpperCase()}
       .sorted()
       .forEach{s->println s};​

Output:

C1
C2

Your second case gives you the error as the sample is an Array and stream() is not available on Array. You can get stream by Arrays.stream(array).

import java.util.stream.Collectors;

def sample = ['Groovy', 'Gradle', 'Grails', 'Spock'] as String[]  
def result = Arrays.stream(sample )
                        .filter { s -> s.startsWith('Gr') }                
                        .map { s -> s.toUpperCase() }                
                        .collect(Collectors.toList())​
Community
  • 1
  • 1
Amit Bera
  • 7,075
  • 1
  • 19
  • 42
  • Getting different exception on inside the `forEach`: `javax.script.ScriptException: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: Script11.groovy: 6: Invalid variable name. Must start with a letter but was: ​ . At [6:31] @ line 6, column 31. .forEach{s->println s};` – Ori Marko Apr 22 '19 at 11:00
  • @user7294900 Can you please double check your syntax. It is working fine in my case. Please visit the link https://groovyconsole.appspot.com/script/5173177101582336 – Amit Bera Apr 22 '19 at 11:07
  • I'm executing inside JMeter, using JSR223 Sampler, is that are you executing? – Ori Marko Apr 22 '19 at 11:09
  • For your 2nd case https://groovyconsole.appspot.com/script/5151353835880448 – Amit Bera Apr 22 '19 at 11:09
  • No, I am executing on groovy console but it should work on Jmeter – Amit Bera Apr 22 '19 at 11:10
  • How to print all stream results? – Ori Marko Apr 22 '19 at 11:12
  • @user7294900 from your error (commented) it looks like there is some other issue which is not related to Stream. Maybe bad variable name declaration. – Amit Bera Apr 22 '19 at 11:13
  • There are several ways to print. You can Use `forEach{s->println s};​` to print the result or just loop over the result collection and simple print it. – Amit Bera Apr 22 '19 at 11:15
  • `forEach{s->println s}` doesn't works, or syntax as `OUT::println` – Ori Marko Apr 22 '19 at 11:17
  • @user7294900 Use `forEach{s->OUT.println(s)}` in case of Jmeter. – Amit Bera Apr 22 '19 at 11:21