1094332d3Sopenharmony_ci/* 2094332d3Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd. 3094332d3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4094332d3Sopenharmony_ci * you may not use this file except in compliance with the License. 5094332d3Sopenharmony_ci * You may obtain a copy of the License at 6094332d3Sopenharmony_ci * 7094332d3Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8094332d3Sopenharmony_ci * 9094332d3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10094332d3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11094332d3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12094332d3Sopenharmony_ci * See the License for the specific language governing permissions and 13094332d3Sopenharmony_ci * limitations under the License. 14094332d3Sopenharmony_ci */ 15094332d3Sopenharmony_ci 16094332d3Sopenharmony_ci#include <benchmark/benchmark.h> 17094332d3Sopenharmony_ci#include <cmath> 18094332d3Sopenharmony_ci#include <cstdio> 19094332d3Sopenharmony_ci#include <gtest/gtest.h> 20094332d3Sopenharmony_ci#include <securec.h> 21094332d3Sopenharmony_ci#include <string> 22094332d3Sopenharmony_ci#include <vector> 23094332d3Sopenharmony_ci#include "hdf_base.h" 24094332d3Sopenharmony_ci#include "light_type.h" 25094332d3Sopenharmony_ci#include "osal_time.h" 26094332d3Sopenharmony_ci#include "v1_0/ilight_interface.h" 27094332d3Sopenharmony_ci 28094332d3Sopenharmony_ciusing namespace OHOS::HDI::Light::V1_0; 29094332d3Sopenharmony_ciusing namespace testing::ext; 30094332d3Sopenharmony_ciusing namespace std; 31094332d3Sopenharmony_ci 32094332d3Sopenharmony_cinamespace { 33094332d3Sopenharmony_ci constexpr int32_t ITERATION_FREQUENCY = 100; 34094332d3Sopenharmony_ci constexpr int32_t REPETITION_FREQUENCY = 3; 35094332d3Sopenharmony_ci constexpr int32_t COLORVALUE_RED = 255; 36094332d3Sopenharmony_ci constexpr int32_t COLORVALUE_GREEN = 0; 37094332d3Sopenharmony_ci constexpr int32_t COLORVALUE_BLUE = 0; 38094332d3Sopenharmony_ci constexpr uint32_t SLEEP_TIME = 3; 39094332d3Sopenharmony_ci constexpr int32_t MIN_LIGHT_ID = HDF_LIGHT_ID_BATTERY; 40094332d3Sopenharmony_ci constexpr int32_t MAX_LIGHT_ID = HDF_LIGHT_ID_ATTENTION; 41094332d3Sopenharmony_ci constexpr int32_t ON_TIME = 500; 42094332d3Sopenharmony_ci constexpr int32_t OFF_TIME = 500; 43094332d3Sopenharmony_ci sptr<ILightInterface> g_lightInterface = nullptr; 44094332d3Sopenharmony_ci 45094332d3Sopenharmony_ciclass LightBenchmarkTest : public benchmark::Fixture { 46094332d3Sopenharmony_cipublic: 47094332d3Sopenharmony_ci void SetUp(const ::benchmark::State &state); 48094332d3Sopenharmony_ci void TearDown(const ::benchmark::State &state); 49094332d3Sopenharmony_ci}; 50094332d3Sopenharmony_ci 51094332d3Sopenharmony_civoid LightBenchmarkTest::SetUp(const ::benchmark::State &state) 52094332d3Sopenharmony_ci{ 53094332d3Sopenharmony_ci g_lightInterface = ILightInterface::Get(); 54094332d3Sopenharmony_ci} 55094332d3Sopenharmony_ci 56094332d3Sopenharmony_civoid LightBenchmarkTest::TearDown(const ::benchmark::State &state) 57094332d3Sopenharmony_ci{ 58094332d3Sopenharmony_ci} 59094332d3Sopenharmony_ci 60094332d3Sopenharmony_ci/** 61094332d3Sopenharmony_ci * @tc.name: SUB_DriverSystem_LightBenchmark_GetLightInfo 62094332d3Sopenharmony_ci * @tc.desc: Benchmarktest for interface GetLightInfo 63094332d3Sopenharmony_ci * @tc.type: FUNC 64094332d3Sopenharmony_ci */ 65094332d3Sopenharmony_ciBENCHMARK_F(LightBenchmarkTest, GetLightInfo)(benchmark::State &st) 66094332d3Sopenharmony_ci{ 67094332d3Sopenharmony_ci ASSERT_NE(nullptr, g_lightInterface); 68094332d3Sopenharmony_ci 69094332d3Sopenharmony_ci std::vector<HdfLightInfo> info; 70094332d3Sopenharmony_ci int32_t ret; 71094332d3Sopenharmony_ci 72094332d3Sopenharmony_ci for (auto _ : st) { 73094332d3Sopenharmony_ci ret = g_lightInterface->GetLightInfo(info); 74094332d3Sopenharmony_ci EXPECT_EQ(HDF_SUCCESS, ret); 75094332d3Sopenharmony_ci } 76094332d3Sopenharmony_ci 77094332d3Sopenharmony_ci for (auto iter : info) { 78094332d3Sopenharmony_ci EXPECT_GE(iter.lightId, MIN_LIGHT_ID); 79094332d3Sopenharmony_ci EXPECT_LE(iter.lightId, MAX_LIGHT_ID); 80094332d3Sopenharmony_ci } 81094332d3Sopenharmony_ci} 82094332d3Sopenharmony_ci 83094332d3Sopenharmony_ciBENCHMARK_REGISTER_F(LightBenchmarkTest, GetLightInfo)-> 84094332d3Sopenharmony_ci Iterations(ITERATION_FREQUENCY)->Repetitions(REPETITION_FREQUENCY)->ReportAggregatesOnly(); 85094332d3Sopenharmony_ci 86094332d3Sopenharmony_ci/** 87094332d3Sopenharmony_ci * @tc.name: SUB_DriverSystem_LightBenchmark_TurnOnLight 88094332d3Sopenharmony_ci * @tc.desc: Benchmarktest for interface TurnOnLight 89094332d3Sopenharmony_ci * @tc.type: FUNC 90094332d3Sopenharmony_ci */ 91094332d3Sopenharmony_ciBENCHMARK_F(LightBenchmarkTest, TurnOnLight)(benchmark::State &st) 92094332d3Sopenharmony_ci{ 93094332d3Sopenharmony_ci ASSERT_NE(nullptr, g_lightInterface); 94094332d3Sopenharmony_ci 95094332d3Sopenharmony_ci std::vector<HdfLightInfo> info; 96094332d3Sopenharmony_ci int32_t ret; 97094332d3Sopenharmony_ci 98094332d3Sopenharmony_ci ret = g_lightInterface->GetLightInfo(info); 99094332d3Sopenharmony_ci EXPECT_EQ(HDF_SUCCESS, ret); 100094332d3Sopenharmony_ci 101094332d3Sopenharmony_ci for (auto iter : info) { 102094332d3Sopenharmony_ci EXPECT_GE(iter.lightId, MIN_LIGHT_ID); 103094332d3Sopenharmony_ci EXPECT_LE(iter.lightId, MAX_LIGHT_ID); 104094332d3Sopenharmony_ci 105094332d3Sopenharmony_ci HdfLightEffect effect; 106094332d3Sopenharmony_ci effect.lightColor.colorValue.rgbColor.r = COLORVALUE_RED; 107094332d3Sopenharmony_ci effect.lightColor.colorValue.rgbColor.g = COLORVALUE_GREEN; 108094332d3Sopenharmony_ci effect.lightColor.colorValue.rgbColor.b = COLORVALUE_BLUE; 109094332d3Sopenharmony_ci effect.flashEffect.flashMode = LIGHT_FLASH_NONE; 110094332d3Sopenharmony_ci 111094332d3Sopenharmony_ci for (auto _ : st) { 112094332d3Sopenharmony_ci ret = g_lightInterface->TurnOnLight(iter.lightId, effect); 113094332d3Sopenharmony_ci EXPECT_EQ(HDF_SUCCESS, ret); 114094332d3Sopenharmony_ci } 115094332d3Sopenharmony_ci OsalMSleep(SLEEP_TIME); 116094332d3Sopenharmony_ci ret = g_lightInterface->TurnOffLight(iter.lightId); 117094332d3Sopenharmony_ci EXPECT_EQ(HDF_SUCCESS, ret); 118094332d3Sopenharmony_ci } 119094332d3Sopenharmony_ci} 120094332d3Sopenharmony_ci 121094332d3Sopenharmony_ciBENCHMARK_REGISTER_F(LightBenchmarkTest, TurnOnLight)-> 122094332d3Sopenharmony_ci Iterations(ITERATION_FREQUENCY)->Repetitions(REPETITION_FREQUENCY)->ReportAggregatesOnly(); 123094332d3Sopenharmony_ci 124094332d3Sopenharmony_ci/** 125094332d3Sopenharmony_ci * @tc.name: SUB_DriverSystem_LightBenchmark_TurnOffLight 126094332d3Sopenharmony_ci * @tc.desc: Benchmarktest for interface TurnOffLight 127094332d3Sopenharmony_ci * @tc.type: FUNC 128094332d3Sopenharmony_ci */ 129094332d3Sopenharmony_ciBENCHMARK_F(LightBenchmarkTest, TurnOffLight)(benchmark::State &st) 130094332d3Sopenharmony_ci{ 131094332d3Sopenharmony_ci ASSERT_NE(nullptr, g_lightInterface); 132094332d3Sopenharmony_ci 133094332d3Sopenharmony_ci std::vector<HdfLightInfo> info; 134094332d3Sopenharmony_ci int32_t ret; 135094332d3Sopenharmony_ci 136094332d3Sopenharmony_ci ret = g_lightInterface->GetLightInfo(info); 137094332d3Sopenharmony_ci EXPECT_EQ(HDF_SUCCESS, ret); 138094332d3Sopenharmony_ci 139094332d3Sopenharmony_ci for (auto iter : info) { 140094332d3Sopenharmony_ci EXPECT_GE(iter.lightId, MIN_LIGHT_ID); 141094332d3Sopenharmony_ci EXPECT_LE(iter.lightId, MAX_LIGHT_ID); 142094332d3Sopenharmony_ci 143094332d3Sopenharmony_ci HdfLightEffect effect; 144094332d3Sopenharmony_ci effect.lightColor.colorValue.rgbColor.r = COLORVALUE_RED; 145094332d3Sopenharmony_ci effect.lightColor.colorValue.rgbColor.g = COLORVALUE_GREEN; 146094332d3Sopenharmony_ci effect.lightColor.colorValue.rgbColor.b = COLORVALUE_BLUE; 147094332d3Sopenharmony_ci effect.flashEffect.flashMode = LIGHT_FLASH_BLINK; 148094332d3Sopenharmony_ci effect.flashEffect.onTime = ON_TIME; 149094332d3Sopenharmony_ci effect.flashEffect.offTime = OFF_TIME; 150094332d3Sopenharmony_ci ret = g_lightInterface->TurnOnLight(iter.lightId, effect); 151094332d3Sopenharmony_ci EXPECT_EQ(HDF_SUCCESS, ret); 152094332d3Sopenharmony_ci OsalMSleep(SLEEP_TIME); 153094332d3Sopenharmony_ci for (auto _ : st) { 154094332d3Sopenharmony_ci ret = g_lightInterface->TurnOffLight(iter.lightId); 155094332d3Sopenharmony_ci EXPECT_EQ(HDF_SUCCESS, ret); 156094332d3Sopenharmony_ci } 157094332d3Sopenharmony_ci } 158094332d3Sopenharmony_ci} 159094332d3Sopenharmony_ci 160094332d3Sopenharmony_ciBENCHMARK_REGISTER_F(LightBenchmarkTest, TurnOffLight)-> 161094332d3Sopenharmony_ci Iterations(ITERATION_FREQUENCY)->Repetitions(REPETITION_FREQUENCY)->ReportAggregatesOnly(); 162094332d3Sopenharmony_ci 163094332d3Sopenharmony_ci/** 164094332d3Sopenharmony_ci * @tc.name: SUB_DriverSystem_LightBenchmark_TurnOnMultiLights 165094332d3Sopenharmony_ci * @tc.desc: Benchmarktest for interface TurnOnMultiLights 166094332d3Sopenharmony_ci * @tc.type: FUNC 167094332d3Sopenharmony_ci */ 168094332d3Sopenharmony_ciBENCHMARK_F(LightBenchmarkTest, TurnOnMultiLights)(benchmark::State &st) 169094332d3Sopenharmony_ci{ 170094332d3Sopenharmony_ci ASSERT_NE(nullptr, g_lightInterface); 171094332d3Sopenharmony_ci 172094332d3Sopenharmony_ci std::vector<HdfLightInfo> info; 173094332d3Sopenharmony_ci int32_t ret; 174094332d3Sopenharmony_ci 175094332d3Sopenharmony_ci ret = g_lightInterface->GetLightInfo(info); 176094332d3Sopenharmony_ci EXPECT_EQ(0, ret); 177094332d3Sopenharmony_ci 178094332d3Sopenharmony_ci for (auto iter : info) { 179094332d3Sopenharmony_ci EXPECT_GE(iter.lightId, MIN_LIGHT_ID); 180094332d3Sopenharmony_ci EXPECT_LE(iter.lightId, MAX_LIGHT_ID); 181094332d3Sopenharmony_ci std::vector<HdfLightColor> lightColor; 182094332d3Sopenharmony_ci struct HdfLightColor light; 183094332d3Sopenharmony_ci light.colorValue.rgbColor.b = COLORVALUE_BLUE; 184094332d3Sopenharmony_ci lightColor.push_back(light); 185094332d3Sopenharmony_ci for (auto _ : st) { 186094332d3Sopenharmony_ci ret = g_lightInterface->TurnOnMultiLights(iter.lightId, lightColor); 187094332d3Sopenharmony_ci EXPECT_EQ(HDF_SUCCESS, ret); 188094332d3Sopenharmony_ci } 189094332d3Sopenharmony_ci } 190094332d3Sopenharmony_ci} 191094332d3Sopenharmony_ci 192094332d3Sopenharmony_ciBENCHMARK_REGISTER_F(LightBenchmarkTest, TurnOnMultiLights)-> 193094332d3Sopenharmony_ci Iterations(ITERATION_FREQUENCY)->Repetitions(REPETITION_FREQUENCY)->ReportAggregatesOnly(); 194094332d3Sopenharmony_ci} 195094332d3Sopenharmony_ci 196094332d3Sopenharmony_ciBENCHMARK_MAIN(); 197