blob: 30c239ff32f08c3189d8d39f91508f9ac5706753 [file] [log] [blame]
Ben Clayton55890e12020-01-31 14:07:21 +00001// Copyright 2020 The SwiftShader Authors. All Rights Reserved.
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// http://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 "Coroutine.hpp"
16#include "Reactor.hpp"
17
18#include "benchmark/benchmark.h"
19
20BENCHMARK_MAIN();
21
22class Coroutines : public benchmark::Fixture
23{
24public:
25 void SetUp(const ::benchmark::State &state) {}
26
27 void TearDown(const ::benchmark::State &state) {}
28};
29
Ben Clayton534d12c2020-02-06 17:28:07 +000030BENCHMARK_DEFINE_F(Coroutines, Fibonacci)
Ben Clayton55890e12020-01-31 14:07:21 +000031(benchmark::State &state)
32{
33 using namespace rr;
34
35 if(!Caps.CoroutinesSupported)
36 {
37 state.SkipWithError("Coroutines are not supported");
38 return;
39 }
40
41 Coroutine<int()> function;
42 {
43 Yield(Int(0));
44 Yield(Int(1));
45 Int current = 1;
46 Int next = 1;
47 While(true)
48 {
49 Yield(next);
50 auto tmp = current + next;
51 current = next;
52 next = tmp;
53 }
54 }
55
56 auto coroutine = function();
57
Ben Clayton534d12c2020-02-06 17:28:07 +000058 const auto iterations = state.range(0);
59
Ben Clayton55890e12020-01-31 14:07:21 +000060 int out = 0;
61 for(auto _ : state)
62 {
Ben Clayton534d12c2020-02-06 17:28:07 +000063 for(int64_t i = 0; i < iterations; i++)
64 {
65 coroutine->await(out);
66 }
Ben Clayton55890e12020-01-31 14:07:21 +000067 }
68}
Ben Clayton534d12c2020-02-06 17:28:07 +000069
70BENCHMARK_REGISTER_F(Coroutines, Fibonacci)->RangeMultiplier(8)->Range(1, 0x1000000)->ArgName("iterations");