This repository was archived by the owner on Feb 22, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 248
feat(WatchGroup): Allow measuring cost of running reaction functions and #1148
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
library angular.change_detection.execution_stats; | ||
|
||
import 'package:angular/core/annotation_src.dart'; | ||
|
||
@Injectable() | ||
class ExecutionStats { | ||
final ExecutionStatsConfig config; | ||
final ExecutionStatsEmitter emitter; | ||
List<ExecutionEntry> _dirtyCheckStats; | ||
List<ExecutionEntry> _dirtyWatchStats; | ||
List<ExecutionEntry> _evalStats; | ||
int _evalsCount = 0; | ||
int _dirtyWatchCount = 0; | ||
int _dirtyCheckCount = 0; | ||
|
||
int get _capacity => config.maxEntries; | ||
|
||
ExecutionStats(this.emitter, this.config) { | ||
reset(); | ||
} | ||
|
||
void addDirtyCheckEntry(ExecutionEntry entry) { | ||
if( ++_dirtyCheckCount >= _capacity) _shrinkDirtyCheck(); | ||
_dirtyCheckStats[_dirtyCheckCount] = entry; | ||
} | ||
|
||
void addDirtyWatchEntry(ExecutionEntry entry) { | ||
if( ++_dirtyWatchCount >= _capacity) _shrinkDirtyWatch(); | ||
_dirtyWatchStats[_dirtyWatchCount] = entry; | ||
} | ||
|
||
void addEvalEntry(ExecutionEntry entry) { | ||
if( ++_evalsCount >= _capacity) _shrinkEval(); | ||
_evalStats[_evalsCount] = entry; | ||
} | ||
|
||
void showEvalStats() { | ||
emitter.showEvalStats(this); | ||
} | ||
|
||
void showReactionFnStats() { | ||
emitter.showReactionFnStats(this); | ||
} | ||
|
||
void showDirtyCheckStats() { | ||
emitter.showDirtyCheckStats(this); | ||
} | ||
|
||
Iterable<ExecutionEntry> get dirtyCheckStats { | ||
_shrinkDirtyWatch(); | ||
return _dirtyCheckStats.getRange(0, _capacity).where((e) => e.time > 0); | ||
} | ||
|
||
Iterable<ExecutionEntry> get evalStats { | ||
_shrinkDirtyWatch(); | ||
return _evalStats.getRange(0, _capacity).where((e) => e.time > 0); | ||
} | ||
|
||
Iterable<ExecutionEntry> get reactionFnStats { | ||
_shrinkDirtyWatch(); | ||
return _dirtyWatchStats.getRange(0, _capacity).where((e) => e.time > 0); | ||
} | ||
|
||
void enable() { | ||
config.enabled = true; | ||
} | ||
|
||
void disable() { | ||
config.enabled = false; | ||
} | ||
|
||
void reset() { | ||
_dirtyCheckStats = new List.filled(3 * _capacity, new ExecutionEntry(0, null)); | ||
_dirtyWatchStats = new List.filled(3 * _capacity, new ExecutionEntry(0, null)); | ||
_evalStats = new List.filled(3 * _capacity, new ExecutionEntry(0, null)); | ||
_evalsCount = 0; | ||
_dirtyWatchCount = 0; | ||
_dirtyCheckCount = 0; | ||
} | ||
|
||
void _shrinkDirtyCheck() { | ||
_dirtyCheckStats.sort((ExecutionEntry x, ExecutionEntry y) => y.time.compareTo(x.time)); | ||
for(int i = _capacity; i < 3 * _capacity; i++) _dirtyCheckStats[i] = new ExecutionEntry(0, null); | ||
_dirtyCheckCount = _capacity; | ||
} | ||
|
||
void _shrinkDirtyWatch() { | ||
_dirtyWatchStats.sort((ExecutionEntry x, ExecutionEntry y) => x.time.compareTo(y.time) * -1); | ||
for(int i = _capacity; i < 3 * _capacity; i++) _dirtyWatchStats[i] = new ExecutionEntry(0, null); | ||
_dirtyWatchCount = _capacity; | ||
} | ||
|
||
void _shrinkEval() { | ||
_evalStats.sort((ExecutionEntry x, ExecutionEntry y) => x.time.compareTo(y.time) * -1); | ||
for(int i = _capacity; i < 3 * _capacity; i++) _evalStats[i] = new ExecutionEntry(0, null); | ||
_evalsCount = _capacity; | ||
} | ||
} | ||
|
||
@Injectable() | ||
class ExecutionStatsEmitter { | ||
void showDirtyCheckStats(ExecutionStats fnStats) { | ||
_printLine('Time (us)', 'Field'); | ||
fnStats.dirtyCheckStats.forEach((ExecutionEntry entry) => | ||
_printLine('${entry.time}', '${entry.value}')); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. missing 4ws |
||
} | ||
|
||
void showEvalStats(ExecutionStats fnStats) { | ||
_printLine('Time (us)', 'Name'); | ||
fnStats.evalStats.forEach((ExecutionEntry entry) => | ||
_printLine('${entry.time}', '${entry.value}')); | ||
} | ||
|
||
void showReactionFnStats(ExecutionStats fnStats) { | ||
_printLine('Time (us)', 'Expression'); | ||
fnStats.reactionFnStats.forEach((ExecutionEntry entry) => | ||
_printLine('${entry.time}', '${entry.value}')); | ||
} | ||
|
||
_printLine(String first, String second) { | ||
var timesColLength = 10; | ||
var expressionsColPrefix = 5; | ||
var timesCol = ' ' * (timesColLength - first.length); | ||
var expressionsCol = ' ' * expressionsColPrefix; | ||
print('${timesCol + first}${expressionsCol + second}'); | ||
} | ||
|
||
} | ||
|
||
class ExecutionEntry { | ||
final num time; | ||
final dynamic value; //Record or Watch | ||
|
||
ExecutionEntry(this.time, this.value); | ||
} | ||
|
||
class ExecutionStatsConfig { | ||
bool enabled; | ||
int threshold; | ||
int maxEntries; | ||
|
||
ExecutionStatsConfig({this.enabled: false, this.threshold, this.maxEntries: 15}); | ||
} |
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
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think a better way to do this is to duplicate the whole while loop. One while loop is instrumented and the other is not.
The stats objects should be injected in the constructor and should have on off switch which would then control which while loop to chose. That way one could turn on/off the stats during the lifetime.