blob: 0af8d6551fada644cc0b52fbc5768a40b1e6d7fb [file] [log] [blame]
alexander3c798932021-03-26 21:42:19 +00001/*
Liam Barrycca34a92023-11-03 15:49:34 +00002 * SPDX-FileCopyrightText: Copyright 2021,2023 Arm Limited and/or its affiliates <open-source-office@arm.com>
alexander3c798932021-03-26 21:42:19 +00003 * SPDX-License-Identifier: Apache-2.0
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17#include "Profiler.hpp"
18
19#include "AppContext.hpp"
20#include "TensorFlowLiteMicro.hpp"
21
22#include <catch.hpp>
23#include <iostream>
24
25
26TEST_CASE("Common: Test Profiler")
27{
Kshitij Sisodia4cc40212022-04-08 09:54:53 +010028 hal_platform_init();
alexander3c798932021-03-26 21:42:19 +000029
Kshitij Sisodia4cc40212022-04-08 09:54:53 +010030 SECTION("Test default construction") {
31 arm::app::Profiler profiler{};
32 REQUIRE(true == profiler.StartProfiling());
33 REQUIRE(true == profiler.StopProfiling());
Isabella Gottardi8df12f32021-04-07 17:15:31 +010034 }
alexander3c798932021-03-26 21:42:19 +000035
Isabella Gottardi8df12f32021-04-07 17:15:31 +010036 SECTION("Test valid profiler") {
Kshitij Sisodia4cc40212022-04-08 09:54:53 +010037 arm::app::Profiler profilerValid{"test_valid"};
Isabella Gottardi8df12f32021-04-07 17:15:31 +010038 REQUIRE(true == profilerValid.StartProfiling());
39 REQUIRE(true == profilerValid.StopProfiling());
40 std::vector<arm::app::ProfileResult> results;
41 profilerValid.GetAllResultsAndReset(results);
42 REQUIRE(results.size() == 1);
43 REQUIRE(results[0].name == "test_valid");
Liam Barryf30d7412023-09-27 12:48:35 +010044
45 /* Improper usage should cause failure.
46 * Note: Errors or warnings generated by this test will appear in output of any subsequent
Liam Barrycca34a92023-11-03 15:49:34 +000047 * failing tests causing misleading output unless logging is disabled.*/
Isabella Gottardi8df12f32021-04-07 17:15:31 +010048 REQUIRE(false == profilerValid.StopProfiling()); /* We need to start it first */
Liam Barryf30d7412023-09-27 12:48:35 +010049
Isabella Gottardi8df12f32021-04-07 17:15:31 +010050 REQUIRE(true == profilerValid.StartProfiling()); /* Should be able to start it fine */
51 REQUIRE(false == profilerValid.StartProfiling()); /* Can't restart it without resetting */
52 profilerValid.Reset();
53 REQUIRE(true == profilerValid.StartProfiling()); /* Can start it again now.. */
54 REQUIRE(true == profilerValid.StopProfiling());
55 }
alexander3c798932021-03-26 21:42:19 +000056
Isabella Gottardi8df12f32021-04-07 17:15:31 +010057 SECTION("Test multiple profilers") {
Kshitij Sisodia4cc40212022-04-08 09:54:53 +010058 arm::app::Profiler profilerValid{"one"};
Isabella Gottardi8df12f32021-04-07 17:15:31 +010059 REQUIRE(true == profilerValid.StartProfiling());
60 REQUIRE(true == profilerValid.StopProfiling());
alexander3c798932021-03-26 21:42:19 +000061
Isabella Gottardi8df12f32021-04-07 17:15:31 +010062 REQUIRE(true == profilerValid.StartProfiling("two"));
63 REQUIRE(true == profilerValid.StopProfiling());
64 REQUIRE(true == profilerValid.StartProfiling("two"));
65 REQUIRE(true == profilerValid.StopProfiling());
alexander3c798932021-03-26 21:42:19 +000066
Isabella Gottardi8df12f32021-04-07 17:15:31 +010067 std::vector<arm::app::ProfileResult> results;
68 profilerValid.GetAllResultsAndReset(results);
69 REQUIRE(results.size() == 2);
70 REQUIRE(results[0].name == "one");
71 REQUIRE(results[0].samplesNum == 1);
72 REQUIRE(results[1].name == "two");
73 REQUIRE(results[1].samplesNum == 2);
74 }
75
76#if defined (CPU_PROFILE_ENABLED)
77 SECTION("Test CPU profiler") {
78
Kshitij Sisodia4cc40212022-04-08 09:54:53 +010079 arm::app::Profiler profilerCPU{"test cpu"};
Isabella Gottardi8df12f32021-04-07 17:15:31 +010080 std::vector<arm::app::ProfileResult> results;
81 profilerCPU.StartProfiling();
82 profilerCPU.StopProfiling();
83 profilerCPU.GetAllResultsAndReset(results);
84 REQUIRE(results.size() == 1);
85 bool foundTime = false;
86 bool foundCPU_ACTIVE = false;
87 for(arm::app::Statistics& stat: results[0].data) {
88
89 if (!foundTime) {
90 foundTime = stat.name == "Time";
91 }
92
93 if (!foundCPU_ACTIVE) {
94 foundCPU_ACTIVE = stat.name == "CPU ACTIVE";
95 }
96
97 }
98 REQUIRE(foundTime);
99 REQUIRE(foundCPU_ACTIVE);
100 }
101#endif /* defined (CPU_PROFILE_ENABLED) */
102}