Yiwei Zhang | d476542 | 2019-03-11 15:05:12 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #undef LOG_TAG |
| 18 | #define LOG_TAG "GpuStatsPuller_test" |
| 19 | |
| 20 | #include <gmock/gmock.h> |
| 21 | #include <gtest/gtest.h> |
| 22 | |
| 23 | #include <graphicsenv/GpuStatsInfo.h> |
| 24 | #include <log/log.h> |
| 25 | |
| 26 | #include "src/external/GpuStatsPuller.h" |
| 27 | |
| 28 | #ifdef __ANDROID__ |
| 29 | |
| 30 | namespace android { |
| 31 | namespace os { |
| 32 | namespace statsd { |
| 33 | |
| 34 | // clang-format off |
| 35 | static const std::string DRIVER_PACKAGE_NAME = "TEST_DRIVER"; |
| 36 | static const std::string DRIVER_VERSION_NAME = "TEST_DRIVER_VERSION"; |
| 37 | static const std::string APP_PACKAGE_NAME = "TEST_APP"; |
| 38 | static const int64_t TIMESTAMP_WALLCLOCK = 111; |
| 39 | static const int64_t TIMESTAMP_ELAPSED = 222; |
| 40 | static const int64_t DRIVER_VERSION_CODE = 333; |
| 41 | static const int64_t DRIVER_BUILD_TIME = 444; |
| 42 | static const int64_t GL_LOADING_COUNT = 3; |
| 43 | static const int64_t GL_LOADING_FAILURE_COUNT = 1; |
| 44 | static const int64_t VK_LOADING_COUNT = 4; |
| 45 | static const int64_t VK_LOADING_FAILURE_COUNT = 0; |
| 46 | static const int64_t GL_DRIVER_LOADING_TIME_0 = 555; |
| 47 | static const int64_t GL_DRIVER_LOADING_TIME_1 = 666; |
| 48 | static const int64_t VK_DRIVER_LOADING_TIME_0 = 777; |
| 49 | static const int64_t VK_DRIVER_LOADING_TIME_1 = 888; |
| 50 | static const int64_t VK_DRIVER_LOADING_TIME_2 = 999; |
| 51 | static const size_t NUMBER_OF_VALUES_GLOBAL = 8; |
| 52 | static const size_t NUMBER_OF_VALUES_APP = 4; |
| 53 | // clang-format on |
| 54 | |
| 55 | class MockGpuStatsPuller : public GpuStatsPuller { |
| 56 | public: |
| 57 | MockGpuStatsPuller(const int tagId, vector<std::shared_ptr<LogEvent>>* data) |
| 58 | : GpuStatsPuller(tagId), mData(data){}; |
| 59 | |
| 60 | private: |
| 61 | bool PullInternal(vector<std::shared_ptr<LogEvent>>* data) override { |
| 62 | *data = *mData; |
| 63 | return true; |
| 64 | } |
| 65 | |
| 66 | vector<std::shared_ptr<LogEvent>>* mData; |
| 67 | }; |
| 68 | |
| 69 | class GpuStatsPuller_test : public ::testing::Test { |
| 70 | public: |
| 71 | GpuStatsPuller_test() { |
| 72 | const ::testing::TestInfo* const test_info = |
| 73 | ::testing::UnitTest::GetInstance()->current_test_info(); |
| 74 | ALOGD("**** Setting up for %s.%s\n", test_info->test_case_name(), test_info->name()); |
| 75 | } |
| 76 | |
| 77 | ~GpuStatsPuller_test() { |
| 78 | const ::testing::TestInfo* const test_info = |
| 79 | ::testing::UnitTest::GetInstance()->current_test_info(); |
| 80 | ALOGD("**** Tearing down after %s.%s\n", test_info->test_case_name(), test_info->name()); |
| 81 | } |
| 82 | }; |
| 83 | |
| 84 | TEST_F(GpuStatsPuller_test, PullGpuStatsGlobalInfo) { |
| 85 | vector<std::shared_ptr<LogEvent>> inData, outData; |
| 86 | std::shared_ptr<LogEvent> event = make_shared<LogEvent>(android::util::GPU_STATS_GLOBAL_INFO, |
| 87 | TIMESTAMP_WALLCLOCK, TIMESTAMP_ELAPSED); |
| 88 | EXPECT_TRUE(event->write(DRIVER_PACKAGE_NAME)); |
| 89 | EXPECT_TRUE(event->write(DRIVER_VERSION_NAME)); |
| 90 | EXPECT_TRUE(event->write(DRIVER_VERSION_CODE)); |
| 91 | EXPECT_TRUE(event->write(DRIVER_BUILD_TIME)); |
| 92 | EXPECT_TRUE(event->write(GL_LOADING_COUNT)); |
| 93 | EXPECT_TRUE(event->write(GL_LOADING_FAILURE_COUNT)); |
| 94 | EXPECT_TRUE(event->write(VK_LOADING_COUNT)); |
| 95 | EXPECT_TRUE(event->write(VK_LOADING_FAILURE_COUNT)); |
| 96 | event->init(); |
| 97 | inData.emplace_back(event); |
| 98 | MockGpuStatsPuller mockPuller(android::util::GPU_STATS_GLOBAL_INFO, &inData); |
| 99 | mockPuller.ForceClearCache(); |
| 100 | mockPuller.Pull(&outData); |
| 101 | |
| 102 | ASSERT_EQ(1, outData.size()); |
| 103 | EXPECT_EQ(android::util::GPU_STATS_GLOBAL_INFO, outData[0]->GetTagId()); |
| 104 | ASSERT_EQ(NUMBER_OF_VALUES_GLOBAL, outData[0]->size()); |
| 105 | EXPECT_EQ(DRIVER_PACKAGE_NAME, outData[0]->getValues()[0].mValue.str_value); |
| 106 | EXPECT_EQ(DRIVER_VERSION_NAME, outData[0]->getValues()[1].mValue.str_value); |
| 107 | EXPECT_EQ(DRIVER_VERSION_CODE, outData[0]->getValues()[2].mValue.long_value); |
| 108 | EXPECT_EQ(DRIVER_BUILD_TIME, outData[0]->getValues()[3].mValue.long_value); |
| 109 | EXPECT_EQ(GL_LOADING_COUNT, outData[0]->getValues()[4].mValue.long_value); |
| 110 | EXPECT_EQ(GL_LOADING_FAILURE_COUNT, outData[0]->getValues()[5].mValue.long_value); |
| 111 | EXPECT_EQ(VK_LOADING_COUNT, outData[0]->getValues()[6].mValue.long_value); |
| 112 | EXPECT_EQ(VK_LOADING_FAILURE_COUNT, outData[0]->getValues()[7].mValue.long_value); |
| 113 | } |
| 114 | |
| 115 | TEST_F(GpuStatsPuller_test, PullGpuStatsAppInfo) { |
| 116 | vector<std::shared_ptr<LogEvent>> inData, outData; |
| 117 | std::shared_ptr<LogEvent> event = make_shared<LogEvent>(android::util::GPU_STATS_APP_INFO, |
| 118 | TIMESTAMP_WALLCLOCK, TIMESTAMP_ELAPSED); |
| 119 | EXPECT_TRUE(event->write(APP_PACKAGE_NAME)); |
| 120 | EXPECT_TRUE(event->write(DRIVER_VERSION_CODE)); |
| 121 | std::vector<int64_t> glDriverLoadingTime; |
| 122 | glDriverLoadingTime.emplace_back(GL_DRIVER_LOADING_TIME_0); |
| 123 | glDriverLoadingTime.emplace_back(GL_DRIVER_LOADING_TIME_1); |
| 124 | std::vector<int64_t> vkDriverLoadingTime; |
| 125 | vkDriverLoadingTime.emplace_back(VK_DRIVER_LOADING_TIME_0); |
| 126 | vkDriverLoadingTime.emplace_back(VK_DRIVER_LOADING_TIME_1); |
| 127 | vkDriverLoadingTime.emplace_back(VK_DRIVER_LOADING_TIME_2); |
| 128 | EXPECT_TRUE(event->write(int64VectorToProtoByteString(glDriverLoadingTime))); |
| 129 | EXPECT_TRUE(event->write(int64VectorToProtoByteString(vkDriverLoadingTime))); |
| 130 | event->init(); |
| 131 | inData.emplace_back(event); |
| 132 | MockGpuStatsPuller mockPuller(android::util::GPU_STATS_APP_INFO, &inData); |
| 133 | mockPuller.ForceClearCache(); |
| 134 | mockPuller.Pull(&outData); |
| 135 | |
| 136 | ASSERT_EQ(1, outData.size()); |
| 137 | EXPECT_EQ(android::util::GPU_STATS_APP_INFO, outData[0]->GetTagId()); |
| 138 | ASSERT_EQ(NUMBER_OF_VALUES_APP, outData[0]->size()); |
| 139 | EXPECT_EQ(APP_PACKAGE_NAME, outData[0]->getValues()[0].mValue.str_value); |
| 140 | EXPECT_EQ(DRIVER_VERSION_CODE, outData[0]->getValues()[1].mValue.long_value); |
| 141 | EXPECT_EQ(int64VectorToProtoByteString(glDriverLoadingTime), |
| 142 | outData[0]->getValues()[2].mValue.str_value); |
| 143 | EXPECT_EQ(int64VectorToProtoByteString(vkDriverLoadingTime), |
| 144 | outData[0]->getValues()[3].mValue.str_value); |
| 145 | } |
| 146 | |
| 147 | } // namespace statsd |
| 148 | } // namespace os |
| 149 | } // namespace android |
| 150 | #else |
| 151 | GTEST_LOG_(INFO) << "This test does nothing.\n"; |
| 152 | #endif |