1 /* 2 * Copyright (c) 2021 Huawei Device Co., Ltd. 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 16 #include <benchmark/benchmark.h> 17 #include <string> 18 #include <vector> 19 20 using namespace std; 21 22 namespace { 23 /** 24 * @tc.name: BenchmarkTestExample 25 * @tc.desc: Testcase for testing 'SimpleExample' function. 26 * @tc.type: FUNC 27 * @tc.require: Issue Number 28 */ SimpleExample()29 size_t SimpleExample() 30 { 31 string str = "benchmark test"; 32 return str.size(); 33 } 34 BenchmarkTestExample(benchmark::State & state)35 static void BenchmarkTestExample(benchmark::State &state) 36 { 37 for (auto _ : state) { 38 /* @tc.steps: step1.call SimpleExample in loop */ 39 SimpleExample(); 40 } 41 } 42 43 /* Register the function as a benchmark */ 44 BENCHMARK(BenchmarkTestExample); 45 /* Register benchmark and explicitly set the fix iterations */ 46 BENCHMARK(BenchmarkTestExample)->Iterations(1000); 47 48 /** 49 * @tc.name: BenchmarkTestVectorOperator 50 * @tc.desc: Testcase for testing "AccessVectorElementByOperator" 51 * function. 52 * @tc.type: FUNC 53 * @tc.require: Issue Number 54 */ AccessVectorElementByOperator()55 void AccessVectorElementByOperator() 56 { 57 constexpr int testLen = 5; 58 std::vector<int> testVec(testLen, 0); 59 for (int i = 0; i < testLen; i++) { 60 testVec[i] = i * i; 61 } 62 } 63 BenchmarkTestVectorOperator(benchmark::State & state)64 static void BenchmarkTestVectorOperator(benchmark::State &state) 65 { 66 for (auto _ : state) { 67 /* @tc.steps: step1.call AccessVectorElementByOperator in loop */ 68 AccessVectorElementByOperator(); 69 } 70 } 71 72 /* 73 * Register the function as a benchmark, set iterations repetitions. 74 * And set "ReportAggregatesOnly", it will display the statistics Mean, 75 * Median and Standard Deviation of Repeated Benchmarks. 76 */ 77 BENCHMARK(BenchmarkTestVectorOperator)->Iterations(1000)->Repetitions(3)-> 78 ReportAggregatesOnly(); 79 80 /** 81 * @tc.name: BenchmarkTestVectorAt 82 * @tc.desc: Testcase for testing "AccessVectorElementByAt" 83 * function. 84 * @tc.type: FUNC 85 * @tc.require: Issue Number 86 */ AccessVectorElementByAt()87 void AccessVectorElementByAt() 88 { 89 constexpr int testLen = 5; 90 std::vector<int> testVec(testLen, 0); 91 for (int i = 0; i < testLen; i++) { 92 testVec.at(i) = i * i; 93 } 94 } 95 BenchmarkTestVectorAt(benchmark::State & state)96 static void BenchmarkTestVectorAt(benchmark::State &state) 97 { 98 for (auto _ : state) { 99 /* @tc.steps: step1.call AccessVectorElementByAt in loop */ 100 AccessVectorElementByAt(); 101 } 102 } 103 104 BENCHMARK(BenchmarkTestVectorAt)->Iterations(1000)->Repetitions(3)-> 105 ReportAggregatesOnly(); 106 107 /** 108 * @tc.name: CalculatedAreaTestCase 109 * @tc.desc: Define a testcase that accesses a class member 110 * variable. 111 * @tc.type: FUNC 112 * @tc.require: Issue Number 113 */ 114 class BenchmarkDemoTest : public benchmark::Fixture { 115 public: SetUp(const::benchmark::State & state)116 void SetUp(const ::benchmark::State &state) 117 { 118 /* @tc.setup: width and height assigned */ 119 phoneWidth_ = 1080; /* 1080 is default width */ 120 phoneHeight_ = 2244; /* 2244 is default height */ 121 } 122 TearDown(const::benchmark::State & state)123 void TearDown(const ::benchmark::State &state) 124 { 125 } 126 127 int phoneWidth_; 128 int phoneHeight_; 129 }; 130 BENCHMARK_F(BenchmarkDemoTest,CalculatedAreaTestCase)131 BENCHMARK_F(BenchmarkDemoTest, CalculatedAreaTestCase)( 132 benchmark::State &st) 133 { 134 long int area = 0; 135 for (auto _ : st) { 136 /* @tc.steps: step1.calculate area */ 137 area = phoneWidth_ * phoneHeight_; 138 } 139 } 140 141 BENCHMARK_REGISTER_F(BenchmarkDemoTest, CalculatedAreaTestCase); 142 } 143 144 // Run the benchmark 145 BENCHMARK_MAIN(); 146