19762338dSopenharmony_ci/*
29762338dSopenharmony_ci * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
39762338dSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
49762338dSopenharmony_ci * you may not use this file except in compliance with the License.
59762338dSopenharmony_ci * You may obtain a copy of the License at
69762338dSopenharmony_ci *
79762338dSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
89762338dSopenharmony_ci *
99762338dSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
109762338dSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
119762338dSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
129762338dSopenharmony_ci * See the License for the specific language governing permissions and
139762338dSopenharmony_ci * limitations under the License.
149762338dSopenharmony_ci */
159762338dSopenharmony_ci
169762338dSopenharmony_ci#include <benchmark/benchmark.h>
179762338dSopenharmony_ci#include <string>
189762338dSopenharmony_ci#include <vector>
199762338dSopenharmony_ci
209762338dSopenharmony_ci#include <cmath>
219762338dSopenharmony_ci#include <cstdio>
229762338dSopenharmony_ci#include <gtest/gtest.h>
239762338dSopenharmony_ci#include <securec.h>
249762338dSopenharmony_ci#include "hdf_base.h"
259762338dSopenharmony_ci#include "osal_time.h"
269762338dSopenharmony_ci#include "v1_0/ilight_interface.h"
279762338dSopenharmony_ci#include "light_type.h"
289762338dSopenharmony_ci
299762338dSopenharmony_ciusing namespace OHOS::HDI::Light::V1_0;
309762338dSopenharmony_ciusing namespace testing::ext;
319762338dSopenharmony_ciusing namespace std;
329762338dSopenharmony_ci
339762338dSopenharmony_cinamespace {
349762338dSopenharmony_ci    constexpr uint32_t g_sleepTime = 3;
359762338dSopenharmony_ci    constexpr int32_t g_minLightId = HDF_LIGHT_ID_BATTERY;
369762338dSopenharmony_ci    constexpr int32_t g_maxLightId = HDF_LIGHT_ID_ATTENTION;
379762338dSopenharmony_ci    constexpr int32_t g_onTime = 500;
389762338dSopenharmony_ci    constexpr int32_t g_offTime = 500;
399762338dSopenharmony_ci    sptr<ILightInterface> g_lightInterface = nullptr;
409762338dSopenharmony_ci
419762338dSopenharmony_ciclass lightBenchmarkTest : public benchmark::Fixture {
429762338dSopenharmony_cipublic:
439762338dSopenharmony_ci    void SetUp(const ::benchmark::State &state);
449762338dSopenharmony_ci    void TearDown(const ::benchmark::State &state);
459762338dSopenharmony_ci};
469762338dSopenharmony_ci
479762338dSopenharmony_civoid lightBenchmarkTest::SetUp(const ::benchmark::State &state)
489762338dSopenharmony_ci{
499762338dSopenharmony_ci    g_lightInterface = ILightInterface::Get();
509762338dSopenharmony_ci}
519762338dSopenharmony_ci
529762338dSopenharmony_civoid lightBenchmarkTest::TearDown(const ::benchmark::State &state)
539762338dSopenharmony_ci{
549762338dSopenharmony_ci}
559762338dSopenharmony_ci
569762338dSopenharmony_ci/**
579762338dSopenharmony_ci  * @tc.name: SUB_DriverSystem_LightBenchmark_0010
589762338dSopenharmony_ci  * @tc.desc: Benchmarktest for interface GetLightInfo.
599762338dSopenharmony_ci  * @tc.type: FUNC
609762338dSopenharmony_ci  */
619762338dSopenharmony_ciBENCHMARK_F(lightBenchmarkTest, SUB_DriverSystem_LightBenchmark_0010)(benchmark::State &st)
629762338dSopenharmony_ci{
639762338dSopenharmony_ci    ASSERT_NE(nullptr, g_lightInterface);
649762338dSopenharmony_ci
659762338dSopenharmony_ci    std::vector<HdfLightInfo> info;
669762338dSopenharmony_ci    int32_t ret;
679762338dSopenharmony_ci    for (auto _ : st) {
689762338dSopenharmony_ci        ret = g_lightInterface->GetLightInfo(info);
699762338dSopenharmony_ci        EXPECT_EQ(0, ret);
709762338dSopenharmony_ci    }
719762338dSopenharmony_ci
729762338dSopenharmony_ci    for (auto iter : info)
739762338dSopenharmony_ci    {
749762338dSopenharmony_ci        EXPECT_GE(iter.lightId, g_minLightId);
759762338dSopenharmony_ci        EXPECT_LE(iter.lightId, g_maxLightId);
769762338dSopenharmony_ci    }
779762338dSopenharmony_ci}
789762338dSopenharmony_ci
799762338dSopenharmony_ciBENCHMARK_REGISTER_F(lightBenchmarkTest, SUB_DriverSystem_LightBenchmark_0010)->
809762338dSopenharmony_ci    Iterations(100)->Repetitions(3)->ReportAggregatesOnly();
819762338dSopenharmony_ci
829762338dSopenharmony_ci/**
839762338dSopenharmony_ci  * @tc.name: SUB_DriverSystem_LightBenchmark_0020
849762338dSopenharmony_ci  * @tc.desc: Benchmarktest for interface TurnOnLight.
859762338dSopenharmony_ci  * @tc.type: FUNC
869762338dSopenharmony_ci  */
879762338dSopenharmony_ciBENCHMARK_F(lightBenchmarkTest, SUB_DriverSystem_LightBenchmark_0020)(benchmark::State &st)
889762338dSopenharmony_ci{
899762338dSopenharmony_ci    ASSERT_NE(nullptr, g_lightInterface);
909762338dSopenharmony_ci
919762338dSopenharmony_ci    std::vector<HdfLightInfo> info;
929762338dSopenharmony_ci    int32_t ret = g_lightInterface->GetLightInfo(info);
939762338dSopenharmony_ci    EXPECT_EQ(HDF_SUCCESS, ret);
949762338dSopenharmony_ci
959762338dSopenharmony_ci    for (auto iter : info)
969762338dSopenharmony_ci    {
979762338dSopenharmony_ci        EXPECT_GE(iter.lightId, g_minLightId);
989762338dSopenharmony_ci        EXPECT_LE(iter.lightId, g_maxLightId);
999762338dSopenharmony_ci
1009762338dSopenharmony_ci        HdfLightEffect effect;
1019762338dSopenharmony_ci        effect.lightColor.colorValue.rgbColor.r = 255;
1029762338dSopenharmony_ci        effect.lightColor.colorValue.rgbColor.g = 0;
1039762338dSopenharmony_ci        effect.lightColor.colorValue.rgbColor.b = 0;
1049762338dSopenharmony_ci        effect.flashEffect.flashMode = LIGHT_FLASH_NONE;
1059762338dSopenharmony_ci        int32_t ret;
1069762338dSopenharmony_ci        for (auto _ : st) {
1079762338dSopenharmony_ci            ret = g_lightInterface->TurnOnLight(iter.lightId, effect);
1089762338dSopenharmony_ci        }
1099762338dSopenharmony_ci        EXPECT_EQ(HDF_SUCCESS, ret);
1109762338dSopenharmony_ci        OsalMSleep(g_sleepTime);
1119762338dSopenharmony_ci        ret = g_lightInterface->TurnOffLight(iter.lightId);
1129762338dSopenharmony_ci        EXPECT_EQ(HDF_SUCCESS, ret);
1139762338dSopenharmony_ci    }
1149762338dSopenharmony_ci}
1159762338dSopenharmony_ci
1169762338dSopenharmony_ciBENCHMARK_REGISTER_F(lightBenchmarkTest, SUB_DriverSystem_LightBenchmark_0020)->
1179762338dSopenharmony_ci    Iterations(100)->Repetitions(3)->ReportAggregatesOnly();
1189762338dSopenharmony_ci
1199762338dSopenharmony_ci/**
1209762338dSopenharmony_ci  * @tc.name: SUB_DriverSystem_LightBenchmark_0030
1219762338dSopenharmony_ci  * @tc.desc: Benchmarktest for interface TurnOffLight.
1229762338dSopenharmony_ci  * @tc.type: FUNC
1239762338dSopenharmony_ci  */
1249762338dSopenharmony_ciBENCHMARK_F(lightBenchmarkTest, SUB_DriverSystem_LightBenchmark_0030)(benchmark::State &st)
1259762338dSopenharmony_ci{
1269762338dSopenharmony_ci    ASSERT_NE(nullptr, g_lightInterface);
1279762338dSopenharmony_ci
1289762338dSopenharmony_ci    std::vector<HdfLightInfo> info;
1299762338dSopenharmony_ci    int32_t ret = g_lightInterface->GetLightInfo(info);
1309762338dSopenharmony_ci    EXPECT_EQ(0, ret);
1319762338dSopenharmony_ci    printf("get light list num[%zu]\n\r", info.size());
1329762338dSopenharmony_ci
1339762338dSopenharmony_ci    for (auto iter : info)
1349762338dSopenharmony_ci    {
1359762338dSopenharmony_ci        EXPECT_GE(iter.lightId, g_minLightId);
1369762338dSopenharmony_ci        EXPECT_LE(iter.lightId, g_maxLightId);
1379762338dSopenharmony_ci
1389762338dSopenharmony_ci        HdfLightEffect effect;
1399762338dSopenharmony_ci        effect.lightColor.colorValue.rgbColor.r = 255;
1409762338dSopenharmony_ci        effect.lightColor.colorValue.rgbColor.g = 0;
1419762338dSopenharmony_ci        effect.lightColor.colorValue.rgbColor.b = 0;
1429762338dSopenharmony_ci        effect.flashEffect.flashMode = LIGHT_FLASH_BLINK;
1439762338dSopenharmony_ci        effect.flashEffect.onTime = g_onTime;
1449762338dSopenharmony_ci        effect.flashEffect.offTime = g_offTime;
1459762338dSopenharmony_ci        int32_t ret = g_lightInterface->TurnOnLight(iter.lightId, effect);
1469762338dSopenharmony_ci        EXPECT_EQ(HDF_SUCCESS, ret);
1479762338dSopenharmony_ci        OsalMSleep(g_sleepTime);
1489762338dSopenharmony_ci        for (auto _ : st) {
1499762338dSopenharmony_ci            ret = g_lightInterface->TurnOffLight(iter.lightId);
1509762338dSopenharmony_ci        }
1519762338dSopenharmony_ci        EXPECT_EQ(HDF_SUCCESS, ret);
1529762338dSopenharmony_ci    }
1539762338dSopenharmony_ci}
1549762338dSopenharmony_ci
1559762338dSopenharmony_ciBENCHMARK_REGISTER_F(lightBenchmarkTest, SUB_DriverSystem_LightBenchmark_0030)->
1569762338dSopenharmony_ci    Iterations(100)-> Repetitions(3)->ReportAggregatesOnly();
1579762338dSopenharmony_ci
1589762338dSopenharmony_ci/**
1599762338dSopenharmony_ci  * @tc.name: SUB_DriverSystem_LightBenchmark_0040
1609762338dSopenharmony_ci  * @tc.desc: Benchmarktest for interface TurnOnMultiLights.
1619762338dSopenharmony_ci  * @tc.type: FUNC
1629762338dSopenharmony_ci  */
1639762338dSopenharmony_ciBENCHMARK_F(lightBenchmarkTest, SUB_DriverSystem_LightBenchmark_0040)(benchmark::State &st)
1649762338dSopenharmony_ci{
1659762338dSopenharmony_ci    ASSERT_NE(nullptr, g_lightInterface);
1669762338dSopenharmony_ci
1679762338dSopenharmony_ci    std::vector<HdfLightInfo> info;
1689762338dSopenharmony_ci    int32_t ret = g_lightInterface-> GetLightInfo(info);
1699762338dSopenharmony_ci    EXPECT_EQ(0, ret);
1709762338dSopenharmony_ci    for (auto iter : info)
1719762338dSopenharmony_ci    {
1729762338dSopenharmony_ci        EXPECT_GE(iter.lightId, g_minLightId);
1739762338dSopenharmony_ci        EXPECT_LE(iter.lightId, g_maxLightId);
1749762338dSopenharmony_ci        std::vector<HdfLightColor> lightColor;
1759762338dSopenharmony_ci        struct HdfLightColor light;
1769762338dSopenharmony_ci        light.colorValue.rgbColor.b = 0;
1779762338dSopenharmony_ci        lightColor.push_back(light);
1789762338dSopenharmony_ci        for (auto _ : st) {
1799762338dSopenharmony_ci        ret = g_lightInterface->TurnOnMultiLights(iter.lightId, lightColor);
1809762338dSopenharmony_ci        }
1819762338dSopenharmony_ci        EXPECT_EQ(HDF_SUCCESS, ret);
1829762338dSopenharmony_ci    }
1839762338dSopenharmony_ci}
1849762338dSopenharmony_ciBENCHMARK_REGISTER_F(lightBenchmarkTest, SUB_DriverSystem_LightBenchmark_0040)->
1859762338dSopenharmony_ci    Iterations(100)-> Repetitions(3)->ReportAggregatesOnly();
1869762338dSopenharmony_ci}
1879762338dSopenharmony_ciBENCHMARK_MAIN();