1fb299fa2Sopenharmony_ci/* 2fb299fa2Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd. 3fb299fa2Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4fb299fa2Sopenharmony_ci * you may not use this file except in compliance with the License. 5fb299fa2Sopenharmony_ci * You may obtain a copy of the License at 6fb299fa2Sopenharmony_ci * 7fb299fa2Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8fb299fa2Sopenharmony_ci * 9fb299fa2Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10fb299fa2Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11fb299fa2Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12fb299fa2Sopenharmony_ci * See the License for the specific language governing permissions and 13fb299fa2Sopenharmony_ci * limitations under the License. 14fb299fa2Sopenharmony_ci */ 15fb299fa2Sopenharmony_ci 16fb299fa2Sopenharmony_ci#include <benchmark/benchmark.h> 17fb299fa2Sopenharmony_ci#include <thread> 18fb299fa2Sopenharmony_ci#include "gtest/gtest.h" 19fb299fa2Sopenharmony_ci 20fb299fa2Sopenharmony_ci#include "ring_buffer.h" 21fb299fa2Sopenharmony_ci 22fb299fa2Sopenharmony_ciusing namespace testing::ext; 23fb299fa2Sopenharmony_ci 24fb299fa2Sopenharmony_cinamespace Updater { 25fb299fa2Sopenharmony_ci 26fb299fa2Sopenharmony_ciconstexpr uint32_t RING_MAX_LEN = 1024; 27fb299fa2Sopenharmony_ciconstexpr uint32_t BYTE_SIZE = 255; 28fb299fa2Sopenharmony_ci 29fb299fa2Sopenharmony_ciclass UpdaterBenchmarkTest : public benchmark::Fixture { 30fb299fa2Sopenharmony_cipublic: 31fb299fa2Sopenharmony_ci UpdaterBenchmarkTest() = default; 32fb299fa2Sopenharmony_ci ~UpdaterBenchmarkTest() override = default; 33fb299fa2Sopenharmony_ci void SetUp(const ::benchmark::State &state) override 34fb299fa2Sopenharmony_ci {} 35fb299fa2Sopenharmony_ci void TearDown(const ::benchmark::State &state) override 36fb299fa2Sopenharmony_ci {} 37fb299fa2Sopenharmony_ci}; 38fb299fa2Sopenharmony_ci 39fb299fa2Sopenharmony_civoid ProducerTask(RingBuffer *ringBuffer) 40fb299fa2Sopenharmony_ci{ 41fb299fa2Sopenharmony_ci for (uint32_t i = 0; i < RING_MAX_LEN; i++) { 42fb299fa2Sopenharmony_ci uint8_t buf[4] {}; // 4: test buffer size 43fb299fa2Sopenharmony_ci buf[0] = i % BYTE_SIZE; 44fb299fa2Sopenharmony_ci buf[1] = i / BYTE_SIZE; 45fb299fa2Sopenharmony_ci ringBuffer->Push(buf, sizeof(buf)); 46fb299fa2Sopenharmony_ci } 47fb299fa2Sopenharmony_ci} 48fb299fa2Sopenharmony_ci 49fb299fa2Sopenharmony_civoid ConsumerTask(RingBuffer *ringBuffer) 50fb299fa2Sopenharmony_ci{ 51fb299fa2Sopenharmony_ci for (uint32_t i = 0; i < RING_MAX_LEN; i++) { 52fb299fa2Sopenharmony_ci uint8_t buf[4] {}; // 4: test buffer size 53fb299fa2Sopenharmony_ci uint32_t len = 0; 54fb299fa2Sopenharmony_ci ringBuffer->Pop(buf, sizeof(buf), len); 55fb299fa2Sopenharmony_ci } 56fb299fa2Sopenharmony_ci} 57fb299fa2Sopenharmony_ci 58fb299fa2Sopenharmony_civoid TestRingBuffer() 59fb299fa2Sopenharmony_ci{ 60fb299fa2Sopenharmony_ci RingBuffer ringBuffer; 61fb299fa2Sopenharmony_ci bool ret = ringBuffer.Init(1024, 8); 62fb299fa2Sopenharmony_ci EXPECT_TRUE(ret); 63fb299fa2Sopenharmony_ci std::thread consumer(ConsumerTask, &ringBuffer); 64fb299fa2Sopenharmony_ci std::thread producer(ProducerTask, &ringBuffer); 65fb299fa2Sopenharmony_ci consumer.join(); 66fb299fa2Sopenharmony_ci producer.join(); 67fb299fa2Sopenharmony_ci} 68fb299fa2Sopenharmony_ci 69fb299fa2Sopenharmony_ciBENCHMARK_F(UpdaterBenchmarkTest, TestRingBuffer)(benchmark::State &state) 70fb299fa2Sopenharmony_ci{ 71fb299fa2Sopenharmony_ci for (auto _ : state) { 72fb299fa2Sopenharmony_ci TestRingBuffer(); 73fb299fa2Sopenharmony_ci } 74fb299fa2Sopenharmony_ci} 75fb299fa2Sopenharmony_ci 76fb299fa2Sopenharmony_ciBENCHMARK_REGISTER_F(UpdaterBenchmarkTest, TestRingBuffer)-> 77fb299fa2Sopenharmony_ci Iterations(5)->Repetitions(3)->ReportAggregatesOnly(); 78fb299fa2Sopenharmony_ci 79fb299fa2Sopenharmony_ci} // namespace Updater 80fb299fa2Sopenharmony_ci 81fb299fa2Sopenharmony_ci// Run the benchmark 82fb299fa2Sopenharmony_ciBENCHMARK_MAIN(); 83