Skip to content

Commit 3c0d77a

Browse files
committed
Add ui_benchmarks
We first add a PlatformMessageResponseDartComplete benchmark to test flutter#18838. Specifically, it improves from ~7600 us to ~1200 us after that PR on my MacBook Pro.
1 parent 1482d9b commit 3c0d77a

File tree

8 files changed

+202
-0
lines changed

8 files changed

+202
-0
lines changed

.cirrus.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,13 @@ task:
6262
./txt_benchmarks --benchmark_format=json > txt_benchmarks.json
6363
./fml_benchmarks --benchmark_format=json > fml_benchmarks.json
6464
./shell_benchmarks --benchmark_format=json > shell_benchmarks.json
65+
./ui_benchmarks --benchmark_format=json > ui_benchmarks.json
6566
cd $ENGINE_PATH/src/flutter/testing/benchmark
6667
pub get
6768
dart bin/parse_and_send.dart ../../../out/host_release/txt_benchmarks.json
6869
dart bin/parse_and_send.dart ../../../out/host_release/fml_benchmarks.json
6970
dart bin/parse_and_send.dart ../../../out/host_release/shell_benchmarks.json
71+
dart bin/parse_and_send.dart ../../../out/host_release/ui_benchmarks.json
7072
- name: build_and_test_linux_release
7173
compile_host_script: |
7274
cd $ENGINE_PATH/src

lib/ui/BUILD.gn

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,4 +180,20 @@ if (current_toolchain == host_toolchain) {
180180
"//third_party/dart/runtime/bin:elf_loader",
181181
]
182182
}
183+
184+
executable("ui_benchmarks") {
185+
testonly = true
186+
187+
sources = [
188+
"ui_benchmarks.cc",
189+
]
190+
191+
deps = [
192+
":ui",
193+
":ui_unittests_fixtures",
194+
"//flutter/benchmarking",
195+
"//flutter/shell/common:common",
196+
"//flutter/testing:fixture_test",
197+
]
198+
}
183199
}

lib/ui/fixtures/ui_test.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,7 @@ void _validateVertices(Vertices vertices) native 'ValidateVertices';
4040
void frameCallback(FrameInfo info) {
4141
print('called back');
4242
}
43+
44+
@pragma('vm:entry-point')
45+
void messageCallback(dynamic data) {
46+
}

lib/ui/ui_benchmarks.cc

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#include "flutter/benchmarking/benchmarking.h"
6+
#include "flutter/common/settings.h"
7+
#include "flutter/lib/ui/window/platform_message_response_dart.h"
8+
#include "flutter/runtime/dart_vm_lifecycle.h"
9+
#include "flutter/shell/common/thread_host.h"
10+
#include "flutter/testing/dart_isolate_runner.h"
11+
#include "flutter/testing/fixture_test.h"
12+
13+
#include <future>
14+
15+
namespace flutter {
16+
17+
class Fixture : public testing::FixtureTest {
18+
void TestBody() override{};
19+
};
20+
21+
static void BM_PlatformMessageResponseDartComplete(benchmark::State& state) {
22+
ThreadHost thread_host("test",
23+
ThreadHost::Type::Platform | ThreadHost::Type::GPU |
24+
ThreadHost::Type::IO | ThreadHost::Type::UI);
25+
TaskRunners task_runners("test", thread_host.platform_thread->GetTaskRunner(),
26+
thread_host.raster_thread->GetTaskRunner(),
27+
thread_host.ui_thread->GetTaskRunner(),
28+
thread_host.io_thread->GetTaskRunner());
29+
Fixture fixture;
30+
auto settings = fixture.CreateSettingsForFixture();
31+
auto vm_ref = DartVMRef::Create(settings);
32+
auto isolate =
33+
testing::RunDartCodeInIsolate(vm_ref, settings, task_runners, "main", {},
34+
testing::GetFixturesPath(), {});
35+
36+
while (state.KeepRunning()) {
37+
state.PauseTiming();
38+
bool successful = isolate->RunInIsolateScope([&]() -> bool {
39+
Dart_Handle library = Dart_RootLibrary();
40+
41+
// Simulate a message of 3 MB
42+
std::vector<uint8_t> data(3 << 20, 0);
43+
Dart_Handle closure =
44+
Dart_GetField(library, Dart_NewStringFromCString("messageCallback"));
45+
46+
std::unique_ptr<fml::Mapping> mapping =
47+
std::make_unique<fml::DataMapping>(data);
48+
auto message = fml::MakeRefCounted<PlatformMessageResponseDart>(
49+
tonic::DartPersistentValue(isolate->get(), closure),
50+
thread_host.ui_thread->GetTaskRunner());
51+
message->Complete(std::move(mapping));
52+
return true;
53+
});
54+
FML_CHECK(successful);
55+
state.ResumeTiming();
56+
57+
// We skip timing everything above because the copy triggered by
58+
// message->Complete is a task posted on the UI thread. The following wait
59+
// for a UI task would let us know when that copy is done.
60+
std::promise<bool> completed;
61+
task_runners.GetUITaskRunner()->PostTask([&completed]{
62+
completed.set_value(true);
63+
});
64+
completed.get_future().wait();
65+
}
66+
}
67+
68+
BENCHMARK(BM_PlatformMessageResponseDartComplete)
69+
->Unit(benchmark::kMicrosecond);
70+
71+
} // namespace flutter

testing/BUILD.gn

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,21 @@ source_set("skia") {
8282
]
8383
}
8484

85+
source_set("fixture_test") {
86+
testonly = true
87+
88+
sources = [
89+
"fixture_test.cc",
90+
"fixture_test.h",
91+
]
92+
93+
public_deps = [
94+
":dart",
95+
"//flutter/common",
96+
"//flutter/runtime:runtime",
97+
]
98+
}
99+
85100
if (enable_unittests) {
86101
# SwiftShader only supports x86/x64_64
87102
if (target_cpu == "x86" || target_cpu == "x64") {

testing/fixture_test.cc

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#include "flutter/testing/fixture_test.h"
6+
7+
namespace flutter {
8+
namespace testing {
9+
10+
FixtureTest::FixtureTest()
11+
: native_resolver_(std::make_shared<TestDartNativeResolver>()),
12+
assets_dir_(fml::OpenDirectory(GetFixturesPath(),
13+
false,
14+
fml::FilePermission::kRead)),
15+
aot_symbols_(LoadELFSymbolFromFixturesIfNeccessary()) {}
16+
17+
Settings FixtureTest::CreateSettingsForFixture() {
18+
Settings settings;
19+
settings.leak_vm = false;
20+
settings.task_observer_add = [](intptr_t, fml::closure) {};
21+
settings.task_observer_remove = [](intptr_t) {};
22+
settings.isolate_create_callback = [this]() {
23+
native_resolver_->SetNativeResolverForIsolate();
24+
};
25+
settings.enable_observatory = false;
26+
SetSnapshotsAndAssets(settings);
27+
return settings;
28+
}
29+
30+
void FixtureTest::SetSnapshotsAndAssets(Settings& settings) {
31+
if (!assets_dir_.is_valid()) {
32+
return;
33+
}
34+
35+
settings.assets_dir = assets_dir_.get();
36+
37+
// In JIT execution, all snapshots are present within the binary itself and
38+
// don't need to be explicitly supplied by the embedder. In AOT, these
39+
// snapshots will be present in the application AOT dylib.
40+
if (DartVM::IsRunningPrecompiledCode()) {
41+
FML_CHECK(PrepareSettingsForAOTWithSymbols(settings, aot_symbols_));
42+
} else {
43+
settings.application_kernels = [this]() {
44+
std::vector<std::unique_ptr<const fml::Mapping>> kernel_mappings;
45+
kernel_mappings.emplace_back(
46+
fml::FileMapping::CreateReadOnly(assets_dir_, "kernel_blob.bin"));
47+
return kernel_mappings;
48+
};
49+
}
50+
}
51+
52+
} // namespace testing
53+
} // namespace flutter

testing/fixture_test.h

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#ifndef FLUTTER_TESTING_FIXTURE_TEST_H_
6+
#define FLUTTER_TESTING_FIXTURE_TEST_H_
7+
8+
#include <memory>
9+
10+
#include "flutter/common/settings.h"
11+
#include "flutter/runtime/dart_vm.h"
12+
#include "flutter/testing/elf_loader.h"
13+
#include "flutter/testing/test_dart_native_resolver.h"
14+
#include "flutter/testing/testing.h"
15+
#include "flutter/testing/thread_test.h"
16+
17+
namespace flutter {
18+
namespace testing {
19+
20+
class FixtureTest : public ThreadTest {
21+
public:
22+
FixtureTest();
23+
24+
Settings CreateSettingsForFixture();
25+
26+
private:
27+
std::shared_ptr<TestDartNativeResolver> native_resolver_;
28+
fml::UniqueFD assets_dir_;
29+
ELFAOTSymbols aot_symbols_;
30+
31+
void SetSnapshotsAndAssets(Settings& settings);
32+
33+
FML_DISALLOW_COPY_AND_ASSIGN(FixtureTest);
34+
};
35+
36+
} // namespace testing
37+
} // namespace flutter
38+
39+
#endif // FLUTTER_TESTING_FIXTURE_TEST_H_

testing/run_tests.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,8 @@ def RunEngineBenchmarks(build_dir, filter):
160160

161161
RunEngineExecutable(build_dir, 'fml_benchmarks', filter)
162162

163+
RunEngineExecutable(build_dir, 'ui_benchmarks', filter)
164+
163165
if IsLinux():
164166
RunEngineExecutable(build_dir, 'txt_benchmarks', filter)
165167

0 commit comments

Comments
 (0)