This repository was archived by the owner on Feb 25, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6k
Add benchmarks to measure dart -> native time #28492
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| // Copyright 2013 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. | ||
|
|
||
| #include "flutter/shell/common/shell.h" | ||
|
|
||
| #include "flutter/benchmarking/benchmarking.h" | ||
| #include "flutter/shell/common/thread_host.h" | ||
| #include "flutter/testing/dart_fixture.h" | ||
| #include "flutter/testing/dart_isolate_runner.h" | ||
| #include "flutter/testing/testing.h" | ||
| #include "fml/synchronization/count_down_latch.h" | ||
| #include "runtime/dart_vm_lifecycle.h" | ||
|
|
||
| namespace flutter::testing { | ||
|
|
||
| class DartNativeBenchmarks : public DartFixture, public benchmark::Fixture { | ||
| public: | ||
| DartNativeBenchmarks() : DartFixture() {} | ||
|
|
||
| void SetUp(const ::benchmark::State& state) {} | ||
|
|
||
| void TearDown(const ::benchmark::State& state) {} | ||
|
|
||
| private: | ||
| FML_DISALLOW_COPY_AND_ASSIGN(DartNativeBenchmarks); | ||
| }; | ||
|
|
||
| BENCHMARK_F(DartNativeBenchmarks, TimeToFirstNativeMessageFromIsolateInNewVM) | ||
| (benchmark::State& st) { | ||
| while (st.KeepRunning()) { | ||
| fml::AutoResetWaitableEvent latch; | ||
| st.PauseTiming(); | ||
| ASSERT_FALSE(DartVMRef::IsInstanceRunning()); | ||
| AddNativeCallback("NotifyNative", | ||
| CREATE_NATIVE_ENTRY(([&latch](Dart_NativeArguments args) { | ||
| latch.Signal(); | ||
| }))); | ||
|
|
||
| const auto settings = CreateSettingsForFixture(); | ||
| DartVMRef vm_ref = DartVMRef::Create(settings); | ||
|
|
||
| ThreadHost thread_host("io.flutter.test.DartNativeBenchmarks.", | ||
| ThreadHost::Type::Platform | ThreadHost::Type::IO | | ||
| ThreadHost::Type::UI); | ||
| TaskRunners task_runners( | ||
| "test", | ||
| thread_host.platform_thread->GetTaskRunner(), // platform | ||
| thread_host.platform_thread->GetTaskRunner(), // raster | ||
| thread_host.ui_thread->GetTaskRunner(), // ui | ||
| thread_host.io_thread->GetTaskRunner() // io | ||
| ); | ||
|
|
||
| { | ||
| st.ResumeTiming(); | ||
| auto isolate = | ||
| RunDartCodeInIsolate(vm_ref, settings, task_runners, "notifyNative", | ||
| {}, GetDefaultKernelFilePath()); | ||
| ASSERT_TRUE(isolate); | ||
| ASSERT_EQ(isolate->get()->GetPhase(), DartIsolate::Phase::Running); | ||
| latch.Wait(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| BENCHMARK_F(DartNativeBenchmarks, MultipleDartToNativeMessages) | ||
| (benchmark::State& st) { | ||
| while (st.KeepRunning()) { | ||
| fml::CountDownLatch latch(1000); | ||
| st.PauseTiming(); | ||
| ASSERT_FALSE(DartVMRef::IsInstanceRunning()); | ||
| AddNativeCallback("NotifyNative", | ||
| CREATE_NATIVE_ENTRY(([&latch](Dart_NativeArguments args) { | ||
| latch.CountDown(); | ||
| }))); | ||
|
|
||
| const auto settings = CreateSettingsForFixture(); | ||
| DartVMRef vm_ref = DartVMRef::Create(settings); | ||
|
|
||
| ThreadHost thread_host("io.flutter.test.DartNativeBenchmarks.", | ||
| ThreadHost::Type::Platform | ThreadHost::Type::IO | | ||
| ThreadHost::Type::UI); | ||
| TaskRunners task_runners( | ||
| "test", | ||
| thread_host.platform_thread->GetTaskRunner(), // platform | ||
| thread_host.platform_thread->GetTaskRunner(), // raster | ||
| thread_host.ui_thread->GetTaskRunner(), // ui | ||
| thread_host.io_thread->GetTaskRunner() // io | ||
| ); | ||
|
|
||
| { | ||
| st.ResumeTiming(); | ||
| auto isolate = RunDartCodeInIsolate(vm_ref, settings, task_runners, | ||
| "thousandCallsToNative", {}, | ||
| GetDefaultKernelFilePath()); | ||
| ASSERT_TRUE(isolate); | ||
| ASSERT_EQ(isolate->get()->GetPhase(), DartIsolate::Phase::Running); | ||
| latch.Wait(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| } // namespace flutter::testing |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| // Copyright 2013 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. | ||
|
|
||
| #include "flutter/testing/dart_fixture.h" | ||
|
|
||
| namespace flutter::testing { | ||
|
|
||
| DartFixture::DartFixture() | ||
| : DartFixture("kernel_blob.bin", | ||
| kDefaultAOTAppELFFileName, | ||
| kDefaultAOTAppELFSplitFileName) {} | ||
|
|
||
| DartFixture::DartFixture(std::string kernel_filename, | ||
| std::string elf_filename, | ||
| std::string elf_split_filename) | ||
| : native_resolver_(std::make_shared<TestDartNativeResolver>()), | ||
| split_aot_symbols_( | ||
| LoadELFSplitSymbolFromFixturesIfNeccessary(elf_split_filename)), | ||
| kernel_filename_(kernel_filename), | ||
| assets_dir_(fml::OpenDirectory(GetFixturesPath(), | ||
| false, | ||
| fml::FilePermission::kRead)), | ||
| aot_symbols_(LoadELFSymbolFromFixturesIfNeccessary(elf_filename)) {} | ||
|
|
||
| Settings DartFixture::CreateSettingsForFixture() { | ||
| Settings settings; | ||
| settings.leak_vm = false; | ||
| settings.task_observer_add = [](intptr_t, fml::closure) {}; | ||
| settings.task_observer_remove = [](intptr_t) {}; | ||
| settings.isolate_create_callback = [this]() { | ||
| native_resolver_->SetNativeResolverForIsolate(); | ||
| }; | ||
| settings.enable_observatory = false; | ||
| SetSnapshotsAndAssets(settings); | ||
| return settings; | ||
| } | ||
|
|
||
| void DartFixture::SetSnapshotsAndAssets(Settings& settings) { | ||
| if (!assets_dir_.is_valid()) { | ||
| return; | ||
| } | ||
|
|
||
| settings.assets_dir = assets_dir_.get(); | ||
|
|
||
| // In JIT execution, all snapshots are present within the binary itself and | ||
| // don't need to be explicitly supplied by the embedder. In AOT, these | ||
| // snapshots will be present in the application AOT dylib. | ||
| if (DartVM::IsRunningPrecompiledCode()) { | ||
| FML_CHECK(PrepareSettingsForAOTWithSymbols(settings, aot_symbols_)); | ||
| } else { | ||
| settings.application_kernels = [this]() -> Mappings { | ||
| std::vector<std::unique_ptr<const fml::Mapping>> kernel_mappings; | ||
| auto kernel_mapping = | ||
| fml::FileMapping::CreateReadOnly(assets_dir_, kernel_filename_); | ||
| if (!kernel_mapping || !kernel_mapping->IsValid()) { | ||
| FML_LOG(ERROR) << "Could not find kernel blob for test fixture not " | ||
| "running in precompiled mode."; | ||
| return kernel_mappings; | ||
| } | ||
| kernel_mappings.emplace_back(std::move(kernel_mapping)); | ||
| return kernel_mappings; | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| void DartFixture::AddNativeCallback(std::string name, | ||
| Dart_NativeFunction callback) { | ||
| native_resolver_->AddNativeCallback(std::move(name), callback); | ||
| } | ||
|
|
||
| } // namespace flutter::testing |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| // Copyright 2013 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. | ||
|
|
||
| #ifndef FLUTTER_TESTING_DART_FIXTURE_H_ | ||
| #define FLUTTER_TESTING_DART_FIXTURE_H_ | ||
|
|
||
| #include <memory> | ||
|
|
||
| #include "flutter/common/settings.h" | ||
| #include "flutter/runtime/dart_vm.h" | ||
| #include "flutter/testing/elf_loader.h" | ||
| #include "flutter/testing/test_dart_native_resolver.h" | ||
| #include "flutter/testing/testing.h" | ||
| #include "flutter/testing/thread_test.h" | ||
|
|
||
| namespace flutter::testing { | ||
|
|
||
| class DartFixture { | ||
| public: | ||
| // Uses the default filenames from the fixtures generator. | ||
| DartFixture(); | ||
|
|
||
| // Allows to customize the kernel, ELF and split ELF filenames. | ||
| DartFixture(std::string kernel_filename, | ||
| std::string elf_filename, | ||
| std::string elf_split_filename); | ||
|
|
||
| virtual Settings CreateSettingsForFixture(); | ||
|
|
||
| void AddNativeCallback(std::string name, Dart_NativeFunction callback); | ||
|
|
||
| protected: | ||
| void SetSnapshotsAndAssets(Settings& settings); | ||
|
|
||
| std::shared_ptr<TestDartNativeResolver> native_resolver_; | ||
| ELFAOTSymbols split_aot_symbols_; | ||
| std::string kernel_filename_; | ||
| std::string elf_filename_; | ||
| fml::UniqueFD assets_dir_; | ||
| ELFAOTSymbols aot_symbols_; | ||
|
|
||
| private: | ||
| FML_DISALLOW_COPY_AND_ASSIGN(DartFixture); | ||
| }; | ||
|
|
||
| } // namespace flutter::testing | ||
|
|
||
| #endif // FLUTTER_TESTING_DART_FIXTURE_H_ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is just removal of unrelated log spam right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup