|
| 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/shell/common/shell.h" |
| 6 | + |
| 7 | +#include "flutter/benchmarking/benchmarking.h" |
| 8 | +#include "flutter/shell/common/thread_host.h" |
| 9 | +#include "flutter/testing/dart_fixture.h" |
| 10 | +#include "flutter/testing/dart_isolate_runner.h" |
| 11 | +#include "flutter/testing/testing.h" |
| 12 | +#include "fml/synchronization/count_down_latch.h" |
| 13 | +#include "runtime/dart_vm_lifecycle.h" |
| 14 | + |
| 15 | +namespace flutter::testing { |
| 16 | + |
| 17 | +class DartNativeBenchmarks : public DartFixture, public benchmark::Fixture { |
| 18 | + public: |
| 19 | + DartNativeBenchmarks() : DartFixture() {} |
| 20 | + |
| 21 | + void SetUp(const ::benchmark::State& state) {} |
| 22 | + |
| 23 | + void TearDown(const ::benchmark::State& state) {} |
| 24 | + |
| 25 | + private: |
| 26 | + FML_DISALLOW_COPY_AND_ASSIGN(DartNativeBenchmarks); |
| 27 | +}; |
| 28 | + |
| 29 | +BENCHMARK_F(DartNativeBenchmarks, TimeToFirstNativeMessageFromIsolateInNewVM) |
| 30 | +(benchmark::State& st) { |
| 31 | + while (st.KeepRunning()) { |
| 32 | + fml::AutoResetWaitableEvent latch; |
| 33 | + st.PauseTiming(); |
| 34 | + ASSERT_FALSE(DartVMRef::IsInstanceRunning()); |
| 35 | + AddNativeCallback("NotifyNative", |
| 36 | + CREATE_NATIVE_ENTRY(([&latch](Dart_NativeArguments args) { |
| 37 | + latch.Signal(); |
| 38 | + }))); |
| 39 | + |
| 40 | + const auto settings = CreateSettingsForFixture(); |
| 41 | + DartVMRef vm_ref = DartVMRef::Create(settings); |
| 42 | + |
| 43 | + ThreadHost thread_host("io.flutter.test.DartNativeBenchmarks.", |
| 44 | + ThreadHost::Type::Platform | ThreadHost::Type::IO | |
| 45 | + ThreadHost::Type::UI); |
| 46 | + TaskRunners task_runners( |
| 47 | + "test", |
| 48 | + thread_host.platform_thread->GetTaskRunner(), // platform |
| 49 | + thread_host.platform_thread->GetTaskRunner(), // raster |
| 50 | + thread_host.ui_thread->GetTaskRunner(), // ui |
| 51 | + thread_host.io_thread->GetTaskRunner() // io |
| 52 | + ); |
| 53 | + |
| 54 | + { |
| 55 | + st.ResumeTiming(); |
| 56 | + auto isolate = |
| 57 | + RunDartCodeInIsolate(vm_ref, settings, task_runners, "notifyNative", |
| 58 | + {}, GetDefaultKernelFilePath()); |
| 59 | + ASSERT_TRUE(isolate); |
| 60 | + ASSERT_EQ(isolate->get()->GetPhase(), DartIsolate::Phase::Running); |
| 61 | + latch.Wait(); |
| 62 | + } |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +BENCHMARK_F(DartNativeBenchmarks, MultipleDartToNativeMessages) |
| 67 | +(benchmark::State& st) { |
| 68 | + while (st.KeepRunning()) { |
| 69 | + fml::CountDownLatch latch(1000); |
| 70 | + st.PauseTiming(); |
| 71 | + ASSERT_FALSE(DartVMRef::IsInstanceRunning()); |
| 72 | + AddNativeCallback("NotifyNative", |
| 73 | + CREATE_NATIVE_ENTRY(([&latch](Dart_NativeArguments args) { |
| 74 | + latch.CountDown(); |
| 75 | + }))); |
| 76 | + |
| 77 | + const auto settings = CreateSettingsForFixture(); |
| 78 | + DartVMRef vm_ref = DartVMRef::Create(settings); |
| 79 | + |
| 80 | + ThreadHost thread_host("io.flutter.test.DartNativeBenchmarks.", |
| 81 | + ThreadHost::Type::Platform | ThreadHost::Type::IO | |
| 82 | + ThreadHost::Type::UI); |
| 83 | + TaskRunners task_runners( |
| 84 | + "test", |
| 85 | + thread_host.platform_thread->GetTaskRunner(), // platform |
| 86 | + thread_host.platform_thread->GetTaskRunner(), // raster |
| 87 | + thread_host.ui_thread->GetTaskRunner(), // ui |
| 88 | + thread_host.io_thread->GetTaskRunner() // io |
| 89 | + ); |
| 90 | + |
| 91 | + { |
| 92 | + st.ResumeTiming(); |
| 93 | + auto isolate = RunDartCodeInIsolate(vm_ref, settings, task_runners, |
| 94 | + "thousandCallsToNative", {}, |
| 95 | + GetDefaultKernelFilePath()); |
| 96 | + ASSERT_TRUE(isolate); |
| 97 | + ASSERT_EQ(isolate->get()->GetPhase(), DartIsolate::Phase::Running); |
| 98 | + latch.Wait(); |
| 99 | + } |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +} // namespace flutter::testing |
0 commit comments