Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tracking function execution time with jasmine

Is it possible to create a reusable jasmine matcher that would assert a function to run for less than N seconds?

Sample expectation style:

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

We'd like to tie this up with Protractor and custom performance tests where we want to assert that certain UI execution steps don't go beyond the time limit.

like image 661
alecxe Avatar asked Jul 02 '16 01:07

alecxe


1 Answers

I would measure the elapsed time with a custom timer and then assert the result with .toBeLessThan:

var timer = Timer();

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

expect(timer.elapsed).toBeLessThan(1000);   // to be under 1 second

The timer:

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

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

  return {
    get elapsed() {
      return browser.controlFlow().execute(() => Date.now() - starttime);
    }
  };
};
like image 62
Florent B. Avatar answered Nov 03 '22 18:11

Florent B.