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