blob: d10be915e2dfb5715dd3ff8010a38ea5d5c7eaee [file] [log] [blame]
Colin Crossb27e2002013-01-28 17:19:43 -08001/*
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"
Greg Hackmanncb215a72013-02-13 14:41:48 -080018#include <unistd.h>
Colin Crossb27e2002013-01-28 17:19:43 -080019
20#define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
21#include <sys/_system_properties.h>
22
23#include <vector>
Greg Hackmanncb215a72013-02-13 14:41:48 -080024#include <string>
Colin Crossb27e2002013-01-28 17:19:43 -080025
Greg Hackmann1540f602013-06-19 13:31:21 -070026extern void *__system_property_area__;
Colin Crossb27e2002013-01-28 17:19:43 -080027
28#define TEST_NUM_PROPS \
Greg Hackmanncb215a72013-02-13 14:41:48 -080029 Arg(1)->Arg(4)->Arg(16)->Arg(64)->Arg(128)->Arg(256)->Arg(512)->Arg(1024)
Colin Crossb27e2002013-01-28 17:19:43 -080030
31struct LocalPropertyTestState {
Greg Hackmanncb215a72013-02-13 14:41:48 -080032 LocalPropertyTestState(int nprops) : nprops(nprops), valid(false) {
Colin Crossb27e2002013-01-28 17:19:43 -080033 static const char prop_name_chars[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.-_";
Greg Hackmanncb215a72013-02-13 14:41:48 -080034
35 char dir_template[] = "/data/nativetest/prop-XXXXXX";
36 char *dirname = mkdtemp(dir_template);
37 if (!dirname) {
38 perror("making temp file for test state failed (is /data/nativetest writable?)");
39 return;
40 }
41
Greg Hackmann1540f602013-06-19 13:31:21 -070042 old_pa = __system_property_area__;
43 __system_property_area__ = NULL;
Greg Hackmanncb215a72013-02-13 14:41:48 -080044
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();
Colin Crossb27e2002013-01-28 17:19:43 -080050
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 }
Greg Hackmanncb215a72013-02-13 14:41:48 -080072
73 valid = true;
Colin Crossb27e2002013-01-28 17:19:43 -080074 }
75
76 ~LocalPropertyTestState() {
Greg Hackmanncb215a72013-02-13 14:41:48 -080077 if (!valid)
78 return;
79
Greg Hackmann1540f602013-06-19 13:31:21 -070080 __system_property_area__ = old_pa;
Greg Hackmanncb215a72013-02-13 14:41:48 -080081
82 __system_property_set_filename(PROP_FILENAME);
83 unlink(pa_filename.c_str());
84 rmdir(pa_dirname.c_str());
85
Colin Crossb27e2002013-01-28 17:19:43 -080086 for (int i = 0; i < nprops; i++) {
87 delete names[i];
88 delete values[i];
89 }
Colin Cross7d068132013-06-18 13:08:28 -070090 delete[] names;
91 delete[] name_lens;
92 delete[] values;
93 delete[] value_lens;
Colin Crossb27e2002013-01-28 17:19:43 -080094 }
95public:
96 const int nprops;
97 char **names;
98 int *name_lens;
99 char **values;
100 int *value_lens;
Greg Hackmanncb215a72013-02-13 14:41:48 -0800101 bool valid;
Colin Crossb27e2002013-01-28 17:19:43 -0800102
103private:
Greg Hackmanncb215a72013-02-13 14:41:48 -0800104 std::string pa_dirname;
105 std::string pa_filename;
Greg Hackmann1540f602013-06-19 13:31:21 -0700106 void *old_pa;
Colin Crossb27e2002013-01-28 17:19:43 -0800107};
108
109static void BM_property_get(int iters, int nprops)
110{
111 StopBenchmarkTiming();
112
113 LocalPropertyTestState pa(nprops);
114 char value[PROP_VALUE_MAX];
115
Greg Hackmanncb215a72013-02-13 14:41:48 -0800116 if (!pa.valid)
117 return;
118
Colin Cross7d90cfa2013-06-18 12:47:38 -0700119 srandom(iters * nprops);
120
Colin Crossb27e2002013-01-28 17:19:43 -0800121 StartBenchmarkTiming();
122
123 for (int i = 0; i < iters; i++) {
Colin Cross7d90cfa2013-06-18 12:47:38 -0700124 __system_property_get(pa.names[random() % nprops], value);
Colin Crossb27e2002013-01-28 17:19:43 -0800125 }
126 StopBenchmarkTiming();
127}
128BENCHMARK(BM_property_get)->TEST_NUM_PROPS;
129
130static void BM_property_find(int iters, int nprops)
131{
132 StopBenchmarkTiming();
133
134 LocalPropertyTestState pa(nprops);
135
Greg Hackmanncb215a72013-02-13 14:41:48 -0800136 if (!pa.valid)
137 return;
138
Colin Cross7d90cfa2013-06-18 12:47:38 -0700139 srandom(iters * nprops);
140
Colin Crossb27e2002013-01-28 17:19:43 -0800141 StartBenchmarkTiming();
142
143 for (int i = 0; i < iters; i++) {
Colin Cross7d90cfa2013-06-18 12:47:38 -0700144 __system_property_find(pa.names[random() % nprops]);
Colin Crossb27e2002013-01-28 17:19:43 -0800145 }
146 StopBenchmarkTiming();
147}
148BENCHMARK(BM_property_find)->TEST_NUM_PROPS;