blob: 098b4ccea8cfff6999902564e197ab00d368ebe0 [file] [log] [blame]
John Reckdf1742e2017-01-19 15:56:21 -08001/*
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
Kweku Adams1856a4c2018-04-03 16:31:10 -070019#include "protos/graphicsstats.pb.h"
John Reckdf1742e2017-01-19 15:56:21 -080020#include "service/GraphicsStatsService.h"
21
John Reckdf1742e2017-01-19 15:56:21 -080022#include <stdio.h>
23#include <stdlib.h>
John Reck1bcacfd2017-11-03 10:12:19 -070024#include <sys/stat.h>
25#include <sys/types.h>
John Reckdf1742e2017-01-19 15:56:21 -080026#include <unistd.h>
27
28using namespace android;
29using namespace android::uirenderer;
30
31std::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;
John Reck1bcacfd2017-11-03 10:12:19 -070037 fprintf(stderr, "Failed to read from /proc/self/exe; r=%zd, err=%d (%s)\n", r, err,
38 strerror(err));
John Reckdf1742e2017-01-19 15:56:21 -080039 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
51TEST(GraphicsStats, findRootPath) {
Colin Cross99c9bf62017-05-04 09:58:59 -070052#ifdef __LP64__
53 std::string expected = "/data/nativetest64/hwui_unit_tests";
54#else
John Reckdf1742e2017-01-19 15:56:21 -080055 std::string expected = "/data/nativetest/hwui_unit_tests";
Colin Cross99c9bf62017-05-04 09:58:59 -070056#endif
John Reckdf1742e2017-01-19 15:56:21 -080057 EXPECT_EQ(expected, findRootPath());
58}
59
60TEST(GraphicsStats, saveLoad) {
61 std::string path = findRootPath() + "/test_saveLoad";
62 std::string packageName = "com.test.saveLoad";
John Reck7075c792017-07-05 14:03:43 -070063 MockProfileData mockData;
64 mockData.editJankFrameCount() = 20;
65 mockData.editTotalFrameCount() = 100;
66 mockData.editStatStartTime() = 10000;
John Reckdf1742e2017-01-19 15:56:21 -080067 // Fill with patterned data we can recognize but which won't map to a
68 // memset or basic for iteration count
John Reck7075c792017-07-05 14:03:43 -070069 for (size_t i = 0; i < mockData.editFrameCounts().size(); i++) {
70 mockData.editFrameCounts()[i] = ((i % 10) + 1) * 2;
John Reckdf1742e2017-01-19 15:56:21 -080071 }
John Reck7075c792017-07-05 14:03:43 -070072 for (size_t i = 0; i < mockData.editSlowFrameCounts().size(); i++) {
73 mockData.editSlowFrameCounts()[i] = (i % 5) + 1;
John Reckdf1742e2017-01-19 15:56:21 -080074 }
75 GraphicsStatsService::saveBuffer(path, packageName, 5, 3000, 7000, &mockData);
Kweku Adams1856a4c2018-04-03 16:31:10 -070076 protos::GraphicsStatsProto loadedProto;
John Reckdf1742e2017-01-19 15:56:21 -080077 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());
John Reck7075c792017-07-05 14:03:43 -070089 EXPECT_EQ(mockData.editFrameCounts().size() + mockData.editSlowFrameCounts().size(),
John Reck1bcacfd2017-11-03 10:12:19 -070090 (size_t)loadedProto.histogram_size());
91 for (size_t i = 0; i < (size_t)loadedProto.histogram_size(); i++) {
John Reckdf1742e2017-01-19 15:56:21 -080092 int expectedCount, expectedBucket;
John Reck7075c792017-07-05 14:03:43 -070093 if (i < mockData.editFrameCounts().size()) {
John Reckdf1742e2017-01-19 15:56:21 -080094 expectedCount = ((i % 10) + 1) * 2;
John Reck7075c792017-07-05 14:03:43 -070095 expectedBucket = ProfileData::frameTimeForFrameCountIndex(i);
John Reckdf1742e2017-01-19 15:56:21 -080096 } else {
John Reck7075c792017-07-05 14:03:43 -070097 int temp = i - mockData.editFrameCounts().size();
John Reckdf1742e2017-01-19 15:56:21 -080098 expectedCount = (temp % 5) + 1;
John Reck7075c792017-07-05 14:03:43 -070099 expectedBucket = ProfileData::frameTimeForSlowFrameCountIndex(temp);
John Reckdf1742e2017-01-19 15:56:21 -0800100 }
101 EXPECT_EQ(expectedCount, loadedProto.histogram().Get(i).frame_count());
102 EXPECT_EQ(expectedBucket, loadedProto.histogram().Get(i).render_millis());
103 }
104}
105
106TEST(GraphicsStats, merge) {
107 std::string path = findRootPath() + "/test_merge";
108 std::string packageName = "com.test.merge";
John Reck7075c792017-07-05 14:03:43 -0700109 MockProfileData mockData;
110 mockData.editJankFrameCount() = 20;
111 mockData.editTotalFrameCount() = 100;
112 mockData.editStatStartTime() = 10000;
John Reckdf1742e2017-01-19 15:56:21 -0800113 // Fill with patterned data we can recognize but which won't map to a
114 // memset or basic for iteration count
John Reck7075c792017-07-05 14:03:43 -0700115 for (size_t i = 0; i < mockData.editFrameCounts().size(); i++) {
116 mockData.editFrameCounts()[i] = ((i % 10) + 1) * 2;
John Reckdf1742e2017-01-19 15:56:21 -0800117 }
John Reck7075c792017-07-05 14:03:43 -0700118 for (size_t i = 0; i < mockData.editSlowFrameCounts().size(); i++) {
119 mockData.editSlowFrameCounts()[i] = (i % 5) + 1;
John Reckdf1742e2017-01-19 15:56:21 -0800120 }
121 GraphicsStatsService::saveBuffer(path, packageName, 5, 3000, 7000, &mockData);
John Reck7075c792017-07-05 14:03:43 -0700122 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;
John Reckdf1742e2017-01-19 15:56:21 -0800126 }
John Reck7075c792017-07-05 14:03:43 -0700127 for (size_t i = 0; i < mockData.editSlowFrameCounts().size(); i++) {
128 mockData.editSlowFrameCounts()[i] = ((i % 10) + 1) * 2;
John Reckdf1742e2017-01-19 15:56:21 -0800129 }
130 GraphicsStatsService::saveBuffer(path, packageName, 5, 7050, 10000, &mockData);
131
Kweku Adams1856a4c2018-04-03 16:31:10 -0700132 protos::GraphicsStatsProto loadedProto;
John Reckdf1742e2017-01-19 15:56:21 -0800133 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());
John Reck7075c792017-07-05 14:03:43 -0700145 EXPECT_EQ(mockData.editFrameCounts().size() + mockData.editSlowFrameCounts().size(),
John Reck1bcacfd2017-11-03 10:12:19 -0700146 (size_t)loadedProto.histogram_size());
147 for (size_t i = 0; i < (size_t)loadedProto.histogram_size(); i++) {
John Reckdf1742e2017-01-19 15:56:21 -0800148 int expectedCount, expectedBucket;
John Reck7075c792017-07-05 14:03:43 -0700149 if (i < mockData.editFrameCounts().size()) {
John Reckdf1742e2017-01-19 15:56:21 -0800150 expectedCount = ((i % 10) + 1) * 2;
151 expectedCount += (i % 5) + 1;
John Reck7075c792017-07-05 14:03:43 -0700152 expectedBucket = ProfileData::frameTimeForFrameCountIndex(i);
John Reckdf1742e2017-01-19 15:56:21 -0800153 } else {
John Reck7075c792017-07-05 14:03:43 -0700154 int temp = i - mockData.editFrameCounts().size();
John Reckdf1742e2017-01-19 15:56:21 -0800155 expectedCount = (temp % 5) + 1;
156 expectedCount += ((temp % 10) + 1) * 2;
John Reck7075c792017-07-05 14:03:43 -0700157 expectedBucket = ProfileData::frameTimeForSlowFrameCountIndex(temp);
John Reckdf1742e2017-01-19 15:56:21 -0800158 }
159 EXPECT_EQ(expectedCount, loadedProto.histogram().Get(i).frame_count());
160 EXPECT_EQ(expectedBucket, loadedProto.histogram().Get(i).render_millis());
161 }
Colin Cross99c9bf62017-05-04 09:58:59 -0700162}