使用jasmine跟踪函数执行时间

ale*_*cxe 4 javascript testing performance jasmine protractor

是否有可能创建一个可重用的茉莉花匹配器来断言一个运行时间少于N秒的函数?

样本期待风格:

expect(someFunction, [arg1, arg2]).toBeCompletedForLessThan(3);
expect(obj.someMethod, [arg1, arg2]).toBeCompletedForLessThan(5);
Run Code Online (Sandbox Code Playgroud)

我们想把它与Protractor和自定义性能测试结合起来,我们想断言某些UI执行步骤不会超出时间限制.

Flo*_* B. 5

我将使用自定义计时器测量已用时间,然后使用以下命令断言结果.toBeLessThan:

var timer = Timer();

$("#mytext").getText().then(function(text){
    console.log(text);
});

expect(timer.elapsed).toBeLessThan(1000);   // to be under 1 second
Run Code Online (Sandbox Code Playgroud)

计时器:

var Timer = function() {
  var starttime = 0;

  browser.controlFlow().execute(function() {
    starttime = Date.now()
  });

  return {
    get elapsed() {
      return browser.controlFlow().execute(() => Date.now() - starttime);
    }
  };
};
Run Code Online (Sandbox Code Playgroud)