John Reck | 94c40fe | 2014-10-08 09:28:43 -0700 | [diff] [blame] | 1 | /* |
| 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 Reck | e702c9c | 2015-10-07 10:26:02 -0700 | [diff] [blame] | 17 | #include "Benchmark.h" |
John Reck | 94c40fe | 2014-10-08 09:28:43 -0700 | [diff] [blame] | 18 | |
John Reck | e248bd1 | 2015-08-05 13:53:53 -0700 | [diff] [blame] | 19 | #include "protos/hwui.pb.h" |
| 20 | |
John Reck | b7dd29e | 2015-10-06 13:28:17 -0700 | [diff] [blame] | 21 | #include <getopt.h> |
John Reck | e702c9c | 2015-10-07 10:26:02 -0700 | [diff] [blame] | 22 | #include <stdio.h> |
| 23 | #include <string> |
| 24 | #include <unistd.h> |
| 25 | #include <unordered_map> |
John Reck | b7dd29e | 2015-10-06 13:28:17 -0700 | [diff] [blame] | 26 | #include <vector> |
John Reck | 7f2e5e3 | 2015-05-05 11:00:53 -0700 | [diff] [blame] | 27 | |
John Reck | 94c40fe | 2014-10-08 09:28:43 -0700 | [diff] [blame] | 28 | using namespace android; |
| 29 | using namespace android::uirenderer; |
John Reck | 94c40fe | 2014-10-08 09:28:43 -0700 | [diff] [blame] | 30 | |
John Reck | e702c9c | 2015-10-07 10:26:02 -0700 | [diff] [blame] | 31 | // Not a static global because we need to force the map to be constructed |
| 32 | // before we try to add things to it. |
| 33 | std::unordered_map<std::string, BenchmarkInfo>& testMap() { |
| 34 | static std::unordered_map<std::string, BenchmarkInfo> testMap; |
| 35 | return testMap; |
John Reck | 94c40fe | 2014-10-08 09:28:43 -0700 | [diff] [blame] | 36 | } |
| 37 | |
John Reck | e702c9c | 2015-10-07 10:26:02 -0700 | [diff] [blame] | 38 | void Benchmark::registerBenchmark(const BenchmarkInfo& info) { |
| 39 | testMap()[info.name] = info; |
John Reck | 94c40fe | 2014-10-08 09:28:43 -0700 | [diff] [blame] | 40 | } |
| 41 | |
John Reck | b7dd29e | 2015-10-06 13:28:17 -0700 | [diff] [blame] | 42 | static int gFrameCount = 150; |
| 43 | static int gRepeatCount = 1; |
John Reck | e702c9c | 2015-10-07 10:26:02 -0700 | [diff] [blame] | 44 | static std::vector<BenchmarkInfo> gRunTests; |
John Reck | b7dd29e | 2015-10-06 13:28:17 -0700 | [diff] [blame] | 45 | |
| 46 | static void printHelp() { |
| 47 | printf("\ |
| 48 | USAGE: hwuitest [OPTIONS] <TESTNAME>\n\ |
| 49 | \n\ |
| 50 | OPTIONS:\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 | |
| 58 | static void listTests() { |
| 59 | printf("Tests: \n"); |
John Reck | e702c9c | 2015-10-07 10:26:02 -0700 | [diff] [blame] | 60 | 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 Reck | b7dd29e | 2015-10-06 13:28:17 -0700 | [diff] [blame] | 85 | } |
| 86 | } |
| 87 | |
| 88 | static 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 | |
| 96 | static const char* SHORT_OPTIONS = "c:r:h"; |
| 97 | |
| 98 | void 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 Reck | e702c9c | 2015-10-07 10:26:02 -0700 | [diff] [blame] | 169 | auto pos = testMap().find(test); |
| 170 | if (pos == testMap().end()) { |
John Reck | b7dd29e | 2015-10-06 13:28:17 -0700 | [diff] [blame] | 171 | 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 Reck | e702c9c | 2015-10-07 10:26:02 -0700 | [diff] [blame] | 178 | gRunTests.push_back(testMap()["shadowgrid"]); |
John Reck | b7dd29e | 2015-10-06 13:28:17 -0700 | [diff] [blame] | 179 | } |
| 180 | } |
| 181 | |
Chris Craik | 0318887 | 2015-02-02 18:39:33 -0800 | [diff] [blame] | 182 | int main(int argc, char* argv[]) { |
John Reck | b7dd29e | 2015-10-06 13:28:17 -0700 | [diff] [blame] | 183 | parseOptions(argc, argv); |
| 184 | |
John Reck | e702c9c | 2015-10-07 10:26:02 -0700 | [diff] [blame] | 185 | BenchmarkOptions opts; |
| 186 | opts.count = gFrameCount; |
John Reck | b7dd29e | 2015-10-06 13:28:17 -0700 | [diff] [blame] | 187 | for (int i = 0; i < gRepeatCount; i++) { |
| 188 | for (auto&& test : gRunTests) { |
John Reck | e702c9c | 2015-10-07 10:26:02 -0700 | [diff] [blame] | 189 | test.functor(opts); |
Tim Murray | 1a0f1c7 | 2015-05-06 11:37:37 -0700 | [diff] [blame] | 190 | } |
| 191 | } |
John Reck | 94c40fe | 2014-10-08 09:28:43 -0700 | [diff] [blame] | 192 | printf("Success!\n"); |
| 193 | return 0; |
| 194 | } |