Flutter Engine
The Flutter Engine
ui_benchmarks.cc
Go to the documentation of this file.
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/volatile_path_tracker.h"
8#include "flutter/lib/ui/window/platform_message_response_dart.h"
9#include "flutter/runtime/dart_vm_lifecycle.h"
10#include "flutter/shell/common/thread_host.h"
11#include "flutter/testing/dart_isolate_runner.h"
12#include "flutter/testing/fixture_test.h"
13
14#include <future>
15
16namespace flutter {
17
19 void TestBody() override {};
20};
21
24 "test", ThreadHost::Type::kPlatform | ThreadHost::Type::kRaster |
25 ThreadHost::Type::kIo | ThreadHost::Type::kUi));
26 TaskRunners task_runners("test", thread_host.platform_thread->GetTaskRunner(),
27 thread_host.raster_thread->GetTaskRunner(),
28 thread_host.ui_thread->GetTaskRunner(),
29 thread_host.io_thread->GetTaskRunner());
30 Fixture fixture;
31 auto settings = fixture.CreateSettingsForFixture();
32 auto vm_ref = DartVMRef::Create(settings);
33 auto isolate =
34 testing::RunDartCodeInIsolate(vm_ref, settings, task_runners, "main", {},
36
37 while (state.KeepRunning()) {
38 state.PauseTiming();
39 bool successful = isolate->RunInIsolateScope([&]() -> bool {
40 // Simulate a message of 3 MB
41 std::vector<uint8_t> data(3 << 20, 0);
42 std::unique_ptr<fml::Mapping> mapping =
43 std::make_unique<fml::DataMapping>(data);
44
45 Dart_Handle library = Dart_RootLibrary();
47 Dart_GetField(library, Dart_NewStringFromCString("messageCallback"));
48
49 auto message = fml::MakeRefCounted<PlatformMessageResponseDart>(
50 tonic::DartPersistentValue(isolate->get(), closure),
51 thread_host.ui_thread->GetTaskRunner(), "");
52
53 message->Complete(std::move(mapping));
54
55 return true;
56 });
57 FML_CHECK(successful);
58 state.ResumeTiming();
59
60 // We skip timing everything above because the copy triggered by
61 // message->Complete is a task posted on the UI thread. The following wait
62 // for a UI task would let us know when that copy is done.
63 std::promise<bool> completed;
64 task_runners.GetUITaskRunner()->PostTask(
65 [&completed] { completed.set_value(true); });
66 completed.get_future().wait();
67 }
68}
69
72 "test", ThreadHost::Type::kPlatform | ThreadHost::Type::kRaster |
73 ThreadHost::Type::kIo | ThreadHost::Type::kUi));
74 TaskRunners task_runners("test", thread_host.platform_thread->GetTaskRunner(),
75 thread_host.raster_thread->GetTaskRunner(),
76 thread_host.ui_thread->GetTaskRunner(),
77 thread_host.io_thread->GetTaskRunner());
78
79 VolatilePathTracker tracker(task_runners.GetUITaskRunner(), true);
80
81 while (state.KeepRunning()) {
82 std::vector<std::shared_ptr<VolatilePathTracker::TrackedPath>> paths;
83 constexpr int path_count = 1000;
84 for (int i = 0; i < path_count; i++) {
85 auto path = std::make_shared<VolatilePathTracker::TrackedPath>();
86 path->path = SkPath();
87 path->path.setIsVolatile(true);
88 paths.push_back(std::move(path));
89 }
90
92 task_runners.GetUITaskRunner()->PostTask([&]() {
93 for (const auto& path : paths) {
94 tracker.Track(path);
95 }
96 latch.Signal();
97 });
98
99 latch.Wait();
100
101 task_runners.GetUITaskRunner()->PostTask([&]() { tracker.OnFrame(); });
102
103 for (int i = 0; i < path_count - 10; ++i) {
104 paths[i].reset();
105 }
106
107 task_runners.GetUITaskRunner()->PostTask([&]() { tracker.OnFrame(); });
108
109 latch.Reset();
110 task_runners.GetUITaskRunner()->PostTask([&]() { latch.Signal(); });
111 latch.Wait();
112 }
113}
114
116 ->Unit(benchmark::kMicrosecond);
117
118BENCHMARK(BM_PathVolatilityTracker)->Unit(benchmark::kMillisecond);
119
120} // namespace flutter
Definition: SkPath.h:59
static DartVMRef Create(const Settings &settings, fml::RefPtr< const DartSnapshot > vm_snapshot=nullptr, fml::RefPtr< const DartSnapshot > isolate_snapshot=nullptr)
fml::RefPtr< fml::TaskRunner > GetUITaskRunner() const
Definition: task_runners.cc:34
void Track(const std::shared_ptr< TrackedPath > &path)
virtual Settings CreateSettingsForFixture()
Definition: dart_fixture.cc:30
virtual void PostTask(const fml::closure &task) override
Definition: task_runner.cc:24
struct _Dart_Handle * Dart_Handle
Definition: dart_api.h:258
DART_EXPORT Dart_Handle Dart_NewStringFromCString(const char *str)
DART_EXPORT DART_WARN_UNUSED_RESULT Dart_Handle Dart_GetField(Dart_Handle container, Dart_Handle name)
DART_EXPORT Dart_Handle Dart_RootLibrary(void)
@ kRaster
Suitable for thread which raster data.
Definition: embedder.h:266
AtkStateType state
#define FML_CHECK(condition)
Definition: logging.h:85
Win32Message message
std::string GetDefaultKernelFilePath()
Returns the default path to kernel_blob.bin. This file is within the directory returned by GetFixture...
Definition: testing.cc:19
std::unique_ptr< AutoIsolateShutdown > RunDartCodeInIsolate(DartVMRef &vm_ref, const Settings &settings, const TaskRunners &task_runners, std::string entrypoint, const std::vector< std::string > &args, const std::string &kernel_file_path, fml::WeakPtr< IOManager > io_manager, std::shared_ptr< VolatilePathTracker > volatile_path_tracker, std::unique_ptr< PlatformConfiguration > platform_configuration)
BENCHMARK(BM_PathVolatilityTracker) -> Unit(benchmark::kMillisecond)
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir path
Definition: switches.h:57
static void BM_PathVolatilityTracker(benchmark::State &state)
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot data
Definition: switches.h:41
static void BM_PlatformMessageResponseDartComplete(benchmark::State &state)
std::function< void()> closure
Definition: closure.h:14
The collection of all the threads used by the engine.
Definition: thread_host.h:21
std::unique_ptr< fml::Thread > io_thread
Definition: thread_host.h:86
std::unique_ptr< fml::Thread > platform_thread
Definition: thread_host.h:83
std::unique_ptr< fml::Thread > raster_thread
Definition: thread_host.h:85
std::unique_ptr< fml::Thread > ui_thread
Definition: thread_host.h:84