You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@jmeter.apache.org by Krister Nilsson <kr...@pensionsmyndigheten.se> on 2020/10/01 13:24:52 UTC

JavaScript: subtracting with string?

Hi!

I have tried many things but I come to the conclusion that Jmeter's Javascript behaves a bit differently at least compared to the console in Chrome. Can you please help me out?

I want to do something like this

${__time(yyyy,)}-"1953"

or simplified

2020-"1953"

(It does perhaps look a little bit weird but there is a reason why the second term should be a string.)

Now I need to get an integer returned but with Jmeter I get 67.0 instead of 67. I have tried parseInt() but it has no effect.

Anyone who can help me?

Med vänlig hälsning
Krister Nilsson
____________________________________________________________

Krister Nilsson / översteprest, prestandatest

IT Fond konto och teknisk plattform
Telefon direkt 010-454 21 97, mobil 072-210 21 97
krister.nilsson@pensionsmyndigheten.se<ma...@pensionsmyndigheten.se>

Pensionsmyndigheten,
Telefon vxl 0771-771 771, fax 08-658 13 00, www.pensionsmyndigheten.se<http://www.pensionsmyndigheten.se/>


P Överväg miljöpåverkan innan du skriver ut detta e-brev.


Re: SV: JavaScript: subtracting with string?

Posted by Felix Schumacher <fe...@internetallee.de>.
Am 01.10.20 um 17:47 schrieb Krister Nilsson:
> Thanks Felix!
>
> Your first suggestion does unfortunately have the same problem as before. Still 67.0 .

That might depend on where and how you used it. I tried it in the body
of a JSR223 Sampler set to JavaScript.

>
> I guess I'll have to go for the groovy track. One question though: Can I use the variable __time also in a JSR223 sampler with groovy like
>
> int year = {__time(yyyy,)}

If you use a JSR223 Sampler set to Groovy, you should not (really a must
not) use ${...} expressions in the body of the sampler, as that is
interpreted at a time before the Sampler is evaluated. That leads to
strange problems!

Have a look at the Groovy libraries (for example
http://docs.groovy-lang.org/latest/html/groovy-jdk/java/util/Date.html)
to use them directly.

In Java you could use the Year class from java.time
https://docs.oracle.com/javase/8/docs/api/java/time/Year.html

java.time.Year.now().compareTo(java.time.Year.parse("1953"))

or a bit more readable

import java.time.Year
int x = Year.now() compareTo Year.parse("1953")

Felix

>
> Maybe the format is different and perhaps it does not return an int, but the principle?
>
> BR
> /Krister
>
> -----Ursprungligt meddelande-----
> Från: Felix Schumacher <fe...@internetallee.de> 
> Skickat: den 1 oktober 2020 17:16
> Till: user@jmeter.apache.org
> Ämne: Re: JavaScript: subtracting with string?
>
> Am 01.10.20 um 15:24 schrieb Krister Nilsson:
>> Hi!
>>
>> I have tried many things but I come to the conclusion that Jmeter's Javascript behaves a bit differently at least compared to the console in Chrome. Can you please help me out?
>>
>> I want to do something like this
>>
>> ${__time(yyyy,)}-"1953"
>>
>> or simplified
>>
>> 2020-"1953"
>>
>> (It does perhaps look a little bit weird but there is a reason why the 
>> second term should be a string.)
>>
>> Now I need to get an integer returned but with Jmeter I get 67.0 instead of 67. I have tried parseInt() but it has no effect.
>>
>> Anyone who can help me?
> You could try "" + ${__time(yyyym,)}-"1953"
>
> And JMeter's JavaScript is different to the one in chrome and that difference is one reason, why the builtin JavaScript interpreter has been deprecated with Java 14 and removed with Java 15.
>
> Maybe there are other methods to do the calculation. The JDK has probably a few date conversion method, that you could use (either through JavaScript or through Groovy).
>
> Felix
>
>> Med vänlig hälsning
>> Krister Nilsson
>> ____________________________________________________________
>>
>> Krister Nilsson / översteprest, prestandatest
>>
>> IT Fond konto och teknisk plattform
>> Telefon direkt 010-454 21 97, mobil 072-210 21 97 
>> krister.nilsson@pensionsmyndigheten.se<mailto:krister.nilsson@pensions
>> myndigheten.se>
>>
>> Pensionsmyndigheten,
>> Telefon vxl 0771-771 771, fax 08-658 13 00, 
>> www.pensionsmyndigheten.se<http://www.pensionsmyndigheten.se/>
>>
>>
>> P Överväg miljöpåverkan innan du skriver ut detta e-brev.
>>
>>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@jmeter.apache.org
> For additional commands, e-mail: user-help@jmeter.apache.org
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@jmeter.apache.org
> For additional commands, e-mail: user-help@jmeter.apache.org
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@jmeter.apache.org
For additional commands, e-mail: user-help@jmeter.apache.org


SV: JavaScript: subtracting with string?

Posted by Krister Nilsson <kr...@pensionsmyndigheten.se>.
Thanks Felix!

Your first suggestion does unfortunately have the same problem as before. Still 67.0 .

I guess I'll have to go for the groovy track. One question though: Can I use the variable __time also in a JSR223 sampler with groovy like

int year = {__time(yyyy,)}

Maybe the format is different and perhaps it does not return an int, but the principle?

BR
/Krister

-----Ursprungligt meddelande-----
Från: Felix Schumacher <fe...@internetallee.de> 
Skickat: den 1 oktober 2020 17:16
Till: user@jmeter.apache.org
Ämne: Re: JavaScript: subtracting with string?

Am 01.10.20 um 15:24 schrieb Krister Nilsson:
> Hi!
>
> I have tried many things but I come to the conclusion that Jmeter's Javascript behaves a bit differently at least compared to the console in Chrome. Can you please help me out?
>
> I want to do something like this
>
> ${__time(yyyy,)}-"1953"
>
> or simplified
>
> 2020-"1953"
>
> (It does perhaps look a little bit weird but there is a reason why the 
> second term should be a string.)
>
> Now I need to get an integer returned but with Jmeter I get 67.0 instead of 67. I have tried parseInt() but it has no effect.
>
> Anyone who can help me?

You could try "" + ${__time(yyyym,)}-"1953"

And JMeter's JavaScript is different to the one in chrome and that difference is one reason, why the builtin JavaScript interpreter has been deprecated with Java 14 and removed with Java 15.

Maybe there are other methods to do the calculation. The JDK has probably a few date conversion method, that you could use (either through JavaScript or through Groovy).

Felix

>
> Med vänlig hälsning
> Krister Nilsson
> ____________________________________________________________
>
> Krister Nilsson / översteprest, prestandatest
>
> IT Fond konto och teknisk plattform
> Telefon direkt 010-454 21 97, mobil 072-210 21 97 
> krister.nilsson@pensionsmyndigheten.se<mailto:krister.nilsson@pensions
> myndigheten.se>
>
> Pensionsmyndigheten,
> Telefon vxl 0771-771 771, fax 08-658 13 00, 
> www.pensionsmyndigheten.se<http://www.pensionsmyndigheten.se/>
>
>
> P Överväg miljöpåverkan innan du skriver ut detta e-brev.
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@jmeter.apache.org
For additional commands, e-mail: user-help@jmeter.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@jmeter.apache.org
For additional commands, e-mail: user-help@jmeter.apache.org


Re: JavaScript: subtracting with string?

Posted by Felix Schumacher <fe...@internetallee.de>.
Am 01.10.20 um 15:24 schrieb Krister Nilsson:
> Hi!
>
> I have tried many things but I come to the conclusion that Jmeter's Javascript behaves a bit differently at least compared to the console in Chrome. Can you please help me out?
>
> I want to do something like this
>
> ${__time(yyyy,)}-"1953"
>
> or simplified
>
> 2020-"1953"
>
> (It does perhaps look a little bit weird but there is a reason why the second term should be a string.)
>
> Now I need to get an integer returned but with Jmeter I get 67.0 instead of 67. I have tried parseInt() but it has no effect.
>
> Anyone who can help me?

You could try "" + ${__time(yyyym,)}-"1953"

And JMeter's JavaScript is different to the one in chrome and that
difference is one reason, why the builtin JavaScript interpreter has
been deprecated with Java 14 and removed with Java 15.

Maybe there are other methods to do the calculation. The JDK has
probably a few date conversion method, that you could use (either
through JavaScript or through Groovy).

Felix

>
> Med vänlig hälsning
> Krister Nilsson
> ____________________________________________________________
>
> Krister Nilsson / översteprest, prestandatest
>
> IT Fond konto och teknisk plattform
> Telefon direkt 010-454 21 97, mobil 072-210 21 97
> krister.nilsson@pensionsmyndigheten.se<ma...@pensionsmyndigheten.se>
>
> Pensionsmyndigheten,
> Telefon vxl 0771-771 771, fax 08-658 13 00, www.pensionsmyndigheten.se<http://www.pensionsmyndigheten.se/>
>
>
> P Överväg miljöpåverkan innan du skriver ut detta e-brev.
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@jmeter.apache.org
For additional commands, e-mail: user-help@jmeter.apache.org


Re: JavaScript: subtracting with string?

Posted by Wolfeman <wo...@gmail.com>.
I would probably suggest you use a groovy script to do this. You can use
Java syntax in groovy and you will have access to more powerful functions
related to strings and dates.
https://jmetervn.com/2016/12/05/jsr223-with-groovy-variables-part-1/
This way you can use parse int and get the integer values to subtract from.
Or you could create date objects and use the date class to perform the
subtraction.

On Thu, Oct 1, 2020 at 9:25 AM Krister Nilsson <
krister.nilsson@pensionsmyndigheten.se> wrote:

> Hi!
>
> I have tried many things but I come to the conclusion that Jmeter's
> Javascript behaves a bit differently at least compared to the console in
> Chrome. Can you please help me out?
>
> I want to do something like this
>
> ${__time(yyyy,)}-"1953"
>
> or simplified
>
> 2020-"1953"
>
> (It does perhaps look a little bit weird but there is a reason why the
> second term should be a string.)
>
> Now I need to get an integer returned but with Jmeter I get 67.0 instead
> of 67. I have tried parseInt() but it has no effect.
>
> Anyone who can help me?
>
> Med vänlig hälsning
> Krister Nilsson
> ____________________________________________________________
>
> Krister Nilsson / översteprest, prestandatest
>
> IT Fond konto och teknisk plattform
> Telefon direkt 010-454 21 97, mobil 072-210 21 97
> krister.nilsson@pensionsmyndigheten.se<mailto:
> krister.nilsson@pensionsmyndigheten.se>
>
> Pensionsmyndigheten,
> Telefon vxl 0771-771 771, fax 08-658 13 00, www.pensionsmyndigheten.se<
> http://www.pensionsmyndigheten.se/>
>
>
> P Överväg miljöpåverkan innan du skriver ut detta e-brev.
>
>

-- 
Thanks,
Brian Wolfe
https://www.linkedin.com/in/brian-wolfe-3136425a/