【问题标题】:Tracking function execution time with jasmine用茉莉花跟踪函数执行时间
【发布时间】:2016-07-02 01:21:20
【问题描述】:

是否有可能创建一个可重复使用的 jasmine 匹配器,它会断言一个函数运行时间少于 N 秒?

样本期望风格:

expect(someFunction, [arg1, arg2]).toBeCompletedForLessThan(3);
expect(obj.someMethod, [arg1, arg2]).toBeCompletedForLessThan(5);

我们希望将其与量角器和自定义性能测试结合起来,我们希望在这些测试中断言某些 UI 执行步骤不会超出时间限制。

【问题讨论】:

    标签: javascript performance testing jasmine protractor


    【解决方案1】:

    我会使用自定义计时器测量经过的时间,然后使用 .toBeLessThan 断言结果:

    var timer = Timer();
    
    $("#mytext").getText().then(function(text){
        console.log(text);
    });
    
    expect(timer.elapsed).toBeLessThan(1000);   // to be under 1 second
    

    计时器:

    var Timer = function() {
      var starttime = 0;
    
      browser.controlFlow().execute(function() {
        starttime = Date.now()
      });
    
      return {
        get elapsed() {
          return browser.controlFlow().execute(() => Date.now() - starttime);
        }
      };
    };
    

    【讨论】:

    • 看起来很有前途且合乎逻辑。我会在实践中尝试。再次感谢。
    猜你喜欢
    • 2014-12-12
    • 1970-01-01
    • 2013-06-29
    • 1970-01-01
    • 1970-01-01
    • 2014-01-25
    • 2012-08-15
    • 2015-11-26
    相关资源
    最近更新 更多