1 /*
2 * Copyright (C) 2022 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_TAG "mediautils_scopedstatistics_tests"
18
19 #include <mediautils/ScopedStatistics.h>
20
21 #include <atomic>
22 #include <chrono>
23 #include <gtest/gtest.h>
24 #include <thread>
25 #include <utils/Log.h>
26
27 using namespace android::mediautils;
28 using namespace std::chrono_literals;
29
TEST(mediautils_scopedstatistics_tests,basic)30 TEST(mediautils_scopedstatistics_tests, basic) {
31 auto methodStatistics = std::make_shared<MethodStatistics<std::string>>();
32 std::string METHOD_NAME{"MyMethod"};
33
34 // no stats before
35 auto empty = methodStatistics->getStatistics(METHOD_NAME);
36 ASSERT_EQ(0, empty.getN());
37
38 // create a scoped statistics object.
39 {
40 ScopedStatistics scopedStatistics(METHOD_NAME, methodStatistics);
41
42 std::this_thread::sleep_for(100ms);
43 }
44
45 // check that some stats were logged.
46 auto stats = methodStatistics->getStatistics(METHOD_NAME);
47 ASSERT_EQ(1, stats.getN());
48 auto mean = stats.getMean();
49
50 // mean should be about 100ms, but to avoid false failures,
51 // we check 50ms < mean < 300ms.
52 ASSERT_GT(mean, 50.); // took more than 50ms.
53 ASSERT_LT(mean, 300.); // took less than 300ms.
54 }
55