繁体   English   中英

在机器人框架中以毫秒为单位获取时间的关键字是什么?

[英]What is the keyword to get time in milliseconds in robot framework?

目前,我正在使用关键字Get time epoch ,它以秒为单位返回时间。 但是我需要以毫秒为单位的时间,这样我才能获得特定事件的时间跨度。

还是有其他方法来获取特定事件或测试环境的时间跨度?

检查新的测试库DateTime ,它包含关键字Get Current Date ,它也返回毫秒。 它还具有关键字“ Subtract Dates以计算两个时间戳之间的差异。

机械手更强大的功能之一是,您可以使用Evaluate关键字直接从测试脚本中调用python代码。 例如,您可以调用time.time()函数,并做一些数学运算:

*** Test cases
| Example getting the time in milliseconds
| | ${ms}= | Evaluate | int(round(time.time() * 1000)) | time
| | log | time in ms: ${ms}

请注意,即使time.time返回浮点值,但并非所有系统都会返回比一秒更精确的值。

在报告中,对于每个套件,测试和关键字,您都具有关于开始,结束和长度的信息,以及毫秒详细信息。 就像是:

Start / End / Elapsed:  20140602 10:57:15.948 / 20140602 10:57:16.985 / 00:00:01.037

我看不到使用Builtin做到这一点的方法,请看:

def get_time(format='timestamp', time_=None):
    """Return the given or current time in requested format.

    If time is not given, current time is used. How time is returned is
    is deternined based on the given 'format' string as follows. Note that all
    checks are case insensitive.

    - If 'format' contains word 'epoch' the time is returned in seconds after
      the unix epoch.
    - If 'format' contains any of the words 'year', 'month', 'day', 'hour',
      'min' or 'sec' only selected parts are returned. The order of the returned
      parts is always the one in previous sentence and order of words in
      'format' is not significant. Parts are returned as zero padded strings
      (e.g. May -> '05').
    - Otherwise (and by default) the time is returned as a timestamp string in
      format '2006-02-24 15:08:31'
    """
    time_ = int(time_ or time.time())
    format = format.lower()
    # 1) Return time in seconds since epoc
    if 'epoch' in format:
        return time_
    timetuple = time.localtime(time_)
    parts = []
    for i, match in enumerate('year month day hour min sec'.split()):
        if match in format:
            parts.append('%.2d' % timetuple[i])
    # 2) Return time as timestamp
    if not parts:
        return format_time(timetuple, daysep='-')
    # Return requested parts of the time
    elif len(parts) == 1:
        return parts[0]
    else:
        return parts

您必须编写自己的模块,您需要类似:

import time

def get_time_in_millies():
    time_millies = lambda: int(round(time.time() * 1000))

    return time_millies

然后将该库导入套件的Ride中,您可以使用方法名称(例如关键字),在我的情况下,它将是Get Time In Millies 更多信息在这里

使用janne建议的DateTime library

*** Settings ***
Library    DateTime

*** Test Cases ***
Performance Test
    ${timeAvgMs} =    Test wall clock time    100    MyKeywordToPerformanceTest    and    optional    arguments
    Should be true    ${timeAvgMs} < 50

*** Keywords ***
MyKeywordToPerformanceTest
    # Do something here

Test wall clock time
    [Arguments]    ${iterations}    @{commandAndArgs}
    ${timeBefore} =    Get Current Date
    :FOR   ${it}    IN RANGE    ${iterations}
    \    @{commandAndArgs}
    ${timeAfter} =    Get Current Date
    ${timeTotalMs} =    Subtract Date From Date    ${timeAfter}    ${timeBefore}    result_format=number
    ${timeAvgMs} =    Evaluate    int(${timeTotalMs} / ${iterations} * 1000)
    Return from keyword    ${timeAvgMs}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM