Skip to content

Instantly share code, notes, and snippets.

@GavinJoyce
Last active October 23, 2015 12:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GavinJoyce/c3b5707747121ff067e7 to your computer and use it in GitHub Desktop.
Save GavinJoyce/c3b5707747121ff067e7 to your computer and use it in GitHub Desktop.
Ember component metrics
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