You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@jmeter.apache.org by Hiro Protagonist <hi...@googlemail.com> on 2008/10/23 13:47:58 UTC
How to generate random dates
Hi team,
I need to generate random dates in JMeter. I know that by merely using
the random function and then 'building' the date string I can get 90% of
the way there - however, in order no to risk faulty dates, I always lose
out on dates after the 28th.
The obvious problem here is the interdependency between the months and
their respective number of days.
Does anyone know of a quick and easy solution ?
Thanks !
hiro
---------------------------------------------------------------------
To unsubscribe, e-mail: jmeter-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: jmeter-user-help@jakarta.apache.org
Re: How to generate random dates
Posted by Hiro Protagonist <hi...@googlemail.com>.
Andrey,
Thank you very much. *reference bookmarked*
I didn't know about the zero-bounded month method. That explains it.
Thanks again.
hiro
On Fri, 2008-10-24 at 11:51 +0400, Andrey Beznogov wrote:
> Hi,
>
> please read the reference for javascript Date object - for example here:
>
> https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Date
>
> As written there, for the Date object "Months: 0 (January) to 11
> (December)" - so getMonths() returns the number between 0 and 11 - and
> 9 corresponds to October and not September.
>
> Javascript should not have a problem with long numbers if the date is
> between 1900 and 2100. So the following script
>
> var startMillis = new Date(1900,0,1).getTime(); //Get millis for
> January 1st 1900
> var endMillis = new Date(2100,0,1).getTime(); //Get millis for January 1st 2100
> var randomDate = new Date(startMillis +
> Math.random()*(endMillis-startMillis)); //new Date() with millis set
> to random value between startMillis and endMillis
> randomDate.toUTCString(); //Output UTC string for randomDate
>
> should work fine.
>
> Regards,
> Andrey
>
> On Thu, Oct 23, 2008 at 10:01 PM, Hiro Protagonist
> <hi...@googlemail.com> wrote:
> > Laurent,
> >
> > I think Javascript has a problem with the long numbers...
> > I tried your suggestion but the problem is that javascript cant handle
> > the longs.
> >
> > My other attempt:
> >
> > ${__javaScript(var mydate=new Date();
> > mydate.setDate(mydate.getDate()-${__Random(1,14)});
> > mydate.getDate()+"/"+mydate.getMonth()+"/"+mydate.getFullYear();)}
> >
> > also failed, since it always shows me the month of September (9) even
> > though we are obviously in October and the number of days subtracted
> > from today should be between 1 and 14....
> > Any ideas where I'm going wrong ?
> >
> > Thanks !
> >
> > hiro
> >
> > On Thu, 2008-10-23 at 17:14 +0200, Laurent Perez wrote:
> >> shameless blog copy and paste, looks good enough :
> >>
> >> http://blogs.circlesource.com/2008/07/17/generating-random-datetime-between-2-different-datestimestamps-in-java/
> >>
> >> (java code, if it does not adapt under javascript, you could use a
> >> beanshell script within jmeter)
> >>
> >> laurent
> >>
> >> 2008/10/23 Hiro Protagonist <hi...@googlemail.com>:
> >> > Andrey, All,
> >> >
> >> > I can't seem to get it to generate random dates that have to lie between
> >> > two date boundaries. I've tried sth like this (hack !!):
> >> >
> >> > ${__javaScript(
> >> > var today_date=new Date();
> >> > var old_date=new Date();
> >> > old_date.setDate(today_date.getDate()-5);
> >> > var old_date_int = old_date.getTime();
> >> > var today_date_int = today_date.getTime();
> >> > var random_date_int=old_date_int
> >> > +(Math.random()*(today_date_int-old_date_int));
> >> > random_date_int.toUTCString();)}
> >> >
> >> > but when I use this it doesn't evaulate. Does anyone have anything
> >> > 'out-of-the-box' that works ?
> >> >
> >> > TIA
> >> > hiro
> >> >
> >> > On Thu, 2008-10-23 at 16:05 +0400, Andrey Beznogov wrote:
> >> >> Hi,
> >> >>
> >> >> the best way would be probably randomizing the Time part (the number
> >> >> of milliseconds since 1970/01/01) of the Date(). For example in
> >> >> javascript
> >> >>
> >> >> var mydate=new Date();
> >> >> mydate.setTime(mydate.getTime()*Math.random());
> >> >> mydate.toUTCString();
> >> >>
> >> >> will output a date string in UTC format (like "Sat, 19 Jan 1985
> >> >> 06:22:28 GMT") for a date between now and 1970/01/01.
> >> >>
> >> >> Regards,
> >> >> Andrey
> >> >>
> >> >> On Thu, Oct 23, 2008 at 3:47 PM, Hiro Protagonist
> >> >> <hi...@googlemail.com> wrote:
> >> >> > Hi team,
> >> >> >
> >> >> > I need to generate random dates in JMeter. I know that by merely using
> >> >> > the random function and then 'building' the date string I can get 90% of
> >> >> > the way there - however, in order no to risk faulty dates, I always lose
> >> >> > out on dates after the 28th.
> >> >> > The obvious problem here is the interdependency between the months and
> >> >> > their respective number of days.
> >> >> > Does anyone know of a quick and easy solution ?
> >> >> >
> >> >> > Thanks !
> >> >> >
> >> >> > hiro
> >> >> >
> >> >> >
> >> >> > ---------------------------------------------------------------------
> >> >> > To unsubscribe, e-mail: jmeter-user-unsubscribe@jakarta.apache.org
> >> >> > For additional commands, e-mail: jmeter-user-help@jakarta.apache.org
> >> >> >
> >> >> >
> >> >>
> >> >>
> >> >>
> >> >
> >> >
> >> > ---------------------------------------------------------------------
> >> > To unsubscribe, e-mail: jmeter-user-unsubscribe@jakarta.apache.org
> >> > For additional commands, e-mail: jmeter-user-help@jakarta.apache.org
> >> >
> >> >
> >>
> >>
> >>
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: jmeter-user-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail: jmeter-user-help@jakarta.apache.org
> >
> >
>
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: jmeter-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: jmeter-user-help@jakarta.apache.org
Re: How to generate random dates
Posted by Andrey Beznogov <an...@googlemail.com>.
Hi,
please read the reference for javascript Date object - for example here:
https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Date
As written there, for the Date object "Months: 0 (January) to 11
(December)" - so getMonths() returns the number between 0 and 11 - and
9 corresponds to October and not September.
Javascript should not have a problem with long numbers if the date is
between 1900 and 2100. So the following script
var startMillis = new Date(1900,0,1).getTime(); //Get millis for
January 1st 1900
var endMillis = new Date(2100,0,1).getTime(); //Get millis for January 1st 2100
var randomDate = new Date(startMillis +
Math.random()*(endMillis-startMillis)); //new Date() with millis set
to random value between startMillis and endMillis
randomDate.toUTCString(); //Output UTC string for randomDate
should work fine.
Regards,
Andrey
On Thu, Oct 23, 2008 at 10:01 PM, Hiro Protagonist
<hi...@googlemail.com> wrote:
> Laurent,
>
> I think Javascript has a problem with the long numbers...
> I tried your suggestion but the problem is that javascript cant handle
> the longs.
>
> My other attempt:
>
> ${__javaScript(var mydate=new Date();
> mydate.setDate(mydate.getDate()-${__Random(1,14)});
> mydate.getDate()+"/"+mydate.getMonth()+"/"+mydate.getFullYear();)}
>
> also failed, since it always shows me the month of September (9) even
> though we are obviously in October and the number of days subtracted
> from today should be between 1 and 14....
> Any ideas where I'm going wrong ?
>
> Thanks !
>
> hiro
>
> On Thu, 2008-10-23 at 17:14 +0200, Laurent Perez wrote:
>> shameless blog copy and paste, looks good enough :
>>
>> http://blogs.circlesource.com/2008/07/17/generating-random-datetime-between-2-different-datestimestamps-in-java/
>>
>> (java code, if it does not adapt under javascript, you could use a
>> beanshell script within jmeter)
>>
>> laurent
>>
>> 2008/10/23 Hiro Protagonist <hi...@googlemail.com>:
>> > Andrey, All,
>> >
>> > I can't seem to get it to generate random dates that have to lie between
>> > two date boundaries. I've tried sth like this (hack !!):
>> >
>> > ${__javaScript(
>> > var today_date=new Date();
>> > var old_date=new Date();
>> > old_date.setDate(today_date.getDate()-5);
>> > var old_date_int = old_date.getTime();
>> > var today_date_int = today_date.getTime();
>> > var random_date_int=old_date_int
>> > +(Math.random()*(today_date_int-old_date_int));
>> > random_date_int.toUTCString();)}
>> >
>> > but when I use this it doesn't evaulate. Does anyone have anything
>> > 'out-of-the-box' that works ?
>> >
>> > TIA
>> > hiro
>> >
>> > On Thu, 2008-10-23 at 16:05 +0400, Andrey Beznogov wrote:
>> >> Hi,
>> >>
>> >> the best way would be probably randomizing the Time part (the number
>> >> of milliseconds since 1970/01/01) of the Date(). For example in
>> >> javascript
>> >>
>> >> var mydate=new Date();
>> >> mydate.setTime(mydate.getTime()*Math.random());
>> >> mydate.toUTCString();
>> >>
>> >> will output a date string in UTC format (like "Sat, 19 Jan 1985
>> >> 06:22:28 GMT") for a date between now and 1970/01/01.
>> >>
>> >> Regards,
>> >> Andrey
>> >>
>> >> On Thu, Oct 23, 2008 at 3:47 PM, Hiro Protagonist
>> >> <hi...@googlemail.com> wrote:
>> >> > Hi team,
>> >> >
>> >> > I need to generate random dates in JMeter. I know that by merely using
>> >> > the random function and then 'building' the date string I can get 90% of
>> >> > the way there - however, in order no to risk faulty dates, I always lose
>> >> > out on dates after the 28th.
>> >> > The obvious problem here is the interdependency between the months and
>> >> > their respective number of days.
>> >> > Does anyone know of a quick and easy solution ?
>> >> >
>> >> > Thanks !
>> >> >
>> >> > hiro
>> >> >
>> >> >
>> >> > ---------------------------------------------------------------------
>> >> > To unsubscribe, e-mail: jmeter-user-unsubscribe@jakarta.apache.org
>> >> > For additional commands, e-mail: jmeter-user-help@jakarta.apache.org
>> >> >
>> >> >
>> >>
>> >>
>> >>
>> >
>> >
>> > ---------------------------------------------------------------------
>> > To unsubscribe, e-mail: jmeter-user-unsubscribe@jakarta.apache.org
>> > For additional commands, e-mail: jmeter-user-help@jakarta.apache.org
>> >
>> >
>>
>>
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: jmeter-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: jmeter-user-help@jakarta.apache.org
>
>
--
diem perdidi
---------------------------------------------------------------------
To unsubscribe, e-mail: jmeter-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: jmeter-user-help@jakarta.apache.org
Re: How to generate random dates
Posted by Hiro Protagonist <hi...@googlemail.com>.
Laurent,
I think Javascript has a problem with the long numbers...
I tried your suggestion but the problem is that javascript cant handle
the longs.
My other attempt:
${__javaScript(var mydate=new Date();
mydate.setDate(mydate.getDate()-${__Random(1,14)});
mydate.getDate()+"/"+mydate.getMonth()+"/"+mydate.getFullYear();)}
also failed, since it always shows me the month of September (9) even
though we are obviously in October and the number of days subtracted
from today should be between 1 and 14....
Any ideas where I'm going wrong ?
Thanks !
hiro
On Thu, 2008-10-23 at 17:14 +0200, Laurent Perez wrote:
> shameless blog copy and paste, looks good enough :
>
> http://blogs.circlesource.com/2008/07/17/generating-random-datetime-between-2-different-datestimestamps-in-java/
>
> (java code, if it does not adapt under javascript, you could use a
> beanshell script within jmeter)
>
> laurent
>
> 2008/10/23 Hiro Protagonist <hi...@googlemail.com>:
> > Andrey, All,
> >
> > I can't seem to get it to generate random dates that have to lie between
> > two date boundaries. I've tried sth like this (hack !!):
> >
> > ${__javaScript(
> > var today_date=new Date();
> > var old_date=new Date();
> > old_date.setDate(today_date.getDate()-5);
> > var old_date_int = old_date.getTime();
> > var today_date_int = today_date.getTime();
> > var random_date_int=old_date_int
> > +(Math.random()*(today_date_int-old_date_int));
> > random_date_int.toUTCString();)}
> >
> > but when I use this it doesn't evaulate. Does anyone have anything
> > 'out-of-the-box' that works ?
> >
> > TIA
> > hiro
> >
> > On Thu, 2008-10-23 at 16:05 +0400, Andrey Beznogov wrote:
> >> Hi,
> >>
> >> the best way would be probably randomizing the Time part (the number
> >> of milliseconds since 1970/01/01) of the Date(). For example in
> >> javascript
> >>
> >> var mydate=new Date();
> >> mydate.setTime(mydate.getTime()*Math.random());
> >> mydate.toUTCString();
> >>
> >> will output a date string in UTC format (like "Sat, 19 Jan 1985
> >> 06:22:28 GMT") for a date between now and 1970/01/01.
> >>
> >> Regards,
> >> Andrey
> >>
> >> On Thu, Oct 23, 2008 at 3:47 PM, Hiro Protagonist
> >> <hi...@googlemail.com> wrote:
> >> > Hi team,
> >> >
> >> > I need to generate random dates in JMeter. I know that by merely using
> >> > the random function and then 'building' the date string I can get 90% of
> >> > the way there - however, in order no to risk faulty dates, I always lose
> >> > out on dates after the 28th.
> >> > The obvious problem here is the interdependency between the months and
> >> > their respective number of days.
> >> > Does anyone know of a quick and easy solution ?
> >> >
> >> > Thanks !
> >> >
> >> > hiro
> >> >
> >> >
> >> > ---------------------------------------------------------------------
> >> > To unsubscribe, e-mail: jmeter-user-unsubscribe@jakarta.apache.org
> >> > For additional commands, e-mail: jmeter-user-help@jakarta.apache.org
> >> >
> >> >
> >>
> >>
> >>
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: jmeter-user-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail: jmeter-user-help@jakarta.apache.org
> >
> >
>
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: jmeter-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: jmeter-user-help@jakarta.apache.org
Re: How to generate random dates
Posted by Laurent Perez <ha...@gmail.com>.
shameless blog copy and paste, looks good enough :
http://blogs.circlesource.com/2008/07/17/generating-random-datetime-between-2-different-datestimestamps-in-java/
(java code, if it does not adapt under javascript, you could use a
beanshell script within jmeter)
laurent
2008/10/23 Hiro Protagonist <hi...@googlemail.com>:
> Andrey, All,
>
> I can't seem to get it to generate random dates that have to lie between
> two date boundaries. I've tried sth like this (hack !!):
>
> ${__javaScript(
> var today_date=new Date();
> var old_date=new Date();
> old_date.setDate(today_date.getDate()-5);
> var old_date_int = old_date.getTime();
> var today_date_int = today_date.getTime();
> var random_date_int=old_date_int
> +(Math.random()*(today_date_int-old_date_int));
> random_date_int.toUTCString();)}
>
> but when I use this it doesn't evaulate. Does anyone have anything
> 'out-of-the-box' that works ?
>
> TIA
> hiro
>
> On Thu, 2008-10-23 at 16:05 +0400, Andrey Beznogov wrote:
>> Hi,
>>
>> the best way would be probably randomizing the Time part (the number
>> of milliseconds since 1970/01/01) of the Date(). For example in
>> javascript
>>
>> var mydate=new Date();
>> mydate.setTime(mydate.getTime()*Math.random());
>> mydate.toUTCString();
>>
>> will output a date string in UTC format (like "Sat, 19 Jan 1985
>> 06:22:28 GMT") for a date between now and 1970/01/01.
>>
>> Regards,
>> Andrey
>>
>> On Thu, Oct 23, 2008 at 3:47 PM, Hiro Protagonist
>> <hi...@googlemail.com> wrote:
>> > Hi team,
>> >
>> > I need to generate random dates in JMeter. I know that by merely using
>> > the random function and then 'building' the date string I can get 90% of
>> > the way there - however, in order no to risk faulty dates, I always lose
>> > out on dates after the 28th.
>> > The obvious problem here is the interdependency between the months and
>> > their respective number of days.
>> > Does anyone know of a quick and easy solution ?
>> >
>> > Thanks !
>> >
>> > hiro
>> >
>> >
>> > ---------------------------------------------------------------------
>> > To unsubscribe, e-mail: jmeter-user-unsubscribe@jakarta.apache.org
>> > For additional commands, e-mail: jmeter-user-help@jakarta.apache.org
>> >
>> >
>>
>>
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: jmeter-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: jmeter-user-help@jakarta.apache.org
>
>
--
<a href="http://in-pocket.blogspot.com">http://in-pocket.blogspot.com
- Mobile world, technology and more</a>
---------------------------------------------------------------------
To unsubscribe, e-mail: jmeter-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: jmeter-user-help@jakarta.apache.org
Re: How to generate random dates
Posted by Hiro Protagonist <hi...@googlemail.com>.
Andrey, All,
I can't seem to get it to generate random dates that have to lie between
two date boundaries. I've tried sth like this (hack !!):
${__javaScript(
var today_date=new Date();
var old_date=new Date();
old_date.setDate(today_date.getDate()-5);
var old_date_int = old_date.getTime();
var today_date_int = today_date.getTime();
var random_date_int=old_date_int
+(Math.random()*(today_date_int-old_date_int));
random_date_int.toUTCString();)}
but when I use this it doesn't evaulate. Does anyone have anything
'out-of-the-box' that works ?
TIA
hiro
On Thu, 2008-10-23 at 16:05 +0400, Andrey Beznogov wrote:
> Hi,
>
> the best way would be probably randomizing the Time part (the number
> of milliseconds since 1970/01/01) of the Date(). For example in
> javascript
>
> var mydate=new Date();
> mydate.setTime(mydate.getTime()*Math.random());
> mydate.toUTCString();
>
> will output a date string in UTC format (like "Sat, 19 Jan 1985
> 06:22:28 GMT") for a date between now and 1970/01/01.
>
> Regards,
> Andrey
>
> On Thu, Oct 23, 2008 at 3:47 PM, Hiro Protagonist
> <hi...@googlemail.com> wrote:
> > Hi team,
> >
> > I need to generate random dates in JMeter. I know that by merely using
> > the random function and then 'building' the date string I can get 90% of
> > the way there - however, in order no to risk faulty dates, I always lose
> > out on dates after the 28th.
> > The obvious problem here is the interdependency between the months and
> > their respective number of days.
> > Does anyone know of a quick and easy solution ?
> >
> > Thanks !
> >
> > hiro
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: jmeter-user-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail: jmeter-user-help@jakarta.apache.org
> >
> >
>
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: jmeter-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: jmeter-user-help@jakarta.apache.org
Re: How to generate random dates
Posted by Hiro Protagonist <hi...@googlemail.com>.
Hi Andrey,
Thanks for your response. I tried it, and it works very well !
(Now restricting it to a random date within a date range...)
hiro
On Thu, 2008-10-23 at 16:05 +0400, Andrey Beznogov wrote:
> Hi,
>
> the best way would be probably randomizing the Time part (the number
> of milliseconds since 1970/01/01) of the Date(). For example in
> javascript
>
> var mydate=new Date();
> mydate.setTime(mydate.getTime()*Math.random());
> mydate.toUTCString();
>
> will output a date string in UTC format (like "Sat, 19 Jan 1985
> 06:22:28 GMT") for a date between now and 1970/01/01.
>
> Regards,
> Andrey
>
> On Thu, Oct 23, 2008 at 3:47 PM, Hiro Protagonist
> <hi...@googlemail.com> wrote:
> > Hi team,
> >
> > I need to generate random dates in JMeter. I know that by merely using
> > the random function and then 'building' the date string I can get 90% of
> > the way there - however, in order no to risk faulty dates, I always lose
> > out on dates after the 28th.
> > The obvious problem here is the interdependency between the months and
> > their respective number of days.
> > Does anyone know of a quick and easy solution ?
> >
> > Thanks !
> >
> > hiro
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: jmeter-user-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail: jmeter-user-help@jakarta.apache.org
> >
> >
>
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: jmeter-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: jmeter-user-help@jakarta.apache.org
Re: How to generate random dates
Posted by Andrey Beznogov <an...@googlemail.com>.
Hi,
the best way would be probably randomizing the Time part (the number
of milliseconds since 1970/01/01) of the Date(). For example in
javascript
var mydate=new Date();
mydate.setTime(mydate.getTime()*Math.random());
mydate.toUTCString();
will output a date string in UTC format (like "Sat, 19 Jan 1985
06:22:28 GMT") for a date between now and 1970/01/01.
Regards,
Andrey
On Thu, Oct 23, 2008 at 3:47 PM, Hiro Protagonist
<hi...@googlemail.com> wrote:
> Hi team,
>
> I need to generate random dates in JMeter. I know that by merely using
> the random function and then 'building' the date string I can get 90% of
> the way there - however, in order no to risk faulty dates, I always lose
> out on dates after the 28th.
> The obvious problem here is the interdependency between the months and
> their respective number of days.
> Does anyone know of a quick and easy solution ?
>
> Thanks !
>
> hiro
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: jmeter-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: jmeter-user-help@jakarta.apache.org
>
>
--
diem perdidi
---------------------------------------------------------------------
To unsubscribe, e-mail: jmeter-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: jmeter-user-help@jakarta.apache.org