Ben Clayton | 55890e1 | 2020-01-31 14:07:21 +0000 | [diff] [blame] | 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: |
| 25 | void SetUp(const ::benchmark::State &state) {} |
| 26 | |
| 27 | void TearDown(const ::benchmark::State &state) {} |
| 28 | }; |
| 29 | |
Ben Clayton | 534d12c | 2020-02-06 17:28:07 +0000 | [diff] [blame] | 30 | BENCHMARK_DEFINE_F(Coroutines, Fibonacci) |
Ben Clayton | 55890e1 | 2020-01-31 14:07:21 +0000 | [diff] [blame] | 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 | |
Ben Clayton | 534d12c | 2020-02-06 17:28:07 +0000 | [diff] [blame] | 58 | const auto iterations = state.range(0); |
| 59 | |
Ben Clayton | 55890e1 | 2020-01-31 14:07:21 +0000 | [diff] [blame] | 60 | int out = 0; |
| 61 | for(auto _ : state) |
| 62 | { |
Ben Clayton | 534d12c | 2020-02-06 17:28:07 +0000 | [diff] [blame] | 63 | for(int64_t i = 0; i < iterations; i++) |
| 64 | { |
| 65 | coroutine->await(out); |
| 66 | } |
Ben Clayton | 55890e1 | 2020-01-31 14:07:21 +0000 | [diff] [blame] | 67 | } |
| 68 | } |
Ben Clayton | 534d12c | 2020-02-06 17:28:07 +0000 | [diff] [blame] | 69 | |
| 70 | BENCHMARK_REGISTER_F(Coroutines, Fibonacci)->RangeMultiplier(8)->Range(1, 0x1000000)->ArgName("iterations"); |