diff options
| author | Misko Hevery | 2011-03-19 09:57:18 +0530 | 
|---|---|---|
| committer | Misko Hevery | 2011-06-08 13:49:10 -0700 | 
| commit | fab4ada3c849becede839530d812748064654bd6 (patch) | |
| tree | 395563f7c834da1fea993b2693a071e4e4a88947 /perf/testUtils.js | |
| parent | d6eba8f39ff78061a4f9bfaefa1bb1f592f7e0ef (diff) | |
| download | angular.js-fab4ada3c849becede839530d812748064654bd6.tar.bz2 | |
Created a performance test harness and reporter
Diffstat (limited to 'perf/testUtils.js')
| -rw-r--r-- | perf/testUtils.js | 73 | 
1 files changed, 60 insertions, 13 deletions
diff --git a/perf/testUtils.js b/perf/testUtils.js index ca016c31..2fc4f8a2 100644 --- a/perf/testUtils.js +++ b/perf/testUtils.js @@ -1,20 +1,67 @@  if (window.jstestdriver) {    jstd = jstestdriver; -  dump = angular.bind(jstd.console, jstd.console.log); +  dump = bind(jstd.console, jstd.console.log);  } -function time(fn, times) { -  times = times || 1; +function time(fn) { +  var count = 1, +      targetTime = 500, +      start = new Date().getTime(), +      stop = start + targetTime, +      elapsed, +      end, +      iterations, +      pad = angularFilter.number; -  var i, -      start, -      duration = 0; - -  for (i=0; i<times; i++) { -    start = Date.now(); -    fn(); -    duration += Date.now() - start; +  // do one iteration to guess how long it will take +  fn(); +  while((end=new Date().getTime()) < stop ){ +    // how much time has elapsed since we started the test +    elapsed = (end-start) || 1; +    // guess how many more iterations we need before we reach +    // the time limit. We do this so that we spend most of our +    // time in tight loop +    iterations = Math.ceil( +        // how much more time we need +        (targetTime - elapsed) +        / +        2 // to prevent overrun guess low +        / +        // this is how much the cost is so far per iteration +        (elapsed / count) +      ); +    count += iterations; +    while(iterations--) { +      fn(); +    }    } +  elapsed = end - start; +  return { +    count: count, +    total: elapsed, +    time: elapsed / count, +    name: fn.name, +    msg: '' + pad(elapsed / count, 3) +      + ' ms [ ' + pad(1 / elapsed * count * 1000, 0) + ' ops/sec ] ' +      + '(' + elapsed + ' ms/' + count + ')' +  }; + +} -  return duration; -}
\ No newline at end of file +function perf() { +  var log = [], +      summary = [], +      i, +      baseline, +      pad = angularFilter.number; + +  for (i = 0; i < arguments.length; i++) { +    var fn = arguments[i]; +    var info = time(fn); +    if (baseline === undefined) baseline = info.time; +    summary.push(info.name + ': ' + pad(baseline / info.time, 2) + ' X'); +    log.push('\n        ' + info.name + ': ' + info.msg); +  } +  log.unshift(summary.join(' - ')); +  dump(log.join(' ')); +}  | 
