Want to get Integer value only from rational number

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Want to get Integer value only from rational number

Shyam Panjiyar
Hi,


Getting below error :-
Response code: 500
Response message: org.apache.jorphan.util.JMeterException: Error invoking
bsh method: eval    Sourced file: inline evaluation of: ``import
org.apache.http.NameValuePair; import org.apache.http.client.utils.URLEnc .
. . '' : TargetError

Request :-
import org.apache.http.NameValuePair;
import org.apache.http.client.utils.URLEncodedUtils;
import java.net.URI;
import java.util.List;
import java.lang.*;
import java.util.*;
import org.apache.commons.math3.util.Precision;
try
{
 String SFinPlanIDGen = vars.get("323.0");
 System.out.println("SFinPlanIDGen");
int IFinPlanIDGen = Integer.parseInt("SFinPlanIDGen");
int TFinPlanIDGen = Math.round(IFinPlanIDGen);
int NFinPlanIDGen = Integer.valueOf(IFinPlanIDGen).intValue();
System.out.println(NFinPlanIDGen);
vars.put("FinPlanIDGenerated",NFinPlanIDGen);
System.out.println(FinPlanIDGenerated);
}
catch (Exception ex){
    log.warn("Error in my script", ex);
    throw ex; // elsewise JMeter will "swallow" the above exception
}

Kindly help to resolve it.

Sincerely,

Shyam Kishor

+91-7506056770

 

Reply | Threaded
Open this post in threaded view
|

Re: Want to get Integer value only from rational number

Felix Schumacher

Am 09.05.19 um 07:21 schrieb Shyam Panjiyar:

> Hi,
>
>
> Getting below error :-
> Response code: 500
> Response message: org.apache.jorphan.util.JMeterException: Error invoking
> bsh method: eval    Sourced file: inline evaluation of: ``import
> org.apache.http.NameValuePair; import org.apache.http.client.utils.URLEnc .
> . . '' : TargetError
>
> Request :-
> import org.apache.http.NameValuePair;
> import org.apache.http.client.utils.URLEncodedUtils;
> import java.net.URI;
> import java.util.List;
> import java.lang.*;
> import java.util.*;
> import org.apache.commons.math3.util.Precision;
> try
> {
>  String SFinPlanIDGen = vars.get("323.0");
>  System.out.println("SFinPlanIDGen");
> int IFinPlanIDGen = Integer.parseInt("SFinPlanIDGen");
> int TFinPlanIDGen = Math.round(IFinPlanIDGen);
> int NFinPlanIDGen = Integer.valueOf(IFinPlanIDGen).intValue();
> System.out.println(NFinPlanIDGen);
> vars.put("FinPlanIDGenerated",NFinPlanIDGen);
> System.out.println(FinPlanIDGenerated);
> }
> catch (Exception ex){
>     log.warn("Error in my script", ex);
>     throw ex; // elsewise JMeter will "swallow" the above exception
> }
>
> Kindly help to resolve it.

I get other exceptions than you seem to get, so some general advise:

* Variables in JMeter should have meaningful names. "323.0" is IMHO not
a good name

* Variables that are not defined in JMeter will be reported back as the
name of the variable, so SFinPlanIDGen will probably be initialized with
"323.0" as a value

* Integer.parseInt(String) expects a value that can be parsed as an
integer. "SFinPlanIDGen" is not such a value. You probably wanted to
place the variable with the same name there: ... =
Integer.parseInt(SFinPlanIDGen);

* JMeter variables are String values by default. If you want to place
other objects as values, you have to use vars.putObject(String, Object)
instead of vars.put(String, Object)

* Don't use System.out.println. Use log.info or log.debug instead

* I had no problems with the exception, even when I removed the try
catch block

* BeanShell is not a good choice for your scripts. groovy is way better
(for complex scripts). jexl3 can be a good alternative for simple
expressions

* The above logic seems to be equivalent to "${__jexl3((${testVar} + 0.5
).intValue().toString())}" (Note that I haven't seen an easy way to
round numbers, so I emulated it)

* I don't know which version of JMeter, Java or OS you are using, so
things might behave differently on your installation

Felix


>
> Sincerely,
>
> Shyam Kishor
>
> +91-7506056770
>
>  
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Want to get Integer value only from rational number

Shyam Panjiyar
Hi ,

Could you please help with Bean Shell Sampler Script for below  requirement :-

Input - String sequence from JSON Extractor as 123.0

Output - String sequence in variable as 123

Thanks & Regards,
Shyam Panjiyar



---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: Want to get Integer value only from rational number

Felix Schumacher

Am 10.05.19 um 12:54 schrieb Shyam Panjiyar:
> Hi ,
>
> Could you please help with Bean Shell Sampler Script for below  requirement :-
>
> Input - String sequence from JSON Extractor as 123.0
>
> Output - String sequence in variable as 123

Last mail you asked about a conversion from JSON Extractor
123.0/124.0/125.0 to 123/124/125.

As it is almost the question about extracting multiple values from json
on stackoverflow
[https://stackoverflow.com/questions/55819872/extracting-and-adding-multiple-values-from-json-in-jmeter]
I will quote and enhance my answer here inline (Note that I used ',' as
the separator, as it is the default as I believe):

Assuming you have a JSON structure like this

|{"body":[{"count":0.0 },{"count":23.0 },{"count":-1.0 },{"count":20.0
},{"no_count_here":"really"}]} and your JSON Extractor uses
"countFromResponse" as "Names of created variables", "$.body[*].count"
as "JSON Path expression", "-1" for "Match No." and check "Compute
concatenation var". |

Then your Post Processor will add a variable named |"countFromResponse|"
with the content |"0.0,23.0,-1.0,20.0"|. To round these values, you have
to split the string at each comma, convert the string snippets to Double
values and round them. Then convert them back to strings and join those
again. That value can then be stored in another variable named
|"countFromResponse_ROUNDED|".

This can be accomplished using a JSR223 Post Processor, that uses the
following groovy code:

|overall =vars.get("countFromResponse_ALL").split(",").collect
{Math.round(Double.valueOf(it)).toString()} .join(",")
vars.put("countFromResponse_ROUNDED",overall)|

The conversion back to string is important, as all normal variables in
JMeter will be treated as strings.

Felix

>
> Thanks & Regards,
> Shyam Panjiyar
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>
Reply | Threaded
Open this post in threaded view
|

RE: Want to get Integer value only from rational number

Shyam Panjiyar
In reply to this post by Felix Schumacher
Thanks for Reply

Resolved now

try
{
 int number = ${__jexl3((${FinPlanIDGen} + 0.0 ).intValue().toString())};
 log.info("The converted integer is " + number);
 vars.put("FinPlanIDGenerated", String.valueOf(number));
}
catch (Exception ex){
    log.warn("Error in my script", ex);
    throw ex; // elsewise JMeter will "swallow" the above exception
}

Thanks & Regards,
Shyam Kishor Panjiyar
 
-----Original Message-----
From: Shyam Panjiyar <[hidden email]>
Sent: 10 May 2019 16:24
To: 'JMeter Users List' <[hidden email]>
Subject: Want to get Integer value only from rational number

Hi ,

Could you please help with Bean Shell Sampler Script for below  requirement :-

Input - String sequence from JSON Extractor as 123.0

Output - String sequence in variable as 123

Thanks & Regards,
Shyam Panjiyar



---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: Want to get Integer value only from rational number

Felix Schumacher

Am 10.05.19 um 14:42 schrieb Shyam Panjiyar:
> Thanks for Reply
>
> Resolved now

good to know, but please don't mix ${varname} and JSR223 Samplers. The
replacement of the variable to a String value will take place before the
script is sent to the Sampler. This will work against the caching of the
scripts, which is default now.

The " ${var} + 0.5" in my first reply is meant to emulate rounding with
the common round-up method. A value of "0.0" will do nothing and should
be left out.

The .toString() in your example is looking really strange, as it seems
to imply that you are assigning a string value to an int variable. This
is of course not the case as you are mixing ${...} with JSR 223
Samplers, which is -- I repeat -- not a good idea.

If you insist on using a JSR 223 Sampler, use the tools that they provide:

 int number =
Math.round(Double.valueOf(vars.get('FinPlanIDGen'))).toInteger();

I gave the example with jexl3 function in case you wanted to convert the
variable without a sampler or post-processor.

Felix

>
> try
> {
>  int number = ${__jexl3((${FinPlanIDGen} + 0.0 ).intValue().toString())};
>  log.info("The converted integer is " + number);
>  vars.put("FinPlanIDGenerated", String.valueOf(number));
> }
> catch (Exception ex){
>     log.warn("Error in my script", ex);
>     throw ex; // elsewise JMeter will "swallow" the above exception
> }
>
> Thanks & Regards,
> Shyam Kishor Panjiyar
>  
> -----Original Message-----
> From: Shyam Panjiyar <[hidden email]>
> Sent: 10 May 2019 16:24
> To: 'JMeter Users List' <[hidden email]>
> Subject: Want to get Integer value only from rational number
>
> Hi ,
>
> Could you please help with Bean Shell Sampler Script for below  requirement :-
>
> Input - String sequence from JSON Extractor as 123.0
>
> Output - String sequence in variable as 123
>
> Thanks & Regards,
> Shyam Panjiyar
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]