Colin Cross | b27e200 | 2013-01-28 17:19:43 -0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2012 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 "benchmark.h" |
| 18 | |
| 19 | #define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_ |
| 20 | #include <sys/_system_properties.h> |
| 21 | |
| 22 | #include <vector> |
| 23 | |
| 24 | extern void *__system_property_area__; |
| 25 | |
| 26 | #define TEST_NUM_PROPS \ |
| 27 | Arg(1)->Arg(4)->Arg(16)->Arg(64)->Arg(128)->Arg(247) |
| 28 | |
| 29 | struct LocalPropertyTestState { |
| 30 | LocalPropertyTestState(int nprops) : nprops(nprops) { |
| 31 | static const char prop_name_chars[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.-_"; |
| 32 | old_pa = __system_property_area__; |
| 33 | pa = malloc(PA_SIZE); |
| 34 | __system_property_area_init(pa); |
| 35 | |
| 36 | names = new char* [nprops]; |
| 37 | name_lens = new int[nprops]; |
| 38 | values = new char* [nprops]; |
| 39 | value_lens = new int[nprops]; |
| 40 | |
| 41 | srandom(nprops); |
| 42 | |
| 43 | for (int i = 0; i < nprops; i++) { |
| 44 | name_lens[i] = random() % PROP_NAME_MAX; |
| 45 | names[i] = new char[PROP_NAME_MAX + 1]; |
| 46 | for (int j = 0; j < name_lens[i]; j++) { |
| 47 | names[i][j] = prop_name_chars[random() % (sizeof(prop_name_chars) - 1)]; |
| 48 | } |
| 49 | names[i][name_lens[i]] = 0; |
| 50 | value_lens[i] = random() % PROP_VALUE_MAX; |
| 51 | values[i] = new char[PROP_VALUE_MAX]; |
| 52 | for (int j = 0; j < value_lens[i]; j++) { |
| 53 | values[i][j] = prop_name_chars[random() % (sizeof(prop_name_chars) - 1)]; |
| 54 | } |
| 55 | __system_property_add(names[i], name_lens[i], values[i], value_lens[i]); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | ~LocalPropertyTestState() { |
| 60 | __system_property_area__ = old_pa; |
| 61 | for (int i = 0; i < nprops; i++) { |
| 62 | delete names[i]; |
| 63 | delete values[i]; |
| 64 | } |
| 65 | delete names; |
| 66 | delete name_lens; |
| 67 | delete values; |
| 68 | delete value_lens; |
| 69 | free(pa); |
| 70 | } |
| 71 | public: |
| 72 | const int nprops; |
| 73 | char **names; |
| 74 | int *name_lens; |
| 75 | char **values; |
| 76 | int *value_lens; |
| 77 | |
| 78 | private: |
| 79 | void *pa; |
| 80 | void *old_pa; |
| 81 | }; |
| 82 | |
| 83 | static void BM_property_get(int iters, int nprops) |
| 84 | { |
| 85 | StopBenchmarkTiming(); |
| 86 | |
| 87 | LocalPropertyTestState pa(nprops); |
| 88 | char value[PROP_VALUE_MAX]; |
| 89 | |
| 90 | StartBenchmarkTiming(); |
| 91 | |
| 92 | for (int i = 0; i < iters; i++) { |
| 93 | for (int j = 0; j < nprops; j++) { |
| 94 | __system_property_get(pa.names[j], value); |
| 95 | } |
| 96 | } |
| 97 | StopBenchmarkTiming(); |
| 98 | } |
| 99 | BENCHMARK(BM_property_get)->TEST_NUM_PROPS; |
| 100 | |
| 101 | static void BM_property_find(int iters, int nprops) |
| 102 | { |
| 103 | StopBenchmarkTiming(); |
| 104 | |
| 105 | LocalPropertyTestState pa(nprops); |
| 106 | |
| 107 | StartBenchmarkTiming(); |
| 108 | |
| 109 | for (int i = 0; i < iters; i++) { |
| 110 | for (int j = 0; j < nprops; j++) { |
| 111 | __system_property_find(pa.names[j]); |
| 112 | } |
| 113 | } |
| 114 | StopBenchmarkTiming(); |
| 115 | } |
| 116 | BENCHMARK(BM_property_find)->TEST_NUM_PROPS; |