1 // 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 
20 BENCHMARK_MAIN();
21 
22 class Coroutines : public benchmark::Fixture
23 {
24 public:
SetUp(const::benchmark::State & state)25 	void SetUp(const ::benchmark::State &state) {}
26 
TearDown(const::benchmark::State & state)27 	void TearDown(const ::benchmark::State &state) {}
28 };
29 
BENCHMARK_DEFINE_F(Coroutines,Fibonacci)30 BENCHMARK_DEFINE_F(Coroutines, Fibonacci)
31 (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 
58 	const auto iterations = state.range(0);
59 
60 	int out = 0;
61 	for(auto _ : state)
62 	{
63 		for(int64_t i = 0; i < iterations; i++)
64 		{
65 			coroutine->await(out);
66 		}
67 	}
68 }
69 
70 BENCHMARK_REGISTER_F(Coroutines, Fibonacci)->RangeMultiplier(8)->Range(1, 0x1000000)->ArgName("iterations");
71