| Yao Chen | 48d7518 | 2018-01-23 09:40:48 -0800 | [diff] [blame] | 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 | #include <vector> |
| 17 | #include "benchmark/benchmark.h" |
| 18 | #include "logd/LogEvent.h" |
| 19 | |
| 20 | namespace android { |
| 21 | namespace os { |
| 22 | namespace statsd { |
| 23 | |
| 24 | using std::vector; |
| 25 | |
| 26 | /* Special markers for android_log_list_element type */ |
| 27 | static const char EVENT_TYPE_LIST_STOP = '\n'; /* declare end of list */ |
| 28 | static const char EVENT_TYPE_UNKNOWN = '?'; /* protocol error */ |
| 29 | |
| 30 | static const char EVENT_TYPE_INT = 0; |
| 31 | static const char EVENT_TYPE_LONG = 1; |
| 32 | static const char EVENT_TYPE_STRING = 2; |
| 33 | static const char EVENT_TYPE_LIST = 3; |
| 34 | static const char EVENT_TYPE_FLOAT = 4; |
| 35 | |
| 36 | static const int kLogMsgHeaderSize = 28; |
| 37 | |
| 38 | static void write4Bytes(int val, vector<char>* buffer) { |
| 39 | buffer->push_back(static_cast<char>(val)); |
| 40 | buffer->push_back(static_cast<char>((val >> 8) & 0xFF)); |
| 41 | buffer->push_back(static_cast<char>((val >> 16) & 0xFF)); |
| 42 | buffer->push_back(static_cast<char>((val >> 24) & 0xFF)); |
| 43 | } |
| 44 | |
| 45 | static void getSimpleLogMsgData(log_msg* msg) { |
| 46 | vector<char> buffer; |
| 47 | // stats_log tag id |
| 48 | write4Bytes(1937006964, &buffer); |
| 49 | buffer.push_back(EVENT_TYPE_LIST); |
| 50 | buffer.push_back(2); // field counts; |
| 51 | buffer.push_back(EVENT_TYPE_INT); |
| 52 | write4Bytes(10 /* atom id */, &buffer); |
| 53 | buffer.push_back(EVENT_TYPE_INT); |
| 54 | write4Bytes(99 /* a value to log*/, &buffer); |
| 55 | buffer.push_back(EVENT_TYPE_LIST_STOP); |
| 56 | |
| 57 | msg->entry_v1.len = buffer.size(); |
| 58 | msg->entry.hdr_size = kLogMsgHeaderSize; |
| 59 | msg->entry_v1.sec = time(nullptr); |
| 60 | std::copy(buffer.begin(), buffer.end(), msg->buf + kLogMsgHeaderSize); |
| 61 | } |
| 62 | |
| 63 | static void BM_LogEventCreation(benchmark::State& state) { |
| 64 | log_msg msg; |
| 65 | getSimpleLogMsgData(&msg); |
| 66 | while (state.KeepRunning()) { |
| 67 | benchmark::DoNotOptimize(LogEvent(msg)); |
| 68 | } |
| 69 | } |
| 70 | BENCHMARK(BM_LogEventCreation); |
| 71 | |
| 72 | } // namespace statsd |
| 73 | } // namespace os |
| 74 | } // namespace android |