如何在Loadrunner中获取当前时间(以毫秒为单位)?

时间:2013-02-27 21:25:50

标签: time epoch loadrunner

我在Loadrunner中寻找相当于Java的System.currentTimeMillis()。我只能找到lr_save_datetime(“%x%X”,0,“now”);以日期和时间格式输出,但不是自纪元以来的时间。

在C中,time.h可以提供此功能,但LR脚本中的#include“time.h”导致编译错误。 time.h可能依赖于其他.h和.inl文件。

我有#error错误:只支持Win32目标!即使我已经从Visual Studio C ++编译器文件夹中复制了所有丢失的文件。 (C:\ Program Files \ Microsoft Visual Studio 9.0 \ VC \ include \ time.h)

5 个答案:

答案 0 :(得分:2)

另一种方法是使用 lr_save_timestamp 以秒为单位获取时间戳 - 它比web_save_timestamp_param更强大。

我的功能文档:

int lr_save_timestamp( const char* tmstampParam, [DIGITS,] LAST );

<强> tmstampParam

存储时间戳的参数名称。

<强> DIGITS

可选。时间戳(整数)的位数。默认值为13(纪元时间包括3位毫秒精度)。

如果DIGITS的值小于1或大于16,则使用默认值

在此示例中,保存时间戳并指定10位数的长度。以秒为单位返回纪元时间

lr_save_timestamp(“param”, “DIGITS=10″, LAST );

答案 1 :(得分:1)

您可以使用'time()'函数输出自1970年1月1日以来的秒数,如UNIX风格格式:

long t;  // create the variable

// output the seconds to the log
lr_output_message("seconds since 01/01/1970 : %d", 
                   time(t) ); // populate the variable
                              // with the current seconds count 
------------------------------------------------------------------
Output: Action.c(11): time : 1362080852

如果您要查找当前毫秒,可以在“参数”列表中创建新的“日期/时间”参数并使用格式代码“%S.000”在LoadRunner中获取该毫秒数。一旦到位,请这样称呼:

lr_eval_string("{datetime_param_name}");

例如,如果您在日期/时间参数格式字段中输入此内容:
%H%M%S.000

...获得当前时间,分钟,秒和毫秒...... 133808.132

由于某种原因,格式代码'%S.000'在日期/时间参数列表中工作,但不适用于lr_save_datetime()函数。如果你在函数上使用相同的代码,你会得到这个:
133808.000

答案 2 :(得分:0)

web_save_timestamp_param();从纪元开始,返回13位毫秒的精确时间。

LoadRunner不使用Visual C / C ++作为C运行时引擎。它使用LCC,轻量级的跨平台C编译器。因此,如果您需要扩展其他库,我建议使用LCC附带的库。有关LCC的更多信息,请参阅 The University of Virgina Department of Computer Science website on LCC for Windows

答案 3 :(得分:0)

在参数化下选择日期和时间参数类型,我们有很多格式。检查一次....我希望它能解决你的问题而无需任何编码工作

答案 4 :(得分:0)

您可以使用ftime实用程序功能。它接受指向 timeb 结构的指针,并用当前时间值填充它。

timeb 结构定义如下:

struct timeb {
   time_t time; //seconds since epoch
   unsigned short millis; //milliseconds since time field
   short timezone;
   short daytimesaveflag;
} 

根据LR函数引用使用ftime函数你需要定义timeb结构(不包括time.h

  struct timeb {
   long time; //seconds since epoch
   unsigned short millis; //milliseconds since time field
   short timezone;
   short daytimesaveflag;
} timestruct;
long millisSinceEpoch=0 
ftime(&timestruct);
millisSinceEpoch=timestruct.time_t*1000+timestruct.millis;