blob: 62c734754a1928dbccd7b958641335ea00adbdcd [file] [log] [blame]
alexander3c798932021-03-26 21:42:19 +00001/*
Richard Burtonf32a86a2022-11-15 11:46:11 +00002 * SPDX-FileCopyrightText: Copyright 2021 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");
44 /* Abuse should still fail: */
45 REQUIRE(false == profilerValid.StopProfiling()); /* We need to start it first */
46 REQUIRE(true == profilerValid.StartProfiling()); /* Should be able to start it fine */
47 REQUIRE(false == profilerValid.StartProfiling()); /* Can't restart it without resetting */
48 profilerValid.Reset();
49 REQUIRE(true == profilerValid.StartProfiling()); /* Can start it again now.. */
50 REQUIRE(true == profilerValid.StopProfiling());
51 }
alexander3c798932021-03-26 21:42:19 +000052
Isabella Gottardi8df12f32021-04-07 17:15:31 +010053 SECTION("Test multiple profilers") {
Kshitij Sisodia4cc40212022-04-08 09:54:53 +010054 arm::app::Profiler profilerValid{"one"};
Isabella Gottardi8df12f32021-04-07 17:15:31 +010055 REQUIRE(true == profilerValid.StartProfiling());
56 REQUIRE(true == profilerValid.StopProfiling());
alexander3c798932021-03-26 21:42:19 +000057
Isabella Gottardi8df12f32021-04-07 17:15:31 +010058 REQUIRE(true == profilerValid.StartProfiling("two"));
59 REQUIRE(true == profilerValid.StopProfiling());
60 REQUIRE(true == profilerValid.StartProfiling("two"));
61 REQUIRE(true == profilerValid.StopProfiling());
alexander3c798932021-03-26 21:42:19 +000062
Isabella Gottardi8df12f32021-04-07 17:15:31 +010063 std::vector<arm::app::ProfileResult> results;
64 profilerValid.GetAllResultsAndReset(results);
65 REQUIRE(results.size() == 2);
66 REQUIRE(results[0].name == "one");
67 REQUIRE(results[0].samplesNum == 1);
68 REQUIRE(results[1].name == "two");
69 REQUIRE(results[1].samplesNum == 2);
70 }
71
72#if defined (CPU_PROFILE_ENABLED)
73 SECTION("Test CPU profiler") {
74
Kshitij Sisodia4cc40212022-04-08 09:54:53 +010075 arm::app::Profiler profilerCPU{"test cpu"};
Isabella Gottardi8df12f32021-04-07 17:15:31 +010076 std::vector<arm::app::ProfileResult> results;
77 profilerCPU.StartProfiling();
78 profilerCPU.StopProfiling();
79 profilerCPU.GetAllResultsAndReset(results);
80 REQUIRE(results.size() == 1);
81 bool foundTime = false;
82 bool foundCPU_ACTIVE = false;
83 for(arm::app::Statistics& stat: results[0].data) {
84
85 if (!foundTime) {
86 foundTime = stat.name == "Time";
87 }
88
89 if (!foundCPU_ACTIVE) {
90 foundCPU_ACTIVE = stat.name == "CPU ACTIVE";
91 }
92
93 }
94 REQUIRE(foundTime);
95 REQUIRE(foundCPU_ACTIVE);
96 }
97#endif /* defined (CPU_PROFILE_ENABLED) */
98}