Skip to content

Instantly share code, notes, and snippets.

@robatron
Last active August 29, 2015 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robatron/437b8c0ed61687b124ab to your computer and use it in GitHub Desktop.
Save robatron/437b8c0ed61687b124ab to your computer and use it in GitHub Desktop.
/**
* Get the total time for the specified timers, counting parallel time only
* once if no timers match the filter, return 0.
* @param {String} filter - 'contains'-filter by name
*/
Timer.getTotalTime = function ( filter ) {
// Sort timers by start time, ascending
var sortedTimers = _.sortBy( Timer.getTimers( filter ), 'startTime' );
// Total execution time
var totalTime = 0;
// Parallel group start/stop time. Start with zeros because if there are no
// matching timer, the loop below never runs, and final time is zero
var groupStartTime = 0;
var groupStopTime = 0;
// For each matching timer, group parallel timers
_.forEach( sortedTimers, function ( curTimer ) {
// If the current timer's start time is before or at the time of the
// group's stop time, it's part of the current parallel group
if ( curTimer.startTime <= groupStopTime ) {
// If the current timer's stop time is also after the group's stop
// time, update the group's stop time.
if ( curTimer.stopTime > groupStopTime ) {
groupStopTime = curTimer.stopTime;
}
// Otherwise, the current timer is the beginning of the new group.
// Add the elapsed time of the entire previous group to the total time,
// and set the group's start and stop time to the current timer's
} else {
totalTime += groupStopTime - groupStartTime;
groupStartTime = curTimer.startTime;
groupStopTime = curTimer.stopTime;
}
} );
// Add the final group's time to the total time
totalTime += groupStopTime - groupStartTime;
return totalTime;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment