Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove methods toJson and fromJson. #5989

Merged
merged 11 commits into from
Jul 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// found in the LICENSE file.

import 'dart:async';
import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:leak_tracker/devtools_integration.dart';
Expand Down Expand Up @@ -116,47 +115,25 @@ class LeaksPaneController {
notGCed: notGCedAnalyzed,
);

_saveResultAndSetAnalysisStatus(yaml, task);
_saveResultAndSetAnalysisStatus(yaml);
} catch (error) {
analysisStatus.message.value = 'Error: $error';
analysisStatus.status.value = AnalysisStatus.showingError;
}
}

void _saveResultAndSetAnalysisStatus(
String yaml,
NotGCedAnalyzerTask? task,
) async {
void _saveResultAndSetAnalysisStatus(String yaml) {
final now = DateTime.now();
final yamlFile = ExportController.generateFileName(
time: now,
prefix: yamlFilePrefix,
type: ExportFileType.yaml,
);
_exportController.downloadFile(yaml, fileName: yamlFile);
final String? taskFile = _saveTask(task, now);

final taskFileMessage = taskFile == null ? '' : ' and $taskFile';
await _setMessageWithDelay(
'Downloaded the leak analysis to $yamlFile$taskFileMessage.',
);
analysisStatus.status.value = AnalysisStatus.showingResult;
}

/// Saves raw analysis task for troubleshooting and deeper analysis.
String? _saveTask(NotGCedAnalyzerTask? task, DateTime? now) {
if (task == null) return null;

final json = jsonEncode(task.toJson());
final jsonFile = ExportController.generateFileName(
time: now,
prefix: yamlFilePrefix,
postfix: '.raw',
type: ExportFileType.json,
);
return _exportController.downloadFile(json, fileName: jsonFile);
}

Future<void> _setMessageWithDelay(String message) async {
analysisStatus.message.value = message;
await delayToReleaseUiThread(micros: 5000);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import '../../../shared/heap/spanning_tree.dart';
import 'model.dart';

/// Analyzes notGCed leaks and returns result of the analysis.
// TODO(polina-c): add tests for this method.
// https://github.com/flutter/devtools/issues/3951
Future<NotGCedAnalyzed> analyzeNotGCed(NotGCedAnalyzerTask task) async {
await analyzeHeapAndSetRetainingPaths(task.heap, task.reports);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,6 @@ import 'package:vm_service/vm_service.dart';

import '../../../../../shared/memory/adapted_heap_data.dart';

/// Names for json fields.
class _JsonFields {
static const String reports = 'reports';
static const String heap = 'heap';
}

/// Result of analysis of [notGCed] memory leaks.
class NotGCedAnalyzed {
NotGCedAnalyzed({
Expand All @@ -37,16 +31,6 @@ class NotGCedAnalyzerTask {
required this.reports,
});

factory NotGCedAnalyzerTask.fromJson(Map<String, Object?> json) =>
NotGCedAnalyzerTask(
reports: (json[_JsonFields.reports] as List<Object?>)
.map((e) => LeakReport.fromJson((e as Map).cast<String, Object?>()))
.toList(),
heap: AdaptedHeapData.fromJson(
json[_JsonFields.heap] as Map<String, Object?>,
),
);

static Future<NotGCedAnalyzerTask> fromSnapshot(
HeapSnapshotGraph graph,
List<LeakReport> reports,
Expand All @@ -60,9 +44,4 @@ class NotGCedAnalyzerTask {

final AdaptedHeapData heap;
final List<LeakReport> reports;

Map<String, Object?> toJson() => {
_JsonFields.reports: reports.map((e) => e.toJson()).toList(),
_JsonFields.heap: heap.toJson(),
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,15 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:collection/collection.dart';
import 'dart:io';

import 'package:flutter/foundation.dart';
import 'package:vm_service/vm_service.dart';

import '../primitives/utils.dart';
import 'adapted_heap_object.dart';
import 'simple_items.dart';

/// Names for json fields.
class _JsonFields {
static const String objects = 'objects';
static const String rootIndex = 'rootIndex';
static const String created = 'created';
static const String isolateId = 'isolateId';
}

@immutable
class HeapObjectSelection {
const HeapObjectSelection(this.heap, {required this.object});
Expand Down Expand Up @@ -66,21 +59,6 @@ class AdaptedHeapData {
this.created = created ?? DateTime.now();
}

factory AdaptedHeapData.fromJson(Map<String, dynamic> json) {
final createdJson = json[_JsonFields.created];

return AdaptedHeapData(
(json[_JsonFields.objects] as List<Object?>)
.mapIndexed(
(i, e) => AdaptedHeapObject.fromJson(e as Map<String, Object?>, i),
)
.toList(),
created: createdJson == null ? null : DateTime.parse(createdJson),
rootIndex: json[_JsonFields.rootIndex] ?? _defaultRootIndex,
isolateId: json[_JsonFields.isolateId] ?? '',
);
}

static final _uiReleaser = UiReleaser();

static Future<AdaptedHeapData> fromHeapSnapshot(
Expand All @@ -98,6 +76,18 @@ class AdaptedHeapData {
return AdaptedHeapData(objects, isolateId: isolateId);
}

@visibleForTesting
static Future<AdaptedHeapData> fromFile(
String fileName, {
String isolateId = '',
}) async {
final file = File(fileName);
final bytes = await file.readAsBytes();
final data = bytes.buffer.asByteData();
final graph = HeapSnapshotGraph.fromChunks([data]);
return AdaptedHeapData.fromHeapSnapshot(graph, isolateId: isolateId);
}

/// Default value for rootIndex is taken from the doc:
/// https://github.com/dart-lang/sdk/blob/main/runtime/vm/service/heap_snapshot.md#object-ids
static const int _defaultRootIndex = 1;
Expand Down Expand Up @@ -126,12 +116,6 @@ class AdaptedHeapData {
for (var i in Iterable.generate(objects.length)) objects[i].code: i,
};

Map<String, dynamic> toJson() => {
_JsonFields.objects: objects.map((e) => e.toJson()).toList(),
_JsonFields.rootIndex: rootIndex,
_JsonFields.created: created.toIso8601String(),
};

int? objectIndexByIdentityHashCode(IdentityHashCode code) =>
_objectsByCode[code];

Expand Down

This file was deleted.

49 changes: 0 additions & 49 deletions packages/devtools_app/test/memory/leaks/model_test.dart

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import '../../../test_infra/test_data/memory/heap/heap_data.dart';

void main() {
for (var t in goldenHeapTests) {
group(t.name, () {
group(t.fileName, () {
late AdaptedHeapData heap;

setUp(() async {
Expand All @@ -22,22 +22,22 @@ void main() {
expect(
heap.objects[heap.rootIndex].outRefs.length,
greaterThan(1000),
reason: t.name,
reason: t.fileName,
);
});

test('has exactly one object of type ${t.appClassName}.', () {
final appObjects =
heap.objects.where((o) => o.heapClass.className == t.appClassName);
expect(appObjects, hasLength(1), reason: t.name);
expect(appObjects, hasLength(1), reason: t.fileName);
});

test('has path to the object of type ${t.appClassName}.', () async {
await calculateHeap(heap);
final appObject = heap.objects
.where((o) => o.heapClass.className == t.appClassName)
.first;
expect(appObject.retainer, isNotNull, reason: t.name);
expect(appObject.retainer, isNotNull, reason: t.fileName);
});
});
}
Expand Down
21 changes: 0 additions & 21 deletions packages/devtools_app/test/memory/shared/heap/model_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -58,27 +58,6 @@ final _heapPathTests = <_HeapPathTest>[
];

void main() {
test('$AdaptedHeapData serializes.', () {
final json = AdaptedHeapData(
[
AdaptedHeapObject(
code: 1,
outRefs: {3, 4, 5},
heapClass: HeapClassName(
className: 'class',
library: 'library',
),
shallowSize: 1,
),
],
rootIndex: 0,
isolateId: '',
created: DateTime(2000),
).toJson();

expect(json, AdaptedHeapData.fromJson(json).toJson());
});

test('$HeapPath.isRetainedBySameClass returns expected result for.', () {
for (final t in _heapPathTests) {
expect(
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
To retake the snapshots:

1. Create counter app with `flutter create`.
2. Update button handler to take snapshot after increasing counter:

```
void _incrementCounter() {
setState(() {
_counter++;
});
var fileName = 'counter_snapshot$_counter';
fileName = p.absolute(fileName);
print('saving snapshot to $fileName');
NativeRuntime.writeHeapSnapshotToFile(fileName);
}
```

3. Run the counter and click the button four times.
4. Copy the collected files to this folder.
Binary file not shown.

This file was deleted.

Binary file not shown.

This file was deleted.

Loading