* Reland "add a benchmark test for stack size (#75039)"
This reverts commit de683de9d5.
* fix typo
This commit is contained in:
@@ -19,5 +19,8 @@ const String kImageFilteredTransformAnimationRouteName = '/imagefiltered_transfo
|
||||
const String kMultiWidgetConstructionRouteName = '/multi_widget_construction';
|
||||
const String kHeavyGridViewRouteName = '/heavy_gridview';
|
||||
const String kSimpleScrollRouteName = '/simple_scroll';
|
||||
const String kStackSizeRouteName = '/stack_size';
|
||||
|
||||
const String kScrollableName = '/macrobenchmark_listview';
|
||||
|
||||
const String kStackSizeKey = 'stack_size';
|
||||
|
||||
@@ -21,6 +21,7 @@ import 'src/picture_cache.dart';
|
||||
import 'src/post_backdrop_filter.dart';
|
||||
import 'src/simple_animation.dart';
|
||||
import 'src/simple_scroll.dart';
|
||||
import 'src/stack_size.dart';
|
||||
import 'src/text.dart';
|
||||
|
||||
const String kMacrobenchmarks = 'Macrobenchmarks';
|
||||
@@ -54,6 +55,7 @@ class MacrobenchmarksApp extends StatelessWidget {
|
||||
kMultiWidgetConstructionRouteName: (BuildContext context) => const MultiWidgetConstructTable(10, 20),
|
||||
kHeavyGridViewRouteName: (BuildContext context) => HeavyGridViewPage(),
|
||||
kSimpleScrollRouteName: (BuildContext context) => SimpleScroll(),
|
||||
kStackSizeRouteName: (BuildContext context) => StackSizePage(),
|
||||
},
|
||||
);
|
||||
}
|
||||
@@ -181,6 +183,13 @@ class HomePage extends StatelessWidget {
|
||||
Navigator.pushNamed(context, kLargeImageChangerRouteName);
|
||||
},
|
||||
),
|
||||
ElevatedButton(
|
||||
key: const Key(kStackSizeRouteName),
|
||||
child: const Text('Stack Size'),
|
||||
onPressed: () {
|
||||
Navigator.pushNamed(context, kStackSizeRouteName);
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
|
||||
122
dev/benchmarks/macrobenchmarks/lib/src/stack_size.dart
Normal file
122
dev/benchmarks/macrobenchmarks/lib/src/stack_size.dart
Normal file
@@ -0,0 +1,122 @@
|
||||
// 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 'dart:ffi' as ffi;
|
||||
import 'dart:io' as io;
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../common.dart';
|
||||
|
||||
typedef GetStackPointerCallback = int Function();
|
||||
|
||||
// c interop function:
|
||||
// void* mmap(void* addr, size_t len, int prot, int flags, int fd, off_t offset);
|
||||
typedef CMmap = ffi.Pointer<ffi.Void> Function(
|
||||
ffi.Pointer<ffi.Void>, ffi.IntPtr, ffi.Int32, ffi.Int32, ffi.Int32, ffi.IntPtr);
|
||||
typedef DartMmap = ffi.Pointer<ffi.Void> Function(
|
||||
ffi.Pointer<ffi.Void>, int, int, int, int, int);
|
||||
final DartMmap mmap = ffi.DynamicLibrary.process().lookupFunction<CMmap, DartMmap>('mmap');
|
||||
|
||||
// c interop function:
|
||||
// int mprotect(void* addr, size_t len, int prot);
|
||||
typedef CMprotect = ffi.Int32 Function(ffi.Pointer<ffi.Void>, ffi.IntPtr, ffi.Int32);
|
||||
typedef DartMprotect = int Function(ffi.Pointer<ffi.Void>, int, int);
|
||||
final DartMprotect mprotect = ffi.DynamicLibrary.process()
|
||||
.lookupFunction<CMprotect, DartMprotect>('mprotect');
|
||||
|
||||
const int kProtRead = 1;
|
||||
const int kProtWrite = 2;
|
||||
const int kProtExec = 4;
|
||||
|
||||
const int kMapPrivate = 0x02;
|
||||
const int kMapJit = 0x0;
|
||||
const int kMapAnon = 0x20;
|
||||
|
||||
const int kMemorySize = 16;
|
||||
const int kInvalidFileDescriptor = -1;
|
||||
const int kkFileMappingOffset = 0;
|
||||
|
||||
const int kMemoryStartingIndex = 0;
|
||||
|
||||
const int kExitCodeSuccess = 0;
|
||||
|
||||
final GetStackPointerCallback getStackPointer = () {
|
||||
// Makes sure we are running on an Android arm64 device.
|
||||
if (!io.Platform.isAndroid)
|
||||
throw 'This benchmark test can only be run on Android arm64 devices.';
|
||||
final io.ProcessResult result = io.Process.runSync('getprop', <String>['ro.product.cpu.abi']);
|
||||
if (result.exitCode != 0)
|
||||
throw 'Failed to retrieve CPU information.';
|
||||
if (!result.stdout.toString().contains('arm64'))
|
||||
throw 'This benchmark test can only be run on Android arm64 devices.';
|
||||
|
||||
// Creates a block of memory to store the assembly code.
|
||||
final ffi.Pointer<ffi.Void> region = mmap(ffi.nullptr, kMemorySize, kProtRead | kProtWrite,
|
||||
kMapPrivate | kMapAnon | kMapJit, kInvalidFileDescriptor, kkFileMappingOffset);
|
||||
if (region == ffi.nullptr) {
|
||||
throw 'Failed to acquire memory for the test.';
|
||||
}
|
||||
|
||||
// Writes the assembly code into the memory block. This assembly code returns
|
||||
// the memory address of the stack pointer.
|
||||
region.cast<ffi.Uint8>().asTypedList(kMemorySize).setAll(
|
||||
kMemoryStartingIndex,
|
||||
<int>[
|
||||
// "mov x0, sp" in machine code: E0030091.
|
||||
0xe0, 0x03, 0x00, 0x91,
|
||||
// "ret" in machine code: C0035FD6.
|
||||
0xc0, 0x03, 0x5f, 0xd6
|
||||
]
|
||||
);
|
||||
|
||||
// Makes sure the memory block is executable.
|
||||
if (mprotect(region, kMemorySize, kProtRead | kProtExec) != kExitCodeSuccess) {
|
||||
throw 'Failed to write executable code to the memory.';
|
||||
}
|
||||
return region
|
||||
.cast<ffi.NativeFunction<ffi.IntPtr Function()>>()
|
||||
.asFunction<int Function()>();
|
||||
}();
|
||||
|
||||
class StackSizePage extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Material(
|
||||
child: Column(
|
||||
children: <Widget>[
|
||||
Container(
|
||||
width: 200,
|
||||
height: 100,
|
||||
child: ParentWidget(),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class ParentWidget extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final int myStackSize = getStackPointer();
|
||||
return ChildWidget(parentStackSize: myStackSize);
|
||||
}
|
||||
}
|
||||
|
||||
class ChildWidget extends StatelessWidget {
|
||||
const ChildWidget({this.parentStackSize, Key key}) : super(key: key);
|
||||
final int parentStackSize;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final int myStackSize = getStackPointer();
|
||||
// Captures the stack size difference between parent widget and child widget
|
||||
// during the rendering pipeline, i.e. one layer of stateless widget.
|
||||
return Text(
|
||||
'${parentStackSize - myStackSize}',
|
||||
key: const ValueKey<String>(kStackSizeKey),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
// 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 'dart:convert' show JsonEncoder;
|
||||
|
||||
import 'package:flutter_driver/flutter_driver.dart';
|
||||
import 'package:file/file.dart';
|
||||
import 'package:path/path.dart' as path;
|
||||
import 'package:test/test.dart' hide TypeMatcher, isInstanceOf;
|
||||
|
||||
import 'package:macrobenchmarks/common.dart';
|
||||
|
||||
import 'util.dart';
|
||||
|
||||
const JsonEncoder _prettyEncoder = JsonEncoder.withIndent(' ');
|
||||
|
||||
void main() {
|
||||
test('stack_size', () async {
|
||||
int stackSizeInBytes;
|
||||
await runDriverTestForRoute(kStackSizeRouteName, (FlutterDriver driver) async {
|
||||
final String stackSize = await driver.getText(find.byValueKey(kStackSizeKey));
|
||||
expect(stackSize.isNotEmpty, isTrue);
|
||||
stackSizeInBytes = int.parse(stackSize);
|
||||
});
|
||||
|
||||
expect(stackSizeInBytes > 0, isTrue);
|
||||
|
||||
await fs.directory(testOutputsDirectory).create(recursive: true);
|
||||
final File file = fs.file(path.join(testOutputsDirectory, 'stack_size.json'));
|
||||
await file.writeAsString(_encodeJson(<String, dynamic>{
|
||||
'stack_size': stackSizeInBytes,
|
||||
}));
|
||||
}, timeout: const Timeout(kTimeout));
|
||||
}
|
||||
|
||||
String _encodeJson(Map<String, dynamic> jsonObject) {
|
||||
return _prettyEncoder.convert(jsonObject);
|
||||
}
|
||||
@@ -7,51 +7,64 @@ import 'package:test/test.dart' hide TypeMatcher, isInstanceOf;
|
||||
|
||||
import 'package:macrobenchmarks/common.dart';
|
||||
|
||||
const Duration kTimeout = Duration(seconds: 30);
|
||||
|
||||
typedef DriverTestCallBack = Future<void> Function(FlutterDriver driver);
|
||||
|
||||
Future<void> runDriverTestForRoute(String routeName, DriverTestCallBack body) async {
|
||||
final FlutterDriver driver = await FlutterDriver.connect();
|
||||
|
||||
// The slight initial delay avoids starting the timing during a
|
||||
// period of increased load on the device. Without this delay, the
|
||||
// benchmark has greater noise.
|
||||
// See: https://github.com/flutter/flutter/issues/19434
|
||||
await Future<void>.delayed(const Duration(milliseconds: 250));
|
||||
|
||||
await driver.forceGC();
|
||||
|
||||
final SerializableFinder scrollable = find.byValueKey(kScrollableName);
|
||||
expect(scrollable, isNotNull);
|
||||
final SerializableFinder button = find.byValueKey(routeName);
|
||||
expect(button, isNotNull);
|
||||
await driver.scrollUntilVisible(scrollable, button, dyScroll: -50.0);
|
||||
await driver.tap(button);
|
||||
|
||||
await body(driver);
|
||||
|
||||
driver.close();
|
||||
}
|
||||
|
||||
void macroPerfTest(
|
||||
String testName,
|
||||
String routeName,
|
||||
{ Duration pageDelay,
|
||||
Duration duration = const Duration(seconds: 3),
|
||||
Duration timeout = const Duration(seconds: 30),
|
||||
Duration timeout = kTimeout,
|
||||
Future<void> driverOps(FlutterDriver driver),
|
||||
Future<void> setupOps(FlutterDriver driver),
|
||||
}) {
|
||||
test(testName, () async {
|
||||
final FlutterDriver driver = await FlutterDriver.connect();
|
||||
Timeline timeline;
|
||||
await runDriverTestForRoute(routeName, (FlutterDriver driver) async {
|
||||
if (pageDelay != null) {
|
||||
// Wait for the page to load
|
||||
await Future<void>.delayed(pageDelay);
|
||||
}
|
||||
|
||||
// The slight initial delay avoids starting the timing during a
|
||||
// period of increased load on the device. Without this delay, the
|
||||
// benchmark has greater noise.
|
||||
// See: https://github.com/flutter/flutter/issues/19434
|
||||
await Future<void>.delayed(const Duration(milliseconds: 250));
|
||||
|
||||
await driver.forceGC();
|
||||
|
||||
final SerializableFinder scrollable = find.byValueKey(kScrollableName);
|
||||
expect(scrollable, isNotNull);
|
||||
final SerializableFinder button = find.byValueKey(routeName);
|
||||
expect(button, isNotNull);
|
||||
await driver.scrollUntilVisible(scrollable, button, dyScroll: -50.0);
|
||||
await driver.tap(button);
|
||||
|
||||
if (pageDelay != null) {
|
||||
// Wait for the page to load
|
||||
await Future<void>.delayed(pageDelay);
|
||||
}
|
||||
|
||||
if (setupOps != null) {
|
||||
if (setupOps != null) {
|
||||
await setupOps(driver);
|
||||
}
|
||||
}
|
||||
|
||||
final Timeline timeline = await driver.traceAction(() async {
|
||||
timeline = await driver.traceAction(() async {
|
||||
final Future<void> durationFuture = Future<void>.delayed(duration);
|
||||
if (driverOps != null) {
|
||||
await driverOps(driver);
|
||||
}
|
||||
await durationFuture;
|
||||
await durationFuture;
|
||||
});
|
||||
});
|
||||
|
||||
driver.close();
|
||||
expect(timeline, isNotNull);
|
||||
|
||||
final TimelineSummary summary = TimelineSummary.summarize(timeline);
|
||||
await summary.writeSummaryToFile(testName, pretty: true);
|
||||
|
||||
Reference in New Issue
Block a user