-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add a stats object with timings (#1257)
- Loading branch information
1 parent
0f5912f
commit 75c1fbc
Showing
6 changed files
with
108 additions
and
10 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
const now = (typeof process !== 'undefined' && process.hrtime) | ||
? () => { | ||
const t = process.hrtime(); | ||
return t[0] * 1e3 + t[1] / 1e6; | ||
} | ||
: () => window.performance.now(); | ||
|
||
type Timing = { | ||
label: string; | ||
start: number; | ||
end: number; | ||
children: Timing[]; | ||
} | ||
|
||
function collapseTimings(timings) { | ||
const result = {}; | ||
timings.forEach(timing => { | ||
result[timing.label] = Object.assign({ | ||
total: timing.end - timing.start | ||
}, timing.children && collapseTimings(timing.children)); | ||
}); | ||
return result; | ||
} | ||
|
||
export default class Stats { | ||
startTime: number; | ||
currentTiming: Timing; | ||
currentChildren: Timing[]; | ||
timings: Timing[]; | ||
stack: Timing[]; | ||
|
||
constructor() { | ||
this.startTime = now(); | ||
this.stack = []; | ||
this.currentChildren = this.timings = []; | ||
} | ||
|
||
start(label) { | ||
const timing = { | ||
label, | ||
start: now(), | ||
end: null, | ||
children: [] | ||
}; | ||
|
||
this.currentChildren.push(timing); | ||
this.stack.push(timing); | ||
|
||
this.currentTiming = timing; | ||
this.currentChildren = timing.children; | ||
} | ||
|
||
stop(label) { | ||
if (label !== this.currentTiming.label) { | ||
throw new Error(`Mismatched timing labels`); | ||
} | ||
|
||
this.currentTiming.end = now(); | ||
this.stack.pop(); | ||
this.currentTiming = this.stack[this.stack.length - 1]; | ||
this.currentChildren = this.currentTiming ? this.currentTiming.children : this.timings; | ||
} | ||
|
||
toJSON() { | ||
const timings = Object.assign({ | ||
total: now() - this.startTime | ||
}, collapseTimings(this.timings)); | ||
|
||
return { | ||
timings | ||
}; | ||
} | ||
} |
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
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
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
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
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