11if ( window . jstestdriver ) {
22 jstd = jstestdriver ;
3- dump = angular . bind ( jstd . console , jstd . console . log ) ;
3+ dump = bind ( jstd . console , jstd . console . log ) ;
44}
55
6- function time ( fn , times ) {
7- times = times || 1 ;
6+ function time ( fn ) {
7+ var count = 1 ,
8+ targetTime = 500 ,
9+ start = new Date ( ) . getTime ( ) ,
10+ stop = start + targetTime ,
11+ elapsed ,
12+ end ,
13+ iterations ,
14+ pad = angularFilter . number ;
815
9- var i ,
10- start ,
11- duration = 0 ;
12-
13- for ( i = 0 ; i < times ; i ++ ) {
14- start = Date . now ( ) ;
15- fn ( ) ;
16- duration += Date . now ( ) - start ;
16+ // do one iteration to guess how long it will take
17+ fn ( ) ;
18+ while ( ( end = new Date ( ) . getTime ( ) ) < stop ) {
19+ // how much time has elapsed since we started the test
20+ elapsed = ( end - start ) || 1 ;
21+ // guess how many more iterations we need before we reach
22+ // the time limit. We do this so that we spend most of our
23+ // time in tight loop
24+ iterations = Math . ceil (
25+ // how much more time we need
26+ ( targetTime - elapsed )
27+ /
28+ 2 // to prevent overrun guess low
29+ /
30+ // this is how much the cost is so far per iteration
31+ ( elapsed / count )
32+ ) ;
33+ count += iterations ;
34+ while ( iterations -- ) {
35+ fn ( ) ;
36+ }
1737 }
38+ elapsed = end - start ;
39+ return {
40+ count : count ,
41+ total : elapsed ,
42+ time : elapsed / count ,
43+ name : fn . name ,
44+ msg : '' + pad ( elapsed / count , 3 )
45+ + ' ms [ ' + pad ( 1 / elapsed * count * 1000 , 0 ) + ' ops/sec ] '
46+ + '(' + elapsed + ' ms/' + count + ')'
47+ } ;
48+
49+ }
1850
19- return duration ;
20- }
51+ function perf ( ) {
52+ var log = [ ] ,
53+ summary = [ ] ,
54+ i ,
55+ baseline ,
56+ pad = angularFilter . number ;
57+
58+ for ( i = 0 ; i < arguments . length ; i ++ ) {
59+ var fn = arguments [ i ] ;
60+ var info = time ( fn ) ;
61+ if ( baseline === undefined ) baseline = info . time ;
62+ summary . push ( info . name + ': ' + pad ( baseline / info . time , 2 ) + ' X' ) ;
63+ log . push ( '\n ' + info . name + ': ' + info . msg ) ;
64+ }
65+ log . unshift ( summary . join ( ' - ' ) ) ;
66+ dump ( log . join ( ' ' ) ) ;
67+ }
0 commit comments