Last active
August 29, 2015 13:56
-
-
Save ratbeard/9009614 to your computer and use it in GitHub Desktop.
measure angular digest times and periodically log out stats
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
application.config(($provide) -> | |
# Add timing around $digest() | |
window.digestStats = new DigestStats | |
$provide.decorator("$rootScope", ($delegate) -> | |
$digest = $delegate.$digest.bind($delegate) | |
$delegate.$digest = -> | |
startedAt = performance.now() | |
$digest() | |
digestStats.addDuration(performance.now() - startedAt) | |
return $delegate | |
) | |
) | |
class DigestStats | |
timeBetweenLogs: 10 * 1000 | |
numberOfRecentDurationsToShow: 5 | |
shouldLog: true | |
constructor: -> | |
@stats = new Stats | |
@recentDurations = [] | |
setInterval(@logStats, @timeBetweenLogs) | |
toggle: -> | |
@shouldLog = !@shouldLog | |
logStats: => | |
console.group("digestStats") | |
console.log "number of digests:", @stats.n | |
console.log "mean:", @stats.mean | |
console.log "standard deviation:", @stats.sd | |
console.log "last #{@recentDurations.length}:", @recentDurations | |
console.groupEnd("digestStats") | |
addDuration: (duration) -> | |
@stats.sample(duration) | |
@recentDurations.unshift(duration) | |
@recentDurations.pop() if @recentDurations.length > @numberOfRecentDurationsToShow | |
# Rolling statistics calculations. | |
# | |
# From Zed Shaw's Python version as mentioned in https://peepcode.com/products/play-by-play-zed-shaw | |
# | |
# Implemented by Geoffrey Grosenbach | |
# @topfunky | |
# http://peepcode.com | |
class Stats | |
# Optionally start a new object with existing data. | |
# | |
# * `sum` - Total of all samples recorded | |
# * `sumsq` - Cumulative sum of squares of all samples | |
# * `n` - Number of samples | |
# * `min` - Smallest sample | |
# * `max` - Largest sample | |
# | |
# After adding samples, the following properties will also be available: | |
# | |
# * `sd` - Standard deviation | |
# * `mean` - Average | |
constructor: (@sum=0, @sumsq=0, @n=0, @min=0, @max=0) -> | |
# Add a single data sample to the calculations. | |
sample: (s) -> | |
@sum += s | |
@sumsq += s*s | |
if @n == 0 | |
@min = s | |
@max = s | |
else | |
@min = s if @min > s | |
@max = s if @max < s | |
@n += 1 | |
# Update variables | |
@sd = @getSD() | |
@mean = @getMean() | |
@ | |
# Calculate mean (average). | |
getMean: -> | |
result = @sum / @n | |
if isNaN result then 0 else result | |
# Calculate standard deviation. | |
getSD: -> | |
result = Math.sqrt( (@sumsq - (@sum * @sum / @n)) / (@n - 1) ) | |
if isNaN result then 0 else result | |
# Build totals together with another Stats object. | |
combine: (stats) -> | |
@sum += stats.sum | |
@sumsq += stats.sumsq | |
@n += stats.n | |
@min = stats.min if @min > stats.min | |
@max = stats.max if @max < stats.max | |
@sd = @getSD() | |
@mean = @getMean() | |
@ | |
# Return attributes for JSON serialization. | |
toJSON: -> { @sum, @sumsq, @n, @min, @max, @sd, @mean } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment