blob: 0b487e54b4662a0e5735682f7503bcbd4302287d [file] [log] [blame]
alexander3c798932021-03-26 21:42:19 +00001/*
Liam Barryf30d7412023-09-27 12:48:35 +01002 * 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
47 * failing tests causing misleading output. Give warning until solution is found */
48 printf("Invalid profiler usage common test output:\n");
Isabella Gottardi8df12f32021-04-07 17:15:31 +010049 REQUIRE(false == profilerValid.StopProfiling()); /* We need to start it first */
Liam Barryf30d7412023-09-27 12:48:35 +010050
Isabella Gottardi8df12f32021-04-07 17:15:31 +010051 REQUIRE(true == profilerValid.StartProfiling()); /* Should be able to start it fine */
52 REQUIRE(false == profilerValid.StartProfiling()); /* Can't restart it without resetting */
Liam Barryf30d7412023-09-27 12:48:35 +010053 printf("End of Invalid profiler usage common test output. \nERROR messages above this line "
54 "are expected and can be ignored.\n\n");
Isabella Gottardi8df12f32021-04-07 17:15:31 +010055 profilerValid.Reset();
56 REQUIRE(true == profilerValid.StartProfiling()); /* Can start it again now.. */
57 REQUIRE(true == profilerValid.StopProfiling());
58 }
alexander3c798932021-03-26 21:42:19 +000059
Isabella Gottardi8df12f32021-04-07 17:15:31 +010060 SECTION("Test multiple profilers") {
Kshitij Sisodia4cc40212022-04-08 09:54:53 +010061 arm::app::Profiler profilerValid{"one"};
Isabella Gottardi8df12f32021-04-07 17:15:31 +010062 REQUIRE(true == profilerValid.StartProfiling());
63 REQUIRE(true == profilerValid.StopProfiling());
alexander3c798932021-03-26 21:42:19 +000064
Isabella Gottardi8df12f32021-04-07 17:15:31 +010065 REQUIRE(true == profilerValid.StartProfiling("two"));
66 REQUIRE(true == profilerValid.StopProfiling());
67 REQUIRE(true == profilerValid.StartProfiling("two"));
68 REQUIRE(true == profilerValid.StopProfiling());
alexander3c798932021-03-26 21:42:19 +000069
Isabella Gottardi8df12f32021-04-07 17:15:31 +010070 std::vector<arm::app::ProfileResult> results;
71 profilerValid.GetAllResultsAndReset(results);
72 REQUIRE(results.size() == 2);
73 REQUIRE(results[0].name == "one");
74 REQUIRE(results[0].samplesNum == 1);
75 REQUIRE(results[1].name == "two");
76 REQUIRE(results[1].samplesNum == 2);
77 }
78
79#if defined (CPU_PROFILE_ENABLED)
80 SECTION("Test CPU profiler") {
81
Kshitij Sisodia4cc40212022-04-08 09:54:53 +010082 arm::app::Profiler profilerCPU{"test cpu"};
Isabella Gottardi8df12f32021-04-07 17:15:31 +010083 std::vector<arm::app::ProfileResult> results;
84 profilerCPU.StartProfiling();
85 profilerCPU.StopProfiling();
86 profilerCPU.GetAllResultsAndReset(results);
87 REQUIRE(results.size() == 1);
88 bool foundTime = false;
89 bool foundCPU_ACTIVE = false;
90 for(arm::app::Statistics& stat: results[0].data) {
91
92 if (!foundTime) {
93 foundTime = stat.name == "Time";
94 }
95
96 if (!foundCPU_ACTIVE) {
97 foundCPU_ACTIVE = stat.name == "CPU ACTIVE";
98 }
99
100 }
101 REQUIRE(foundTime);
102 REQUIRE(foundCPU_ACTIVE);
103 }
104#endif /* defined (CPU_PROFILE_ENABLED) */
105}