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 #include <unistd.h> 19 20 #define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_ 21 #include <sys/_system_properties.h> 22 23 #include <vector> 24 #include <string> 25 26 extern void *__system_property_area__; 27 28 #define TEST_NUM_PROPS \ 29 Arg(1)->Arg(4)->Arg(16)->Arg(64)->Arg(128)->Arg(256)->Arg(512)->Arg(1024) 30 31 struct LocalPropertyTestState { 32 LocalPropertyTestState(int nprops) : nprops(nprops), valid(false) { 33 static const char prop_name_chars[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.-_"; 34 35 char dir_template[] = "/data/local/tmp/prop-XXXXXX"; 36 char *dirname = mkdtemp(dir_template); 37 if (!dirname) { 38 perror("making temp file for test state failed (is /data/local/tmp writable?)"); 39 return; 40 } 41 42 old_pa = __system_property_area__; 43 __system_property_area__ = NULL; 44 45 pa_dirname = dirname; 46 pa_filename = pa_dirname + "/__properties__"; 47 48 __system_property_set_filename(pa_filename.c_str()); 49 __system_property_area_init(); 50 51 names = new char* [nprops]; 52 name_lens = new int[nprops]; 53 values = new char* [nprops]; 54 value_lens = new int[nprops]; 55 56 srandom(nprops); 57 58 for (int i = 0; i < nprops; i++) { 59 name_lens[i] = random() % PROP_NAME_MAX; 60 names[i] = new char[PROP_NAME_MAX + 1]; 61 for (int j = 0; j < name_lens[i]; j++) { 62 names[i][j] = prop_name_chars[random() % (sizeof(prop_name_chars) - 1)]; 63 } 64 names[i][name_lens[i]] = 0; 65 value_lens[i] = random() % PROP_VALUE_MAX; 66 values[i] = new char[PROP_VALUE_MAX]; 67 for (int j = 0; j < value_lens[i]; j++) { 68 values[i][j] = prop_name_chars[random() % (sizeof(prop_name_chars) - 1)]; 69 } 70 __system_property_add(names[i], name_lens[i], values[i], value_lens[i]); 71 } 72 73 valid = true; 74 } 75 76 ~LocalPropertyTestState() { 77 if (!valid) 78 return; 79 80 __system_property_area__ = old_pa; 81 82 __system_property_set_filename(PROP_FILENAME); 83 unlink(pa_filename.c_str()); 84 rmdir(pa_dirname.c_str()); 85 86 for (int i = 0; i < nprops; i++) { 87 delete names[i]; 88 delete values[i]; 89 } 90 delete[] names; 91 delete[] name_lens; 92 delete[] values; 93 delete[] value_lens; 94 } 95 public: 96 const int nprops; 97 char **names; 98 int *name_lens; 99 char **values; 100 int *value_lens; 101 bool valid; 102 103 private: 104 std::string pa_dirname; 105 std::string pa_filename; 106 void *old_pa; 107 }; 108 109 static void BM_property_get(int iters, int nprops) 110 { 111 StopBenchmarkTiming(); 112 113 LocalPropertyTestState pa(nprops); 114 char value[PROP_VALUE_MAX]; 115 116 if (!pa.valid) 117 return; 118 119 srandom(iters * nprops); 120 121 StartBenchmarkTiming(); 122 123 for (int i = 0; i < iters; i++) { 124 __system_property_get(pa.names[random() % nprops], value); 125 } 126 StopBenchmarkTiming(); 127 } 128 BENCHMARK(BM_property_get)->TEST_NUM_PROPS; 129 130 static void BM_property_find(int iters, int nprops) 131 { 132 StopBenchmarkTiming(); 133 134 LocalPropertyTestState pa(nprops); 135 136 if (!pa.valid) 137 return; 138 139 srandom(iters * nprops); 140 141 StartBenchmarkTiming(); 142 143 for (int i = 0; i < iters; i++) { 144 __system_property_find(pa.names[random() % nprops]); 145 } 146 StopBenchmarkTiming(); 147 } 148 BENCHMARK(BM_property_find)->TEST_NUM_PROPS; 149