Last active
October 23, 2015 12:55
-
-
Save GavinJoyce/c3b5707747121ff067e7 to your computer and use it in GitHub Desktop.
Ember component metrics
This file contains hidden or 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
import Em from 'ember'; | |
import Metrics from 'myapp/models/metrics'; | |
var FrontendStatsService = Em.Service.extend({ | |
stats: {}, | |
timings: {}, | |
start: function(key) { | |
if(window.performance) { | |
if(this.timings[key] === undefined) { //skip the first so we're not capturing data when loading the app | |
this.timings[key] = null; | |
} else { | |
this.timings[key] = window.performance.now(); | |
} | |
} | |
}, | |
stop: function(key, bufferSize, numberOfItemsRendered) { | |
if(window.performance) { | |
var start = this.timings[key]; | |
if(start) { | |
var duration = window.performance.now() - start; | |
this.capture(key, duration, bufferSize, numberOfItemsRendered); | |
this.timings[key] = null; | |
} | |
} | |
}, | |
timeUntilAfterRender: function(key, options) { | |
options = options || {}; | |
this.start(key); | |
Em.run.schedule('afterRender', this, function() { | |
this.stop(key, options.bufferSize, options.numberOfItemsRendered); | |
}); | |
}, | |
capture: function(key, value, bufferSize, numberOfItemsRendered) { | |
bufferSize = bufferSize || 10; | |
numberOfItemsRendered = numberOfItemsRendered || 1; | |
this.stats[key] = this.stats[key] || []; | |
this.stats[key].push(value / numberOfItemsRendered); | |
if(this.stats[key].length >= bufferSize) { | |
Metrics.capture(this.stats); //post stats payload to server | |
this.stats = {}; | |
} | |
} | |
}); | |
export default FrontendStatsService; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment