Mahesh A R
Mahesh A R

Reputation: 17

Jmeter unable to execute Stream<JSON> in Java from jmx file

I am writing a program to perform CRUD operation in TypeDB

private static Stream fetchAllUsers(TypeDBDriver driver, String dbName) {

 Stream<JSON>  answers = null;
 try (TypeDBSession session = driver.session(dbName, TypeDBSession.Type.DATA)) {
     try (TypeDBTransaction tx = session.transaction(TypeDBTransaction.Type.READ)) {
         String query = "match\r\n"
                + "$user isa user, has name \"Alex\";\r\n"
                + "\r\n"
                + "(subject: $user, action: $act) isa permission;\r\n"
                + "fetch\r\n"
                + "$act: name;\r\n"
                + "";
        System.out.println("CP-4w3");
     
      answers = tx.query().fetch(query);

       System.out.println("CP-5-..-");
   

answers.limit(100).forEach(json -> System.out.println("JSON: " + json.toString()));

My problem:

What does not work? The same class, I built as a jar and tried to call from a jmx file. It works exactly before

answers.limit(100).forEach(json -> System.out.println("JSON: " + json.toString()));

The libraries I had imported are

import com.vaticle.typedb.driver.api.*;
import com.vaticle.typedb.driver.api.answer.ConceptMap;
import com.vaticle.typedb.driver.TypeDB;
import com.vaticle.typedb.driver.api.answer.JSON;
import com.vaticle.typedb.driver.api.query.QueryManager;

import com.vaticle.typedb.driver.common.exception.TypeDBDriverException;
import com.vaticle.typeql.lang.TypeQL;

After that it fails and no response is seen Jmeter. In Jmeter Consle I get an error like below

2024-07-05 15:14:54,183 ERROR o.a.j.u.BeanShellInterpreter: Error invoking bsh method: eval Sourced file: inline evaluation of: ``import com.jmeter.typedb.stresstest.StressTest_3; import com.vaticle.typedb.driv . . . '' : Method Invocation myObj.processRequests

2024-07-05 15:14:54,183 WARN

o.a.j.p.j.s.BeanShellSampler: Exception executing script. org.apache.jorphan.util.JMeterException: Error invoking bsh method: eval Sourced file: inline evaluation of: ``import com.jmeter.typedb.stresstest.StressTest_3; import com.vaticle.typedb.driv . . . '' : Method Invocation myObj.processRequests

Inside my Jmeter BeanSampler, below is how I have scripted

All jars are placed inside the JMETER /lib folder

import com.jmeter.typedb.stresstest.StressTest_3;
    
String DB_NAME = "mhsh-stress-test1";
StressTest_3 myObj=new StressTest_3();

 myObj.processRequests();
System.out.println("Setup Over.");

Can any one help me on this please? What could be going wrong while calling from Jmeter jmx file alone? But works when I call from a class Also to note, even answers.count(); throws the same error. I am unable to discover what is happening.

Upvotes: -1

Views: 46

Answers (2)

Mahesh A R
Mahesh A R

Reputation: 17

@Ivan G

Thanks for the suggestion. Actually when I tried with Groovy, I was able to narrow down the actual issue. Then I moved to normal BeanShell Sampler and tried that also worked now.

What I had missed was, though I added all the TypeDB libraries but I missed another dependent library which never got reflected in console when I used BeanShell Sampler jmx file

The library I missed was "com.eclipsesource.json.Json", which was internally a part of "com.vaticle.typedb.driver.api.answer.JSON"

which I had used in my

Stream<JSON> answers= tx.query().fetch(query);

Still one thing is that the final response didn't come to my console, but a file write showed that the response is being written.

Thanks Ivan for the suggestion

Upvotes: -1

Ivan G
Ivan G

Reputation: 2707

  1. Beanshell != Java, it has different syntax and the closest match is Java 5 which didn't support diamond operators
  2. Since JMeter 3.1 it's recommended to use JSR223 Test Elements and Groovy language for scripting due to performance reasons (Groovy scripts can be compiled and cached and Beanshell are being interpreted each time)

So I'm pretty sure that if you switch to JSR223 Sampler with Groovy language the error will go away as in the majority of cases valid Java code will be valid Groovy code, moreover your code will run much faster and consume less resources. More information: Beanshell vs. JSR223 vs. Java For JMeter: Complete Showdown

Upvotes: 1

Related Questions