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