1 /*
2  * Copyright (C) 2018 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 <procinfo/process_map.h>
18 
19 #include <string.h>
20 #include <sys/types.h>
21 
22 #include <string>
23 
24 #include <android-base/file.h>
25 #include <android-base/logging.h>
26 #include <backtrace/BacktraceMap.h>
27 #include <unwindstack/Maps.h>
28 
29 #include <benchmark/benchmark.h>
30 
BM_ReadMapFile(benchmark::State & state)31 static void BM_ReadMapFile(benchmark::State& state) {
32   std::string map_file = android::base::GetExecutableDirectory() + "/testdata/maps";
33   for (auto _ : state) {
34     std::vector<android::procinfo::MapInfo> maps;
35     android::procinfo::ReadMapFile(
36         map_file, [&](const android::procinfo::MapInfo& mapinfo) { maps.emplace_back(mapinfo); });
37     CHECK_EQ(maps.size(), 2043u);
38   }
39 }
40 BENCHMARK(BM_ReadMapFile);
41 
BM_unwindstack_FileMaps(benchmark::State & state)42 static void BM_unwindstack_FileMaps(benchmark::State& state) {
43   std::string map_file = android::base::GetExecutableDirectory() + "/testdata/maps";
44   for (auto _ : state) {
45     unwindstack::FileMaps maps(map_file);
46     maps.Parse();
47     CHECK_EQ(maps.Total(), 2043u);
48   }
49 }
50 BENCHMARK(BM_unwindstack_FileMaps);
51 
BM_unwindstack_BufferMaps(benchmark::State & state)52 static void BM_unwindstack_BufferMaps(benchmark::State& state) {
53   std::string map_file = android::base::GetExecutableDirectory() + "/testdata/maps";
54   std::string content;
55   CHECK(android::base::ReadFileToString(map_file, &content));
56   for (auto _ : state) {
57     unwindstack::BufferMaps maps(content.c_str());
58     maps.Parse();
59     CHECK_EQ(maps.Total(), 2043u);
60   }
61 }
62 BENCHMARK(BM_unwindstack_BufferMaps);
63 
BM_backtrace_BacktraceMap(benchmark::State & state)64 static void BM_backtrace_BacktraceMap(benchmark::State& state) {
65   pid_t pid = getpid();
66   for (auto _ : state) {
67     BacktraceMap* map = BacktraceMap::Create(pid, true);
68     CHECK(map != nullptr);
69     delete map;
70   }
71 }
72 BENCHMARK(BM_backtrace_BacktraceMap);
73 
74 BENCHMARK_MAIN();
75