Unverified Commit c81449e5 authored by Yegor's avatar Yegor Committed by GitHub

Allow Recorder override shouldContinue (#57039)

parent 573625b7
...@@ -111,6 +111,17 @@ abstract class Recorder { ...@@ -111,6 +111,17 @@ abstract class Recorder {
/// prefix. /// prefix.
final String name; final String name;
/// Returns the recorded profile.
///
/// This value is only available while the benchmark is running.
Profile get profile;
/// Whether the benchmark should continue running.
///
/// Returns `false` if the benchmark collected enough data and it's time to
/// stop.
bool shouldContinue() => profile.shouldContinue();
/// Called once before all runs of this benchmark recorder. /// Called once before all runs of this benchmark recorder.
/// ///
/// This is useful for doing one-time setup work that's needed for the /// This is useful for doing one-time setup work that's needed for the
...@@ -158,15 +169,19 @@ abstract class RawRecorder extends Recorder { ...@@ -158,15 +169,19 @@ abstract class RawRecorder extends Recorder {
/// This is the part that records measurements of the benchmark. /// This is the part that records measurements of the benchmark.
void body(Profile profile); void body(Profile profile);
@override
Profile get profile => _profile;
Profile _profile;
@override @override
@nonVirtual @nonVirtual
Future<Profile> run() async { Future<Profile> run() async {
final Profile profile = Profile(name: name); _profile = Profile(name: name);
do { do {
await Future<void>.delayed(Duration.zero); await Future<void>.delayed(Duration.zero);
body(profile); body(_profile);
} while (profile.shouldContinue()); } while (shouldContinue());
return profile; return _profile;
} }
} }
...@@ -198,6 +213,10 @@ abstract class RawRecorder extends Recorder { ...@@ -198,6 +213,10 @@ abstract class RawRecorder extends Recorder {
abstract class SceneBuilderRecorder extends Recorder { abstract class SceneBuilderRecorder extends Recorder {
SceneBuilderRecorder({@required String name}) : super._(name, true); SceneBuilderRecorder({@required String name}) : super._(name, true);
@override
Profile get profile => _profile;
Profile _profile;
/// Called from [Window.onBeginFrame]. /// Called from [Window.onBeginFrame].
@mustCallSuper @mustCallSuper
void onBeginFrame() {} void onBeginFrame() {}
...@@ -212,7 +231,7 @@ abstract class SceneBuilderRecorder extends Recorder { ...@@ -212,7 +231,7 @@ abstract class SceneBuilderRecorder extends Recorder {
@override @override
Future<Profile> run() { Future<Profile> run() {
final Completer<Profile> profileCompleter = Completer<Profile>(); final Completer<Profile> profileCompleter = Completer<Profile>();
final Profile profile = Profile(name: name); _profile = Profile(name: name);
window.onBeginFrame = (_) { window.onBeginFrame = (_) {
try { try {
...@@ -225,22 +244,22 @@ abstract class SceneBuilderRecorder extends Recorder { ...@@ -225,22 +244,22 @@ abstract class SceneBuilderRecorder extends Recorder {
}; };
window.onDrawFrame = () { window.onDrawFrame = () {
try { try {
profile.record('drawFrameDuration', () { _profile.record('drawFrameDuration', () {
final SceneBuilder sceneBuilder = SceneBuilder(); final SceneBuilder sceneBuilder = SceneBuilder();
onDrawFrame(sceneBuilder); onDrawFrame(sceneBuilder);
profile.record('sceneBuildDuration', () { _profile.record('sceneBuildDuration', () {
final Scene scene = sceneBuilder.build(); final Scene scene = sceneBuilder.build();
profile.record('windowRenderDuration', () { _profile.record('windowRenderDuration', () {
window.render(scene); window.render(scene);
}, reported: false); }, reported: false);
}, reported: false); }, reported: false);
}, reported: true); }, reported: true);
endMeasureFrame(); endMeasureFrame();
if (profile.shouldContinue()) { if (shouldContinue()) {
window.scheduleFrame(); window.scheduleFrame();
} else { } else {
profileCompleter.complete(profile); profileCompleter.complete(_profile);
} }
} catch (error, stackTrace) { } catch (error, stackTrace) {
profileCompleter.completeError(error, stackTrace); profileCompleter.completeError(error, stackTrace);
...@@ -326,6 +345,7 @@ abstract class WidgetRecorder extends Recorder implements FrameRecorder { ...@@ -326,6 +345,7 @@ abstract class WidgetRecorder extends Recorder implements FrameRecorder {
@override @override
VoidCallback didStop; VoidCallback didStop;
@override
Profile profile; Profile profile;
Completer<void> _runCompleter; Completer<void> _runCompleter;
...@@ -344,7 +364,7 @@ abstract class WidgetRecorder extends Recorder implements FrameRecorder { ...@@ -344,7 +364,7 @@ abstract class WidgetRecorder extends Recorder implements FrameRecorder {
endMeasureFrame(); endMeasureFrame();
profile.addDataPoint('drawFrameDuration', _drawFrameStopwatch.elapsed, reported: true); profile.addDataPoint('drawFrameDuration', _drawFrameStopwatch.elapsed, reported: true);
if (profile.shouldContinue()) { if (shouldContinue()) {
window.scheduleFrame(); window.scheduleFrame();
} else { } else {
didStop(); didStop();
...@@ -414,6 +434,7 @@ abstract class WidgetBuildRecorder extends Recorder implements FrameRecorder { ...@@ -414,6 +434,7 @@ abstract class WidgetBuildRecorder extends Recorder implements FrameRecorder {
@override @override
VoidCallback didStop; VoidCallback didStop;
@override
Profile profile; Profile profile;
Completer<void> _runCompleter; Completer<void> _runCompleter;
...@@ -453,7 +474,7 @@ abstract class WidgetBuildRecorder extends Recorder implements FrameRecorder { ...@@ -453,7 +474,7 @@ abstract class WidgetBuildRecorder extends Recorder implements FrameRecorder {
profile.addDataPoint('drawFrameDuration', _drawFrameStopwatch.elapsed, reported: true); profile.addDataPoint('drawFrameDuration', _drawFrameStopwatch.elapsed, reported: true);
} }
if (profile.shouldContinue()) { if (shouldContinue()) {
showWidget = !showWidget; showWidget = !showWidget;
_hostState._setStateTrampoline(); _hostState._setStateTrampoline();
} else { } else {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment