Home | History | Annotate | Download | only in unit
      1 /*
      2  * Copyright (C) 2016 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 #include <gtest/gtest.h>
     18 
     19 #include "service/GraphicsStatsService.h"
     20 
     21 #include <frameworks/base/core/proto/android/service/graphicsstats.pb.h>
     22 
     23 #include <sys/types.h>
     24 #include <sys/stat.h>
     25 #include <stdio.h>
     26 #include <stdlib.h>
     27 #include <unistd.h>
     28 
     29 using namespace android;
     30 using namespace android::uirenderer;
     31 
     32 std::string findRootPath() {
     33     char path[1024];
     34     ssize_t r = readlink("/proc/self/exe", path, 1024);
     35     // < 1023 because we need room for the null terminator
     36     if (r <= 0 || r > 1023) {
     37         int err = errno;
     38         fprintf(stderr, "Failed to read from /proc/self/exe; r=%zd, err=%d (%s)\n",
     39                 r, err, strerror(err));
     40         exit(EXIT_FAILURE);
     41     }
     42     while (--r > 0) {
     43         if (path[r] == '/') {
     44             path[r] = '\0';
     45             return std::string(path);
     46         }
     47     }
     48     return std::string();
     49 }
     50 
     51 // No code left untested
     52 TEST(GraphicsStats, findRootPath) {
     53 #ifdef __LP64__
     54     std::string expected = "/data/nativetest64/hwui_unit_tests";
     55 #else
     56     std::string expected = "/data/nativetest/hwui_unit_tests";
     57 #endif
     58     EXPECT_EQ(expected, findRootPath());
     59 }
     60 
     61 TEST(GraphicsStats, saveLoad) {
     62     std::string path = findRootPath() + "/test_saveLoad";
     63     std::string packageName = "com.test.saveLoad";
     64     MockProfileData mockData;
     65     mockData.editJankFrameCount() = 20;
     66     mockData.editTotalFrameCount() = 100;
     67     mockData.editStatStartTime() = 10000;
     68     // Fill with patterned data we can recognize but which won't map to a
     69     // memset or basic for iteration count
     70     for (size_t i = 0; i < mockData.editFrameCounts().size(); i++) {
     71         mockData.editFrameCounts()[i] = ((i % 10) + 1) * 2;
     72     }
     73     for (size_t i = 0; i < mockData.editSlowFrameCounts().size(); i++) {
     74         mockData.editSlowFrameCounts()[i] = (i % 5) + 1;
     75     }
     76     GraphicsStatsService::saveBuffer(path, packageName, 5, 3000, 7000, &mockData);
     77     service::GraphicsStatsProto loadedProto;
     78     EXPECT_TRUE(GraphicsStatsService::parseFromFile(path, &loadedProto));
     79     // Clean up the file
     80     unlink(path.c_str());
     81 
     82     EXPECT_EQ(packageName, loadedProto.package_name());
     83     EXPECT_EQ(5, loadedProto.version_code());
     84     EXPECT_EQ(3000, loadedProto.stats_start());
     85     EXPECT_EQ(7000, loadedProto.stats_end());
     86     // ASSERT here so we don't continue with a nullptr deref crash if this is false
     87     ASSERT_TRUE(loadedProto.has_summary());
     88     EXPECT_EQ(20, loadedProto.summary().janky_frames());
     89     EXPECT_EQ(100, loadedProto.summary().total_frames());
     90     EXPECT_EQ(mockData.editFrameCounts().size() + mockData.editSlowFrameCounts().size(),
     91             (size_t) loadedProto.histogram_size());
     92     for (size_t i = 0; i < (size_t) loadedProto.histogram_size(); i++) {
     93         int expectedCount, expectedBucket;
     94         if (i < mockData.editFrameCounts().size()) {
     95             expectedCount = ((i % 10) + 1) * 2;
     96             expectedBucket = ProfileData::frameTimeForFrameCountIndex(i);
     97         } else {
     98             int temp = i - mockData.editFrameCounts().size();
     99             expectedCount = (temp % 5) + 1;
    100             expectedBucket = ProfileData::frameTimeForSlowFrameCountIndex(temp);
    101         }
    102         EXPECT_EQ(expectedCount, loadedProto.histogram().Get(i).frame_count());
    103         EXPECT_EQ(expectedBucket, loadedProto.histogram().Get(i).render_millis());
    104     }
    105 }
    106 
    107 TEST(GraphicsStats, merge) {
    108     std::string path = findRootPath() + "/test_merge";
    109     std::string packageName = "com.test.merge";
    110     MockProfileData mockData;
    111     mockData.editJankFrameCount() = 20;
    112     mockData.editTotalFrameCount() = 100;
    113     mockData.editStatStartTime() = 10000;
    114     // Fill with patterned data we can recognize but which won't map to a
    115     // memset or basic for iteration count
    116     for (size_t i = 0; i < mockData.editFrameCounts().size(); i++) {
    117         mockData.editFrameCounts()[i] = ((i % 10) + 1) * 2;
    118     }
    119     for (size_t i = 0; i < mockData.editSlowFrameCounts().size(); i++) {
    120         mockData.editSlowFrameCounts()[i] = (i % 5) + 1;
    121     }
    122     GraphicsStatsService::saveBuffer(path, packageName, 5, 3000, 7000, &mockData);
    123     mockData.editJankFrameCount() = 50;
    124     mockData.editTotalFrameCount() = 500;
    125     for (size_t i = 0; i < mockData.editFrameCounts().size(); i++) {
    126         mockData.editFrameCounts()[i] = (i % 5) + 1;
    127     }
    128     for (size_t i = 0; i < mockData.editSlowFrameCounts().size(); i++) {
    129         mockData.editSlowFrameCounts()[i] = ((i % 10) + 1) * 2;
    130     }
    131     GraphicsStatsService::saveBuffer(path, packageName, 5, 7050, 10000, &mockData);
    132 
    133     service::GraphicsStatsProto loadedProto;
    134     EXPECT_TRUE(GraphicsStatsService::parseFromFile(path, &loadedProto));
    135     // Clean up the file
    136     unlink(path.c_str());
    137 
    138     EXPECT_EQ(packageName, loadedProto.package_name());
    139     EXPECT_EQ(5, loadedProto.version_code());
    140     EXPECT_EQ(3000, loadedProto.stats_start());
    141     EXPECT_EQ(10000, loadedProto.stats_end());
    142     // ASSERT here so we don't continue with a nullptr deref crash if this is false
    143     ASSERT_TRUE(loadedProto.has_summary());
    144     EXPECT_EQ(20 + 50, loadedProto.summary().janky_frames());
    145     EXPECT_EQ(100 + 500, loadedProto.summary().total_frames());
    146     EXPECT_EQ(mockData.editFrameCounts().size() + mockData.editSlowFrameCounts().size(),
    147             (size_t) loadedProto.histogram_size());
    148     for (size_t i = 0; i < (size_t) loadedProto.histogram_size(); i++) {
    149         int expectedCount, expectedBucket;
    150         if (i < mockData.editFrameCounts().size()) {
    151             expectedCount = ((i % 10) + 1) * 2;
    152             expectedCount += (i % 5) + 1;
    153             expectedBucket = ProfileData::frameTimeForFrameCountIndex(i);
    154         } else {
    155             int temp = i - mockData.editFrameCounts().size();
    156             expectedCount = (temp % 5) + 1;
    157             expectedCount += ((temp % 10) + 1) * 2;
    158             expectedBucket = ProfileData::frameTimeForSlowFrameCountIndex(temp);
    159         }
    160         EXPECT_EQ(expectedCount, loadedProto.histogram().Get(i).frame_count());
    161         EXPECT_EQ(expectedBucket, loadedProto.histogram().Get(i).render_millis());
    162     }
    163 }
    164