blob: aee84de3ae7b596c50d2b7558bf11c4c2147936d [file] [log] [blame]
John Reck94c40fe2014-10-08 09:28:43 -07001/*
2 * Copyright (C) 2014 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
John Recke702c9c2015-10-07 10:26:02 -070017#include "Benchmark.h"
John Reck94c40fe2014-10-08 09:28:43 -070018
John Recke248bd12015-08-05 13:53:53 -070019#include "protos/hwui.pb.h"
20
John Reckb7dd29e2015-10-06 13:28:17 -070021#include <getopt.h>
John Recke702c9c2015-10-07 10:26:02 -070022#include <stdio.h>
23#include <string>
24#include <unistd.h>
25#include <unordered_map>
John Reckb7dd29e2015-10-06 13:28:17 -070026#include <vector>
John Reck7f2e5e32015-05-05 11:00:53 -070027
John Reck94c40fe2014-10-08 09:28:43 -070028using namespace android;
29using namespace android::uirenderer;
John Reck94c40fe2014-10-08 09:28:43 -070030
John Recke702c9c2015-10-07 10:26:02 -070031// Not a static global because we need to force the map to be constructed
32// before we try to add things to it.
33std::unordered_map<std::string, BenchmarkInfo>& testMap() {
34 static std::unordered_map<std::string, BenchmarkInfo> testMap;
35 return testMap;
John Reck94c40fe2014-10-08 09:28:43 -070036}
37
John Recke702c9c2015-10-07 10:26:02 -070038void Benchmark::registerBenchmark(const BenchmarkInfo& info) {
39 testMap()[info.name] = info;
John Reck94c40fe2014-10-08 09:28:43 -070040}
41
John Reckb7dd29e2015-10-06 13:28:17 -070042static int gFrameCount = 150;
43static int gRepeatCount = 1;
John Recke702c9c2015-10-07 10:26:02 -070044static std::vector<BenchmarkInfo> gRunTests;
John Reckb7dd29e2015-10-06 13:28:17 -070045
46static void printHelp() {
47 printf("\
48USAGE: hwuitest [OPTIONS] <TESTNAME>\n\
49\n\
50OPTIONS:\n\
51 -c, --count=NUM NUM loops a test should run (example, number of frames)\n\
52 -r, --runs=NUM Repeat the test(s) NUM times\n\
53 -h, --help Display this help\n\
54 --list List all tests\n\
55\n");
56}
57
58static void listTests() {
59 printf("Tests: \n");
John Recke702c9c2015-10-07 10:26:02 -070060 for (auto&& test : testMap()) {
61 auto&& info = test.second;
62 const char* col1 = info.name.c_str();
63 int dlen = info.description.length();
64 const char* col2 = info.description.c_str();
65 // World's best line breaking algorithm.
66 do {
67 int toPrint = dlen;
68 if (toPrint > 50) {
69 char* found = (char*) memrchr(col2, ' ', 50);
70 if (found) {
71 toPrint = found - col2;
72 } else {
73 toPrint = 50;
74 }
75 }
76 printf("%-20s %.*s\n", col1, toPrint, col2);
77 col1 = "";
78 col2 += toPrint;
79 dlen -= toPrint;
80 while (*col2 == ' ') {
81 col2++; dlen--;
82 }
83 } while (dlen > 0);
84 printf("\n");
John Reckb7dd29e2015-10-06 13:28:17 -070085 }
86}
87
88static const struct option LONG_OPTIONS[] = {
89 { "frames", required_argument, nullptr, 'f' },
90 { "repeat", required_argument, nullptr, 'r' },
91 { "help", no_argument, nullptr, 'h' },
92 { "list", no_argument, nullptr, 'l' },
93 { 0, 0, 0, 0 }
94};
95
96static const char* SHORT_OPTIONS = "c:r:h";
97
98void parseOptions(int argc, char* argv[]) {
99 int c;
100 // temporary variable
101 int count;
102 bool error = false;
103 opterr = 0;
104
105 while (true) {
106
107 /* getopt_long stores the option index here. */
108 int option_index = 0;
109
110 c = getopt_long(argc, argv, SHORT_OPTIONS, LONG_OPTIONS, &option_index);
111
112 if (c == -1)
113 break;
114
115 switch (c) {
116 case 0:
117 // Option set a flag, don't need to do anything
118 // (although none of the current LONG_OPTIONS do this...)
119 break;
120
121 case 'l':
122 listTests();
123 exit(EXIT_SUCCESS);
124 break;
125
126 case 'c':
127 count = atoi(optarg);
128 if (!count) {
129 fprintf(stderr, "Invalid frames argument '%s'\n", optarg);
130 error = true;
131 } else {
132 gFrameCount = (count > 0 ? count : INT_MAX);
133 }
134 break;
135
136 case 'r':
137 count = atoi(optarg);
138 if (!count) {
139 fprintf(stderr, "Invalid repeat argument '%s'\n", optarg);
140 error = true;
141 } else {
142 gRepeatCount = (count > 0 ? count : INT_MAX);
143 }
144 break;
145
146 case 'h':
147 printHelp();
148 exit(EXIT_SUCCESS);
149 break;
150
151 case '?':
152 fprintf(stderr, "Unrecognized option '%s'\n", argv[optind - 1]);
153 // fall-through
154 default:
155 error = true;
156 break;
157 }
158 }
159
160 if (error) {
161 fprintf(stderr, "Try 'hwuitest --help' for more information.\n");
162 exit(EXIT_FAILURE);
163 }
164
165 /* Print any remaining command line arguments (not options). */
166 if (optind < argc) {
167 do {
168 const char* test = argv[optind++];
John Recke702c9c2015-10-07 10:26:02 -0700169 auto pos = testMap().find(test);
170 if (pos == testMap().end()) {
John Reckb7dd29e2015-10-06 13:28:17 -0700171 fprintf(stderr, "Unknown test '%s'\n", test);
172 exit(EXIT_FAILURE);
173 } else {
174 gRunTests.push_back(pos->second);
175 }
176 } while (optind < argc);
177 } else {
John Recke702c9c2015-10-07 10:26:02 -0700178 gRunTests.push_back(testMap()["shadowgrid"]);
John Reckb7dd29e2015-10-06 13:28:17 -0700179 }
180}
181
Chris Craik03188872015-02-02 18:39:33 -0800182int main(int argc, char* argv[]) {
John Reckb7dd29e2015-10-06 13:28:17 -0700183 parseOptions(argc, argv);
184
John Recke702c9c2015-10-07 10:26:02 -0700185 BenchmarkOptions opts;
186 opts.count = gFrameCount;
John Reckb7dd29e2015-10-06 13:28:17 -0700187 for (int i = 0; i < gRepeatCount; i++) {
188 for (auto&& test : gRunTests) {
John Recke702c9c2015-10-07 10:26:02 -0700189 test.functor(opts);
Tim Murray1a0f1c72015-05-06 11:37:37 -0700190 }
191 }
John Reck94c40fe2014-10-08 09:28:43 -0700192 printf("Success!\n");
193 return 0;
194}