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/fml/logging.h"
7 #include "flutter/runtime/dart_vm.h"
8 #include "flutter/shell/common/shell.h"
9 #include "flutter/shell/common/thread_host.h"
10 #include "flutter/testing/testing.h"
11
12 namespace flutter {
13
StartupAndShutdownShell(benchmark::State & state,bool measure_startup,bool measure_shutdown)14 static void StartupAndShutdownShell(benchmark::State& state,
15 bool measure_startup,
16 bool measure_shutdown) {
17 auto assets_dir = fml::OpenDirectory(testing::GetFixturesPath(), false,
18 fml::FilePermission::kRead);
19 std::unique_ptr<Shell> shell;
20 std::unique_ptr<ThreadHost> thread_host;
21 {
22 benchmarking::ScopedPauseTiming pause(state, !measure_startup);
23 Settings settings = {};
24 settings.task_observer_add = [](intptr_t, fml::closure) {};
25 settings.task_observer_remove = [](intptr_t) {};
26
27 if (DartVM::IsRunningPrecompiledCode()) {
28 settings.vm_snapshot_data = [&]() {
29 return fml::FileMapping::CreateReadOnly(assets_dir, "vm_snapshot_data");
30 };
31
32 settings.isolate_snapshot_data = [&]() {
33 return fml::FileMapping::CreateReadOnly(assets_dir,
34 "isolate_snapshot_data");
35 };
36
37 settings.vm_snapshot_instr = [&]() {
38 return fml::FileMapping::CreateReadExecute(assets_dir,
39 "vm_snapshot_instr");
40 };
41
42 settings.isolate_snapshot_instr = [&]() {
43 return fml::FileMapping::CreateReadExecute(assets_dir,
44 "isolate_snapshot_instr");
45 };
46
47 } else {
48 settings.application_kernels = [&]() {
49 std::vector<std::unique_ptr<const fml::Mapping>> kernel_mappings;
50 kernel_mappings.emplace_back(
51 fml::FileMapping::CreateReadOnly(assets_dir, "kernel_blob.bin"));
52 return kernel_mappings;
53 };
54 }
55
56 thread_host = std::make_unique<ThreadHost>(
57 "io.flutter.bench.", ThreadHost::Type::Platform |
58 ThreadHost::Type::GPU | ThreadHost::Type::IO |
59 ThreadHost::Type::UI);
60
61 TaskRunners task_runners("test",
62 thread_host->platform_thread->GetTaskRunner(),
63 thread_host->gpu_thread->GetTaskRunner(),
64 thread_host->ui_thread->GetTaskRunner(),
65 thread_host->io_thread->GetTaskRunner());
66
67 shell = Shell::Create(
68 std::move(task_runners), settings,
69 [](Shell& shell) {
70 return std::make_unique<PlatformView>(shell, shell.GetTaskRunners());
71 },
72 [](Shell& shell) {
73 return std::make_unique<Rasterizer>(shell, shell.GetTaskRunners());
74 });
75 }
76
77 FML_CHECK(shell);
78
79 {
80 benchmarking::ScopedPauseTiming pause(state, !measure_shutdown);
81 shell.reset(); // Shutdown is synchronous.
82 thread_host.reset();
83 }
84
85 FML_CHECK(!shell);
86 }
87
BM_ShellInitialization(benchmark::State & state)88 static void BM_ShellInitialization(benchmark::State& state) {
89 while (state.KeepRunning()) {
90 StartupAndShutdownShell(state, true, false);
91 }
92 }
93
94 BENCHMARK(BM_ShellInitialization);
95
BM_ShellShutdown(benchmark::State & state)96 static void BM_ShellShutdown(benchmark::State& state) {
97 while (state.KeepRunning()) {
98 StartupAndShutdownShell(state, false, true);
99 }
100 }
101
102 BENCHMARK(BM_ShellShutdown);
103
BM_ShellInitializationAndShutdown(benchmark::State & state)104 static void BM_ShellInitializationAndShutdown(benchmark::State& state) {
105 while (state.KeepRunning()) {
106 StartupAndShutdownShell(state, true, true);
107 }
108 }
109
110 BENCHMARK(BM_ShellInitializationAndShutdown);
111
112 } // namespace flutter
113