1 /*
2 * Copyright 2017 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 //#define LOG_NDEBUG 0
18 #define LOG_TAG "audio_utils_powerlog_tests"
19
20 #include <audio_utils/PowerLog.h>
21
22 #include <audio_utils/clock.h>
23 #include <gtest/gtest.h>
24 #include <iostream>
25 #include <log/log.h>
26
27 using namespace android;
28
countNewLines(const std::string & s)29 static size_t countNewLines(const std::string &s) {
30 return std::count(s.begin(), s.end(), '\n');
31 }
32
TEST(audio_utils_powerlog,basic_level_1)33 TEST(audio_utils_powerlog, basic_level_1) {
34 auto plog = std::make_unique<PowerLog>(
35 48000 /* sampleRate */,
36 1 /* channelCount */,
37 AUDIO_FORMAT_PCM_16_BIT,
38 100 /* entries */,
39 1 /* framesPerEntry */,
40 1 /* levels */);
41
42 // header
43 EXPECT_EQ((size_t)1, countNewLines(plog->dumpToString()));
44
45 const int16_t zero = 0;
46 const int16_t half = 0x4000;
47
48 plog->log(&half, 1 /* frame */, 0 /* nowNs */);
49 plog->log(&half, 1 /* frame */, 1 /* nowNs */);
50 plog->log(&half, 1 /* frame */, 2 /* nowNs */);
51
52 // one line / signal
53 EXPECT_EQ((size_t)2, countNewLines(plog->dumpToString(
54 "" /* prefix */, 0 /* lines */, 0 /* limitNs */, false /* logPlot */)));
55
56 // one line / signal + logplot
57 EXPECT_EQ((size_t)20, countNewLines(plog->dumpToString()));
58
59 plog->log(&zero, 1 /* frame */, 3 /* nowNs */);
60 // zero termination doesn't change this.
61 EXPECT_EQ((size_t)20, countNewLines(plog->dumpToString()));
62
63 // but adding next line does.
64 plog->log(&half, 1 /* frame */, 4 /* nowNs */);
65 EXPECT_EQ((size_t)21, countNewLines(plog->dumpToString()));
66
67 // truncating on lines (this does not include the logplot).
68 EXPECT_EQ((size_t)20, countNewLines(plog->dumpToString(
69 "" /* prefix */, 2 /* lines */)));
70
71 // truncating on time as well.
72 EXPECT_EQ((size_t)21, countNewLines(plog->dumpToString(
73 "" /* prefix */, 0 /* lines */, 2 /* limitNs */)));
74 // truncating on different time limit.
75 EXPECT_EQ((size_t)20, countNewLines(plog->dumpToString(
76 "" /* prefix */, 0 /* lines */, 3 /* limitNs */)));
77
78 // truncating on a larger line count (this doesn't include the logplot).
79 EXPECT_EQ((size_t)21, countNewLines(plog->dumpToString(
80 "" /* prefix */, 3 /* lines */, 2 /* limitNs */)));
81
82 plog->dump(0 /* fd (stdout) */);
83
84 // The output below depends on the local time zone.
85 // The indentation below is exact, check alignment.
86 /*
87 Signal power history:
88 01-01 00:00:00.000: [ -6.0 -6.0 -6.0 ] sum(-1.2)
89 01-01 00:00:00.000: [ -6.0
90
91 -0.0 -| |
92 -1.0 -| |
93 -2.0 -| |
94 -3.0 -| |
95 -4.0 -| |
96 -5.0 -| |
97 -6.0 -|***|
98 -7.0 -| |
99 -8.0 -| |
100 -9.0 -| |
101 -10.0 -| |
102 -11.0 -| |
103 -12.0 -| |
104 -13.0 -| |
105 |____
106
107 */
108 }
109
TEST(audio_utils_powerlog,basic_level_2)110 TEST(audio_utils_powerlog, basic_level_2) {
111 const uint32_t kSampleRate = 48000;
112 auto plog = std::make_unique<PowerLog>(
113 kSampleRate /* sampleRate */,
114 1 /* channelCount */,
115 AUDIO_FORMAT_PCM_16_BIT,
116 200 /* entries */,
117 1 /* framesPerEntry */,
118 2 /* levels */);
119
120 // header
121 EXPECT_EQ((size_t)2, countNewLines(plog->dumpToString()));
122
123 const int16_t zero = 0;
124 const int16_t half = 0x4000;
125 const std::vector<int16_t> ary(60, 0x1000);
126
127 plog->log(&half, 1 /* frame */, 0 /* nowNs */);
128 plog->log(&half, 1 /* frame */, 1 * NANOS_PER_SECOND / kSampleRate);
129 plog->log(&half, 1 /* frame */, 2 * NANOS_PER_SECOND / kSampleRate);
130 plog->log(ary.data(), ary.size(), 30 * NANOS_PER_SECOND / kSampleRate);
131
132 EXPECT_EQ((size_t)10, countNewLines(plog->dumpToString(
133 "" /* prefix */, 0 /* lines */, 0 /* limitNs */, false /* logPlot */)));
134
135 // add logplot
136 EXPECT_EQ((size_t)28, countNewLines(plog->dumpToString()));
137
138 plog->log(&zero, 1 /* frame */, 100 * NANOS_PER_SECOND / kSampleRate);
139 // zero termination doesn't change this.
140 EXPECT_EQ((size_t)28, countNewLines(plog->dumpToString()));
141
142 // but adding next line does.
143 plog->log(&half, 1 /* frame */, 101 * NANOS_PER_SECOND / kSampleRate);
144 EXPECT_EQ((size_t)29, countNewLines(plog->dumpToString()));
145
146 // truncating on lines (this does not include the logplot).
147 EXPECT_EQ((size_t)22, countNewLines(plog->dumpToString(
148 "" /* prefix */, 4 /* lines */)));
149
150 // truncating on time as well.
151 EXPECT_EQ((size_t)29, countNewLines(plog->dumpToString(
152 "" /* prefix */, 0 /* lines */, 2 /* limitNs */)));
153 // truncating on different time limit.
154 EXPECT_EQ((size_t)29, countNewLines(plog->dumpToString(
155 "" /* prefix */, 0 /* lines */, 3 /* limitNs */)));
156
157 // truncating on a larger line count (this doesn't include the logplot).
158 EXPECT_EQ((size_t)21, countNewLines(plog->dumpToString(
159 "" /* prefix */, 3 /* lines */, 2 /* limitNs */)));
160
161 plog->dump(0 /* fd (stdout) */);
162
163 // The output below depends on the local time zone.
164 // The indentation below is exact, check alignment.
165 /*
166 Signal power history (resolution: 0.4 ms):
167 12-31 16:00:00.000: [ -14.3 -18.1 -18.1 -18.1
168 Signal power history (resolution: 0.0 ms):
169 12-31 16:00:00.000: [ -6.0 -6.0 -6.0 -18.1 -18.1 -18.1 -18.1 -18.1 -18.1 -18.1
170 12-31 16:00:00.000: -18.1 -18.1 -18.1 -18.1 -18.1 -18.1 -18.1 -18.1 -18.1 -18.1
171 12-31 16:00:00.000: -18.1 -18.1 -18.1 -18.1 -18.1 -18.1 -18.1 -18.1 -18.1 -18.1
172 12-31 16:00:00.001: -18.1 -18.1 -18.1 -18.1 -18.1 -18.1 -18.1 -18.1 -18.1 -18.1
173 12-31 16:00:00.001: -18.1 -18.1 -18.1 -18.1 -18.1 -18.1 -18.1 -18.1 -18.1 -18.1
174 12-31 16:00:00.001: -18.1 -18.1 -18.1 -18.1 -18.1 -18.1 -18.1 -18.1 -18.1 -18.1
175 12-31 16:00:00.001: -18.1 -18.1 -18.1 ] sum(2.3)
176 12-31 16:00:00.002: [ -6.0
177
178 -6.0 -|*** |
179 -7.0 -| |
180 -8.0 -| |
181 -9.0 -| |
182 -10.0 -| |
183 -11.0 -| |
184 -12.0 -| |
185 -13.0 -| |
186 -14.0 -| |
187 -15.0 -| |
188 -16.0 -| |
189 -17.0 -| |
190 -18.0 -| ************************************************************|
191 -19.0 -| |
192 |________________________________________________________________
193 */
194 }
195
TEST(audio_utils_powerlog,c)196 TEST(audio_utils_powerlog, c) {
197 power_log_t *power_log = power_log_create(
198 48000 /* sample_rate */,
199 1 /* channel_count */,
200 AUDIO_FORMAT_PCM_16_BIT,
201 100 /* entries */,
202 1 /* frames_per_entry */);
203
204 // soundness test
205 const int16_t zero = 0;
206 const int16_t quarter = 0x2000;
207
208 power_log_log(power_log, &quarter, 1 /* frame */, 0 /* now_ns */);
209 power_log_log(power_log, &zero, 1 /* frame */, 1 /* now_ns */);
210 power_log_dump(power_log, 0 /* fd */, " " /* prefix */, 0 /* lines */, 0 /* limit_ns */);
211 power_log_destroy(power_log);
212
213 // This has a 2 character prefix offset from the previous test when dumping.
214 // The indentation below is exact, check alignment.
215 /*
216 Signal power history:
217 12-31 16:00:00.000: [ -12.0 ] sum(-12.0)
218 */
219 }
220