2013-01-29 02:19:43 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2012 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "benchmark.h"
|
2013-02-13 23:41:48 +01:00
|
|
|
#include <unistd.h>
|
2013-01-29 02:19:43 +01:00
|
|
|
|
|
|
|
#define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
|
|
|
|
#include <sys/_system_properties.h>
|
|
|
|
|
|
|
|
#include <vector>
|
2013-02-13 23:41:48 +01:00
|
|
|
#include <string>
|
2013-01-29 02:19:43 +01:00
|
|
|
|
2013-06-19 22:31:21 +02:00
|
|
|
extern void *__system_property_area__;
|
2013-01-29 02:19:43 +01:00
|
|
|
|
|
|
|
#define TEST_NUM_PROPS \
|
2013-02-13 23:41:48 +01:00
|
|
|
Arg(1)->Arg(4)->Arg(16)->Arg(64)->Arg(128)->Arg(256)->Arg(512)->Arg(1024)
|
2013-01-29 02:19:43 +01:00
|
|
|
|
|
|
|
struct LocalPropertyTestState {
|
2013-02-13 23:41:48 +01:00
|
|
|
LocalPropertyTestState(int nprops) : nprops(nprops), valid(false) {
|
2013-01-29 02:19:43 +01:00
|
|
|
static const char prop_name_chars[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.-_";
|
2013-02-13 23:41:48 +01:00
|
|
|
|
2013-08-15 21:54:40 +02:00
|
|
|
char dir_template[] = "/data/local/tmp/prop-XXXXXX";
|
2013-02-13 23:41:48 +01:00
|
|
|
char *dirname = mkdtemp(dir_template);
|
|
|
|
if (!dirname) {
|
2013-08-15 21:54:40 +02:00
|
|
|
perror("making temp file for test state failed (is /data/local/tmp writable?)");
|
2013-02-13 23:41:48 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-06-19 22:31:21 +02:00
|
|
|
old_pa = __system_property_area__;
|
|
|
|
__system_property_area__ = NULL;
|
2013-02-13 23:41:48 +01:00
|
|
|
|
|
|
|
pa_dirname = dirname;
|
|
|
|
pa_filename = pa_dirname + "/__properties__";
|
|
|
|
|
|
|
|
__system_property_set_filename(pa_filename.c_str());
|
|
|
|
__system_property_area_init();
|
2013-01-29 02:19:43 +01:00
|
|
|
|
|
|
|
names = new char* [nprops];
|
|
|
|
name_lens = new int[nprops];
|
|
|
|
values = new char* [nprops];
|
|
|
|
value_lens = new int[nprops];
|
|
|
|
|
|
|
|
srandom(nprops);
|
|
|
|
|
|
|
|
for (int i = 0; i < nprops; i++) {
|
|
|
|
name_lens[i] = random() % PROP_NAME_MAX;
|
|
|
|
names[i] = new char[PROP_NAME_MAX + 1];
|
|
|
|
for (int j = 0; j < name_lens[i]; j++) {
|
|
|
|
names[i][j] = prop_name_chars[random() % (sizeof(prop_name_chars) - 1)];
|
|
|
|
}
|
|
|
|
names[i][name_lens[i]] = 0;
|
|
|
|
value_lens[i] = random() % PROP_VALUE_MAX;
|
|
|
|
values[i] = new char[PROP_VALUE_MAX];
|
|
|
|
for (int j = 0; j < value_lens[i]; j++) {
|
|
|
|
values[i][j] = prop_name_chars[random() % (sizeof(prop_name_chars) - 1)];
|
|
|
|
}
|
|
|
|
__system_property_add(names[i], name_lens[i], values[i], value_lens[i]);
|
|
|
|
}
|
2013-02-13 23:41:48 +01:00
|
|
|
|
|
|
|
valid = true;
|
2013-01-29 02:19:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
~LocalPropertyTestState() {
|
2013-02-13 23:41:48 +01:00
|
|
|
if (!valid)
|
|
|
|
return;
|
|
|
|
|
2013-06-19 22:31:21 +02:00
|
|
|
__system_property_area__ = old_pa;
|
2013-02-13 23:41:48 +01:00
|
|
|
|
|
|
|
__system_property_set_filename(PROP_FILENAME);
|
|
|
|
unlink(pa_filename.c_str());
|
|
|
|
rmdir(pa_dirname.c_str());
|
|
|
|
|
2013-01-29 02:19:43 +01:00
|
|
|
for (int i = 0; i < nprops; i++) {
|
|
|
|
delete names[i];
|
|
|
|
delete values[i];
|
|
|
|
}
|
2013-06-18 22:08:28 +02:00
|
|
|
delete[] names;
|
|
|
|
delete[] name_lens;
|
|
|
|
delete[] values;
|
|
|
|
delete[] value_lens;
|
2013-01-29 02:19:43 +01:00
|
|
|
}
|
|
|
|
public:
|
|
|
|
const int nprops;
|
|
|
|
char **names;
|
|
|
|
int *name_lens;
|
|
|
|
char **values;
|
|
|
|
int *value_lens;
|
2013-02-13 23:41:48 +01:00
|
|
|
bool valid;
|
2013-01-29 02:19:43 +01:00
|
|
|
|
|
|
|
private:
|
2013-02-13 23:41:48 +01:00
|
|
|
std::string pa_dirname;
|
|
|
|
std::string pa_filename;
|
2013-06-19 22:31:21 +02:00
|
|
|
void *old_pa;
|
2013-01-29 02:19:43 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
static void BM_property_get(int iters, int nprops)
|
|
|
|
{
|
|
|
|
StopBenchmarkTiming();
|
|
|
|
|
|
|
|
LocalPropertyTestState pa(nprops);
|
|
|
|
char value[PROP_VALUE_MAX];
|
|
|
|
|
2013-02-13 23:41:48 +01:00
|
|
|
if (!pa.valid)
|
|
|
|
return;
|
|
|
|
|
2013-06-18 21:47:38 +02:00
|
|
|
srandom(iters * nprops);
|
|
|
|
|
2013-01-29 02:19:43 +01:00
|
|
|
StartBenchmarkTiming();
|
|
|
|
|
|
|
|
for (int i = 0; i < iters; i++) {
|
2013-06-18 21:47:38 +02:00
|
|
|
__system_property_get(pa.names[random() % nprops], value);
|
2013-01-29 02:19:43 +01:00
|
|
|
}
|
|
|
|
StopBenchmarkTiming();
|
|
|
|
}
|
|
|
|
BENCHMARK(BM_property_get)->TEST_NUM_PROPS;
|
|
|
|
|
|
|
|
static void BM_property_find(int iters, int nprops)
|
|
|
|
{
|
|
|
|
StopBenchmarkTiming();
|
|
|
|
|
|
|
|
LocalPropertyTestState pa(nprops);
|
|
|
|
|
2013-02-13 23:41:48 +01:00
|
|
|
if (!pa.valid)
|
|
|
|
return;
|
|
|
|
|
2013-06-18 21:47:38 +02:00
|
|
|
srandom(iters * nprops);
|
|
|
|
|
2013-01-29 02:19:43 +01:00
|
|
|
StartBenchmarkTiming();
|
|
|
|
|
|
|
|
for (int i = 0; i < iters; i++) {
|
2013-06-18 21:47:38 +02:00
|
|
|
__system_property_find(pa.names[random() % nprops]);
|
2013-01-29 02:19:43 +01:00
|
|
|
}
|
|
|
|
StopBenchmarkTiming();
|
|
|
|
}
|
|
|
|
BENCHMARK(BM_property_find)->TEST_NUM_PROPS;
|