mirror of
https://github.com/flutter/flutter.git
synced 2026-02-20 02:29:02 +08:00
WIP Commits separated as follows: - Update lints in analysis_options files - Run `dart fix --apply` - Clean up leftover analysis issues - Run `dart format .` in the right places. Local analysis and testing passes. Checking CI now. Part of https://github.com/flutter/flutter/issues/178827 - Adoption of flutter_lints in examples/api coming in a separate change (cc @loic-sharma) ## Pre-launch Checklist - [ ] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [ ] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [ ] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [ ] I signed the [CLA]. - [ ] I listed at least one issue that this PR fixes in the description above. - [ ] I updated/added relevant documentation (doc comments with `///`). - [ ] I added new tests to check the change I am making, or this PR is [test-exempt]. - [ ] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [ ] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
142 lines
4.9 KiB
Dart
142 lines
4.9 KiB
Dart
// Copyright 2014 The Flutter Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
// found in the LICENSE file.
|
|
|
|
import 'package:flutter/foundation.dart';
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
|
|
// IMPORTANT: keep this in sync with the same constant defined
|
|
// in foundation/timeline.dart
|
|
const int kSliceSize = 500;
|
|
|
|
void main() {
|
|
setUp(() {
|
|
FlutterTimeline.debugReset();
|
|
FlutterTimeline.debugCollectionEnabled = false;
|
|
});
|
|
|
|
test('Does not collect when collection not enabled', () {
|
|
FlutterTimeline.startSync('TEST');
|
|
FlutterTimeline.finishSync();
|
|
expect(() => FlutterTimeline.debugCollect(), throwsStateError);
|
|
});
|
|
|
|
test('Collects when collection is enabled', () {
|
|
FlutterTimeline.debugCollectionEnabled = true;
|
|
FlutterTimeline.startSync('TEST');
|
|
FlutterTimeline.finishSync();
|
|
final AggregatedTimings data = FlutterTimeline.debugCollect();
|
|
expect(data.timedBlocks, hasLength(1));
|
|
expect(data.aggregatedBlocks, hasLength(1));
|
|
|
|
final AggregatedTimedBlock block = data.getAggregated('TEST');
|
|
expect(block.name, 'TEST');
|
|
expect(block.count, 1);
|
|
|
|
// After collection the timeline is reset back to empty.
|
|
final AggregatedTimings data2 = FlutterTimeline.debugCollect();
|
|
expect(data2.timedBlocks, isEmpty);
|
|
expect(data2.aggregatedBlocks, isEmpty);
|
|
});
|
|
|
|
test('Deletes old data when reset', () {
|
|
FlutterTimeline.debugCollectionEnabled = true;
|
|
FlutterTimeline.startSync('TEST');
|
|
FlutterTimeline.finishSync();
|
|
FlutterTimeline.debugReset();
|
|
|
|
final AggregatedTimings data = FlutterTimeline.debugCollect();
|
|
expect(data.timedBlocks, isEmpty);
|
|
expect(data.aggregatedBlocks, isEmpty);
|
|
});
|
|
|
|
test('Reports zero aggregation when requested missing block', () {
|
|
FlutterTimeline.debugCollectionEnabled = true;
|
|
|
|
final AggregatedTimings data = FlutterTimeline.debugCollect();
|
|
final AggregatedTimedBlock block = data.getAggregated('MISSING');
|
|
expect(block.name, 'MISSING');
|
|
expect(block.count, 0);
|
|
expect(block.duration, 0);
|
|
});
|
|
|
|
test('Measures the runtime of a function', () {
|
|
FlutterTimeline.debugCollectionEnabled = true;
|
|
|
|
// The off-by-one values for `start` and `end` are for web's sake where
|
|
// timer values are reported as float64 and toInt/toDouble conversions
|
|
// are noops, so there's no value truncation happening, which makes it
|
|
// a bit inconsistent with Stopwatch.
|
|
final int start = FlutterTimeline.now - 1;
|
|
FlutterTimeline.timeSync('TEST', () {
|
|
final watch = Stopwatch()..start(); // flutter_ignore: stopwatch (see analyze.dart)
|
|
// Ignore context: Used safely for benchmarking.
|
|
while (watch.elapsedMilliseconds < 5) {}
|
|
watch.stop();
|
|
});
|
|
final int end = FlutterTimeline.now + 1;
|
|
|
|
final AggregatedTimings data = FlutterTimeline.debugCollect();
|
|
expect(data.timedBlocks, hasLength(1));
|
|
expect(data.aggregatedBlocks, hasLength(1));
|
|
|
|
final TimedBlock block = data.timedBlocks.single;
|
|
expect(block.name, 'TEST');
|
|
expect(block.start, greaterThanOrEqualTo(start));
|
|
expect(block.end, lessThanOrEqualTo(end));
|
|
expect(block.duration, greaterThan(0));
|
|
|
|
final AggregatedTimedBlock aggregated = data.getAggregated('TEST');
|
|
expect(aggregated.name, 'TEST');
|
|
expect(aggregated.count, 1);
|
|
expect(aggregated.duration, block.duration);
|
|
});
|
|
|
|
test('FlutterTimeline.instanceSync does not collect anything', () {
|
|
FlutterTimeline.debugCollectionEnabled = true;
|
|
FlutterTimeline.instantSync('TEST');
|
|
|
|
final AggregatedTimings data = FlutterTimeline.debugCollect();
|
|
expect(data.timedBlocks, isEmpty);
|
|
expect(data.aggregatedBlocks, isEmpty);
|
|
});
|
|
|
|
test('FlutterTimeline.now returns a value', () {
|
|
FlutterTimeline.debugCollectionEnabled = true;
|
|
expect(FlutterTimeline.now, isNotNull);
|
|
});
|
|
|
|
test('Can collect more than one slice of data', () {
|
|
FlutterTimeline.debugCollectionEnabled = true;
|
|
|
|
for (var i = 0; i < 10 * kSliceSize; i++) {
|
|
FlutterTimeline.startSync('TEST');
|
|
FlutterTimeline.finishSync();
|
|
}
|
|
final AggregatedTimings data = FlutterTimeline.debugCollect();
|
|
expect(data.timedBlocks, hasLength(10 * kSliceSize));
|
|
expect(data.aggregatedBlocks, hasLength(1));
|
|
|
|
final AggregatedTimedBlock block = data.getAggregated('TEST');
|
|
expect(block.name, 'TEST');
|
|
expect(block.count, 10 * kSliceSize);
|
|
});
|
|
|
|
test('Collects blocks in a correct order', () {
|
|
FlutterTimeline.debugCollectionEnabled = true;
|
|
const int testCount = 7 * kSliceSize ~/ 2;
|
|
|
|
for (var i = 0; i < testCount; i++) {
|
|
FlutterTimeline.startSync('TEST$i');
|
|
FlutterTimeline.finishSync();
|
|
}
|
|
|
|
final AggregatedTimings data = FlutterTimeline.debugCollect();
|
|
expect(data.timedBlocks, hasLength(testCount));
|
|
expect(
|
|
data.timedBlocks.map<String>((TimedBlock block) => block.name).toList(),
|
|
List<String>.generate(testCount, (int i) => 'TEST$i'),
|
|
);
|
|
});
|
|
}
|