Repository: flutter
Issue: flutter_driver does not record all timeline events
Labels: framework,t: flutter driver,f: integration_test,has reproducible steps,P2,team-framework,triaged-framework,found in release: 3.24,found in release: 3.27

### Steps to reproduce

While attempting to benchmark my app, I am noticing that `flutter_driver` seems to stop recording timeline events after a certain limit.

In the tests outlined in the code samples section, I have added a parameter called `kTestCount`. This determines how many times the code in the test is looped before ending the test.

The bug becomes evident when you change it from **1 loop to, let's say, 10 loops**. One would expect the number of timeline events to be roughly 10 times, however, the resultant json files are more or less the same size. When looking at the summary.json files, they both seem to have only recorded ~350-400 frames (`frame_count`).

Running similar tests with both `flutter_driver` and `integration_test` (which uses `flutter_driver` under the hood) yields more or less the exact same results.

All tests were run on a **Pixel 3a**, though I had the same issue when running the tests on an emulator.

### Expected results

When running an integration test, either with the `flutter_driver` package or `integration_test package`, I expect all timeline events to be exported via the `stopTracingAndDownloadTimeline` (flutter_driver) or `responseDataCallback` (integration_test) functions.

### Actual results

Both of these seem to hit a hard limit at trying to generate timeline.json files larger than ~8mb or ~40,000 lines, though I don't know if the limit is related to the file size or event count.

### Code sample

<details open><summary>main.dart</summary>

```dart
import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  bool isAnimating = false;

  void _toggleAnimation() async {
    setState(() => isAnimating = true);
    await Future.delayed(const Duration(milliseconds: 2500)).then((_) {
      setState(() => isAnimating = false);
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: Text(widget.title),
      ),
      body: Center(
        child: isAnimating
            ? const SizedBox(
                width: 48,
                height: 48,
                child: CircularProgressIndicator(),
              )
            : const SizedBox(),
      ),
      floatingActionButton: FloatingActionButton(
        key: const ValueKey('fab'),
        onPressed: () => _toggleAnimation(),
        child: const Icon(Icons.play_arrow_rounded),
      ),
    );
  }
}

```

</details>

<details open><summary>flutter_driver test</summary>

run with:
```console
flutter drive --target=test_driver/driver.dart --profile
```
**driver.dart**
```dart
import 'package:flutter_driver/driver_extension.dart';
import 'package:integration_test_example/main.dart' as app;

void main() {
  enableFlutterDriverExtension();
  app.main();
}
```

**driver_test.dart**
```dart
import 'package:flutter_driver/flutter_driver.dart';
import 'package:test/test.dart';

const kTestCount = 1;

final fab = find.byValueKey('fab');

void main() {
  group(
    'end-to-end test',
    () {
      FlutterDriver? driver;

      setUpAll(() async => driver = await FlutterDriver.connect());

      tearDownAll(() async => driver?.close());

      test('Tests timeline events', () async {
        await driver?.startTracing();

        await Future.forEach(List.generate(kTestCount, (i) => i), (i) async {
          print('Test count: $i');

          await Future.delayed(const Duration(seconds: 1));

          await driver?.tap(fab);
          await Future.delayed(const Duration(milliseconds: 3000));

          await driver?.tap(fab);
          await Future.delayed(const Duration(milliseconds: 3000));

          await driver?.tap(fab);
          await Future.delayed(const Duration(milliseconds: 3000));

          await driver?.tap(fab);
          await Future.delayed(const Duration(milliseconds: 3000));
        });

        final timeline = await driver?.stopTracingAndDownloadTimeline(
          timeout: const Duration(minutes: 1),
        );

        if (timeline != null) {
          final summary = TimelineSummary.summarize(timeline);
          await summary.writeTimelineToFile(
            'flutter_driver_timeline_${DateTime.now()}',
            pretty: true,
            includeSummary: true,
          );
        }
      });
    },
  );
}

```

</details>

<details open><summary>integration_test test</summary>

run with:
```console
flutter drive \
--driver=test_driver/perf_driver.dart \
--target=integration_test/loading_animation_integration_test.dart \
--profile --no-dds -d 06JAY1YQ6A

```
**perf_driver.dart**
```dart
import 'package:flutter_driver/flutter_driver.dart' as driver;
import 'package:integration_test/integration_test_driver.dart';

Future<void> main() {
  return integrationDriver(
    responseDataCallback: (data) async {
      if (data != null) {
        final timeline = driver.Timeline.fromJson(
          data['timeline'] as Map<String, dynamic>,
        );

        // Convert the Timeline into a TimelineSummary that's easier to
        // read and understand.
        final summary = driver.TimelineSummary.summarize(timeline);

        // Then, write the entire timeline to disk in a json format.
        // This file can be opened in the Chrome browser's tracing tools
        // found by navigating to chrome://tracing.
        // Optionally, save the summary to disk by setting includeSummary
        // to true
        await summary.writeTimelineToFile(
          'integration_test_timeline${DateTime.now()}',
          pretty: true,
          includeSummary: true,
        );
      }
    },
  );
}

```

**loading_animation_integration_test.dart**
```dart
// ignore_for_file: avoid_print

import 'package:flutter/foundation.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:integration_test/integration_test.dart';
import 'package:integration_test_example/main.dart';

const kTestCount = 1;

void main() {
  final binding = IntegrationTestWidgetsFlutterBinding.ensureInitialized();
  binding.framePolicy = LiveTestWidgetsFlutterBindingFramePolicy.benchmarkLive;

  group('end-to-end test', () {
    testWidgets('Tests timeline events', (tester) async {
      final fab = find.byKey(const ValueKey('fab'));

      await binding.traceAction(() async {
        await tester.pumpWidget(const MyApp());
        await Future.delayed(const Duration(seconds: 3));

        await Future.forEach(List.generate(kTestCount, (i) => i), (i) async {
          print('Test count: $i');

          await Future.delayed(const Duration(seconds: 1));

          await tester.tap(fab);
          await Future.delayed(const Duration(milliseconds: 3000));

          await tester.tap(fab);
          await Future.delayed(const Duration(milliseconds: 3000));

          await tester.tap(fab);
          await Future.delayed(const Duration(milliseconds: 3000));

          await tester.tap(fab);
          await Future.delayed(const Duration(milliseconds: 3000));
        });
      });
    });
  });
}

```

</details>

### Logs

<details open><summary>flutter_driver logs</summary>

```console
pauldhaliwal@Pauls-MacBook-Pro integration_test_example % flutter drive --target=test_driver/driver.dart --profile                          
Resolving dependencies...
Downloading packages...
  _fe_analyzer_shared 70.0.0 (76.0.0 available)
  analyzer 6.5.2 (6.11.0 available)
  async 2.11.0 (2.12.0 available)
  boolean_selector 2.1.1 (2.1.2 available)
  characters 1.3.0 (1.3.1 available)
  clock 1.1.1 (1.1.2 available)
  collection 1.18.0 (1.19.1 available)
  fake_async 1.3.1 (1.3.2 available)
  file 7.0.0 (7.0.1 available)
  flutter_lints 4.0.0 (5.0.0 available)
  http_parser 4.0.2 (4.1.1 available)
  leak_tracker 10.0.5 (10.0.8 available)
  leak_tracker_flutter_testing 3.0.5 (3.0.9 available)
  lints 4.0.0 (5.1.0 available)
  macros 0.1.2-main.4 (0.1.3-main.0 available)
  matcher 0.12.16+1 (0.12.17 available)
  material_color_utilities 0.11.1 (0.12.0 available)
  meta 1.15.0 (1.16.0 available)
  path 1.9.0 (1.9.1 available)
  platform 3.1.5 (3.1.6 available)
  process 5.0.2 (5.0.3 available)
  shelf 1.4.1 (1.4.2 available)
  stack_trace 1.11.1 (1.12.0 available)
  string_scanner 1.2.0 (1.4.0 available)
  test 1.25.7 (1.25.8 available)
  test_api 0.7.2 (0.7.3 available)
  test_core 0.6.4 (0.6.5 available)
  typed_data 1.3.2 (1.4.0 available)
  vm_service 14.2.5 (14.3.1 available)
  webdriver 3.0.3 (3.0.4 available)
Got dependencies!
30 packages have newer versions incompatible with dependency constraints.
Try `flutter pub outdated` for more information.
Running Gradle task 'assembleProfile'...                            3.2s
✓ Built build/app/outputs/flutter-apk/app-profile.apk (15.1MB)
Installing build/app/outputs/flutter-apk/app-profile.apk...      1,413ms
00:00 +0: end-to-end test (setUpAll)

VMServiceFlutterDriver: Connecting to Flutter application at http://127.0.0.1:58502/3Xb8MEqfNSY=/
VMServiceFlutterDriver: Isolate found with number: 7105564028724491
VMServiceFlutterDriver: Isolate is paused at start.
VMServiceFlutterDriver: Attempting to resume isolate
VMServiceFlutterDriver: Connected to Flutter application.
00:00 +0: end-to-end test Tests timeline events

I/Gralloc4(26041): mapper 4.x is not supported
W/Gralloc3(26041): mapper 3.x is not supported
Test count: 0

W/Gralloc4(26041): allocator 4.x is not supported
W/Gralloc3(26041): allocator 3.x is not supported
D/ProfileInstaller(26041): Installing profile for com.example.integration_test_example
00:13 +1: end-to-end test (tearDownAll)

00:13 +1: All tests passed!
```

</details>


<details open><summary>integration_test logs</summary>

```console
pauldhaliwal@Pauls-MacBook-Pro integration_test_example % fvm flutter drive \
--driver=test_driver/perf_driver.dart \
--target=integration_test/loading_animation_integration_test.dart \
--profile --no-dds -d 06JAY1YQ6A
Resolving dependencies...
Downloading packages...
  _fe_analyzer_shared 70.0.0 (76.0.0 available)
  analyzer 6.5.2 (6.11.0 available)
  async 2.11.0 (2.12.0 available)
  boolean_selector 2.1.1 (2.1.2 available)
  characters 1.3.0 (1.3.1 available)
  clock 1.1.1 (1.1.2 available)
  collection 1.18.0 (1.19.1 available)
  fake_async 1.3.1 (1.3.2 available)
  file 7.0.0 (7.0.1 available)
  flutter_lints 4.0.0 (5.0.0 available)
  http_parser 4.0.2 (4.1.1 available)
  leak_tracker 10.0.5 (10.0.8 available)
  leak_tracker_flutter_testing 3.0.5 (3.0.9 available)
  lints 4.0.0 (5.1.0 available)
  macros 0.1.2-main.4 (0.1.3-main.0 available)
  matcher 0.12.16+1 (0.12.17 available)
  material_color_utilities 0.11.1 (0.12.0 available)
  meta 1.15.0 (1.16.0 available)
  path 1.9.0 (1.9.1 available)
  platform 3.1.5 (3.1.6 available)
  process 5.0.2 (5.0.3 available)
  shelf 1.4.1 (1.4.2 available)
  stack_trace 1.11.1 (1.12.0 available)
  string_scanner 1.2.0 (1.4.0 available)
  test 1.25.7 (1.25.8 available)
  test_api 0.7.2 (0.7.3 available)
  test_core 0.6.4 (0.6.5 available)
  typed_data 1.3.2 (1.4.0 available)
  vm_service 14.2.5 (14.3.1 available)
  webdriver 3.0.3 (3.0.4 available)
Got dependencies!
30 packages have newer versions incompatible with dependency constraints.
Try `flutter pub outdated` for more information.
Running Gradle task 'assembleProfile'...                         2,118ms
✓ Built build/app/outputs/flutter-apk/app-profile.apk (17.8MB)
Installing build/app/outputs/flutter-apk/app-profile.apk...      1,355ms
I/AdrenoGLES-0(26506): QUALCOMM build                   : 781e7d0, I46ff5fc46f
I/AdrenoGLES-0(26506): Build Date                       : 12/01/20
I/AdrenoGLES-0(26506): OpenGL ES Shader Compiler Version: EV031.31.04.01
I/AdrenoGLES-0(26506): Local Branch                     : QPR1
I/AdrenoGLES-0(26506): Remote Branch                    : 
I/AdrenoGLES-0(26506): Remote Branch                    : 
I/AdrenoGLES-0(26506): Reconstruct Branch               : 
I/AdrenoGLES-0(26506): Build Config                     : C P 11.0.1 AArch64
I/AdrenoGLES-0(26506): Driver Path                      : /vendor/lib64/egl/libGLESv2_adreno.so
I/AdrenoGLES-0(26506): PFP: 0x016ee190, ME: 0x00000000
VMServiceFlutterDriver: Connecting to Flutter application at http://127.0.0.1:59326/JNVdMK9wqpY=/
VMServiceFlutterDriver: Isolate found with number: 8653515500389835
VMServiceFlutterDriver: Isolate is paused at start.
VMServiceFlutterDriver: Attempting to resume isolate
I/flutter (26506): 00:00 +0: end-to-end test Tests timeline events
I/Gralloc4(26506): mapper 4.x is not supported
W/Gralloc3(26506): mapper 3.x is not supported
W/Gralloc4(26506): allocator 4.x is not supported
W/Gralloc3(26506): allocator 3.x is not supported
VMServiceFlutterDriver: Connected to Flutter application.
I/flutter (26506): Test count: 0
D/ProfileInstaller(26506): Installing profile for com.example.integration_test_example
I/flutter (26506): 00:16 +1: (tearDownAll)
I/flutter (26506): 00:17 +2: All tests passed!
All tests passed.
```

</details>


### Flutter Doctor output

<details open><summary>Doctor output</summary>

```console
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 3.24.4, on macOS 14.6 23G80 darwin-arm64, locale en-CA)
[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
[✓] Xcode - develop for iOS and macOS (Xcode 15.0)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2023.2)
[✓] IntelliJ IDEA Community Edition (version 2023.3.4)
[✓] Connected device (3 available)
[✓] Network resources

• No issues found!

```

</details>