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 <cassert>
6#include <string>
7#include <thread>
8#include <vector>
9#include "flutter/benchmarking/benchmarking.h"
10#include "flutter/fml/message_loop_task_queues.h"
11#include "flutter/fml/synchronization/count_down_latch.h"
12
13namespace fml {
14namespace benchmarking {
15
16static void BM_RegisterAndGetTasks(benchmark::State& state) { // NOLINT
17 while (state.KeepRunning()) {
18 auto task_queue = fml::MessageLoopTaskQueues::GetInstance();
19
20 const int num_task_queues = 10;
21 const int num_tasks_per_queue = 100;
22 const fml::TimePoint past = fml::TimePoint::Now();
23
24 for (int i = 0; i < num_task_queues; i++) {
25 task_queue->CreateTaskQueue();
26 }
27
28 std::vector<std::thread> threads;
29
30 CountDownLatch tasks_registered(num_task_queues);
31 CountDownLatch tasks_done(num_task_queues);
32
33 for (int i = 0; i < num_task_queues; i++) {
34 threads.emplace_back([task_runner_id = i, &task_queue, past, &tasks_done,
35 &tasks_registered]() {
36 for (int j = 0; j < num_tasks_per_queue; j++) {
37 task_queue->RegisterTask(
38 TaskQueueId(task_runner_id), [] {}, past);
39 }
40 tasks_registered.CountDown();
41 tasks_registered.Wait();
42 std::vector<fml::closure> invocations;
43 task_queue->GetTasksToRunNow(TaskQueueId(task_runner_id),
44 fml::FlushType::kAll, invocations);
45 assert(invocations.size() == num_tasks_per_queue);
46 tasks_done.CountDown();
47 });
48 }
49
50 tasks_done.Wait();
51
52 for (auto& thread : threads) {
53 thread.join();
54 }
55 }
56}
57
58BENCHMARK(BM_RegisterAndGetTasks);
59
60} // namespace benchmarking
61} // namespace fml
62