property_benchmark.cpp revision 28417e6314768d057ab7ad7a0208f1af7597b4d6
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 <errno.h>
19#include <stdio.h>
20#include <stdlib.h>
21#include <unistd.h>
22
23#define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
24#include <sys/_system_properties.h>
25
26#include <vector>
27#include <string>
28
29extern void *__system_property_area__;
30
31#define TEST_NUM_PROPS \
32    Arg(1)->Arg(4)->Arg(16)->Arg(64)->Arg(128)->Arg(256)->Arg(512)->Arg(1024)
33
34struct LocalPropertyTestState {
35    LocalPropertyTestState(int nprops) : nprops(nprops), valid(false) {
36        static const char prop_name_chars[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.-_";
37
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        }
51
52        old_pa = __system_property_area__;
53        __system_property_area__ = NULL;
54
55        pa_dirname = dirname;
56        pa_filename = pa_dirname + "/__properties__";
57
58        __system_property_set_filename(pa_filename.c_str());
59        __system_property_area_init();
60
61        names = new char* [nprops];
62        name_lens = new int[nprops];
63        values = new char* [nprops];
64        value_lens = new int[nprops];
65
66        srandom(nprops);
67
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        }
82
83        valid = true;
84    }
85
86    ~LocalPropertyTestState() {
87        if (!valid)
88            return;
89
90        __system_property_area__ = old_pa;
91
92        __system_property_set_filename(PROP_FILENAME);
93        unlink(pa_filename.c_str());
94        rmdir(pa_dirname.c_str());
95
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    }
105public:
106    const int nprops;
107    char **names;
108    int *name_lens;
109    char **values;
110    int *value_lens;
111    bool valid;
112
113private:
114    std::string pa_dirname;
115    std::string pa_filename;
116    void *old_pa;
117};
118
119static void BM_property_get(int iters, int nprops)
120{
121    StopBenchmarkTiming();
122
123    LocalPropertyTestState pa(nprops);
124    char value[PROP_VALUE_MAX];
125
126    if (!pa.valid)
127        return;
128
129    srandom(iters * nprops);
130
131    StartBenchmarkTiming();
132
133    for (int i = 0; i < iters; i++) {
134        __system_property_get(pa.names[random() % nprops], value);
135    }
136    StopBenchmarkTiming();
137}
138BENCHMARK(BM_property_get)->TEST_NUM_PROPS;
139
140static void BM_property_find(int iters, int nprops)
141{
142    StopBenchmarkTiming();
143
144    LocalPropertyTestState pa(nprops);
145
146    if (!pa.valid)
147        return;
148
149    srandom(iters * nprops);
150
151    StartBenchmarkTiming();
152
153    for (int i = 0; i < iters; i++) {
154        __system_property_find(pa.names[random() % nprops]);
155    }
156    StopBenchmarkTiming();
157}
158BENCHMARK(BM_property_find)->TEST_NUM_PROPS;
159
160static void BM_property_read(int iters, int nprops)
161{
162    StopBenchmarkTiming();
163
164    LocalPropertyTestState pa(nprops);
165
166    if (!pa.valid)
167        return;
168
169    srandom(iters * nprops);
170    const prop_info** pinfo = new const prop_info*[iters];
171    char propvalue[PROP_VALUE_MAX];
172
173    for (int i = 0; i < iters; i++) {
174        pinfo[i] = __system_property_find(pa.names[random() % nprops]);
175    }
176
177    StartBenchmarkTiming();
178    for (int i = 0; i < iters; i++) {
179        __system_property_read(pinfo[i], 0, propvalue);
180    }
181    StopBenchmarkTiming();
182
183    delete[] pinfo;
184}
185BENCHMARK(BM_property_read)->TEST_NUM_PROPS;
186
187static void BM_property_serial(int iters, int nprops)
188{
189    StopBenchmarkTiming();
190
191    LocalPropertyTestState pa(nprops);
192
193    if (!pa.valid)
194        return;
195
196    srandom(iters * nprops);
197    const prop_info** pinfo = new const prop_info*[iters];
198
199    for (int i = 0; i < iters; i++) {
200        pinfo[i] = __system_property_find(pa.names[random() % nprops]);
201    }
202
203    StartBenchmarkTiming();
204    for (int i = 0; i < iters; i++) {
205        __system_property_serial(pinfo[i]);
206    }
207    StopBenchmarkTiming();
208
209    delete[] pinfo;
210}
211BENCHMARK(BM_property_serial)->TEST_NUM_PROPS;
212