2

I'm trying to convert a String to a Long integer, then do some calculations, then convert back to String. The entire code in the beanshell postprocessor is here (depositamount and imagecount are variables already known in JMeter):

Long ttl=Long.valueOf(vars.get("depositamount"));
Long med=Long.valueOf(vars.get("depositamount"));
Long intermediate=med/vars.get("imagecount");

String depamt=intermediate.toString();

vars.put("depositamount",depamt);

Double tlnlast=ttl-depamt * (vars.get("imagecount")-1);

vars.put("lastamt",tlnlast.toString());

vars.put("loopcounter","1");

However, when I run this, I get an error: ERROR - jmeter.util.BeanShellInterpreter: Error invoking bsh method: eval Sourced file: inline evaluation of: ``Long ttl=Long.valueOf(vars.get("depositamount")); Long med=Long.valueOf(vars.get . . . '' : Typed variable declaration : Method Invocation Long.valueOf . I've tried to use Long.parseLongin place of the valueOf, but the same error gets thrown, with the "parseLong" taking the place of "valueOf". I can't figure out what I'm doing wrong. Thanks!

user3476534
  • 220
  • 1
  • 4
  • 15
  • 1
    `Long intermediate=med/vars.get("imagecount");` Not that this is your problem, but are you dividing by a string here? – RaGe Feb 09 '16 at 19:28
  • 1
    More string math here: `(vars.get("imagecount")-1)` – RaGe Feb 09 '16 at 19:29
  • Thank you for finding those. Would have gotten errors there once past this question. I'll have to account for that, too. – user3476534 Feb 09 '16 at 19:29
  • One of the problems with jmeter beanshell blocks is that it tends to swallow the actual exception and throws this generic exception that doesn't really help you chase down the issue. Can you stick your code in a try-catch block and in catch, log actual stacktrace? – RaGe Feb 09 '16 at 19:34
  • Well, I fixed the other string issues you pointed out above, and then put in the try-catch, but it didn't throw any issues anymore. It was probably trying to do the string math and threw a generic error. I'll have to remember the try-catch trick with jmeter in the future. Thank you! – user3476534 Feb 09 '16 at 19:43

1 Answers1

1
  • From performance perspective it's better to use Long.parseLong(String) method
  • Double check your med/vars.get("imagecount") and vars.get("imagecount")-1 methods as vars.get() returns String. If your imagecount variable has different type - use vars.getObject() method instead
  • as per How to debug your Apache JMeter script article, if you add debug(); directive to the very beginning of your Beanshell script - you'll be able to see a lot of debugging information in STDOUT which should be enough to troubleshoot any issue.
Dmitri T
  • 159,985
  • 5
  • 83
  • 133
  • 1
    `valueOf()` uses `parseLong()` underneath. There is no performance difference. see: http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/lang/Long.java#507 – RaGe Feb 10 '16 at 22:13