1 // Copyright 2020 The Marl Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "marl_bench.h"
16 
17 #include "marl/waitgroup.h"
18 
19 #include "benchmark/benchmark.h"
20 
BENCHMARK_DEFINE_F(Schedule,Empty)21 BENCHMARK_DEFINE_F(Schedule, Empty)(benchmark::State& state) {
22   run(state, [&](int numTasks) {
23     for (auto _ : state) {
24       for (auto i = 0; i < numTasks; i++) {
25         marl::schedule([] {});
26       }
27     }
28   });
29 }
30 BENCHMARK_REGISTER_F(Schedule, Empty)->Apply(Schedule::args);
31 
BENCHMARK_DEFINE_F(Schedule,SomeWork)32 BENCHMARK_DEFINE_F(Schedule, SomeWork)
33 (benchmark::State& state) {
34   run(state, [&](int numTasks) {
35     for (auto _ : state) {
36       marl::WaitGroup wg;
37       wg.add(numTasks);
38       for (auto i = 0; i < numTasks; i++) {
39         marl::schedule([=] {
40           benchmark::DoNotOptimize(doSomeWork(i));
41           wg.done();
42         });
43       }
44       wg.wait();
45     }
46   });
47 }
48 BENCHMARK_REGISTER_F(Schedule, SomeWork)->Apply(Schedule::args);
49 
BENCHMARK_DEFINE_F(Schedule,SomeWorkWorkerAffinityOneOf)50 BENCHMARK_DEFINE_F(Schedule, SomeWorkWorkerAffinityOneOf)
51 (benchmark::State& state) {
52   marl::Scheduler::Config cfg;
53   cfg.setWorkerThreadAffinityPolicy(
54       marl::Thread::Affinity::Policy::oneOf(marl::Thread::Affinity::all()));
55   run(state, cfg, [&](int numTasks) {
56     for (auto _ : state) {
57       marl::WaitGroup wg;
58       wg.add(numTasks);
59       for (auto i = 0; i < numTasks; i++) {
60         marl::schedule([=] {
61           benchmark::DoNotOptimize(doSomeWork(i));
62           wg.done();
63         });
64       }
65       wg.wait();
66     }
67   });
68 }
69 BENCHMARK_REGISTER_F(Schedule, SomeWorkWorkerAffinityOneOf)
70     ->Apply(Schedule::args);
71