blob: 889e2f2916663ee917d894bdf01fc11a56d8fe34 [file] [log] [blame]
alexander3c798932021-03-26 21:42:19 +00001/*
2 * Copyright (c) 2021 Arm Limited. All rights reserved.
3 * 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{
28 hal_platform platform;
alexander3c798932021-03-26 21:42:19 +000029 platform_timer timer {};
30
31 /* Initialise the HAL and platform. */
Kshitij Sisodia68fdd112022-04-06 13:03:20 +010032 hal_init(&platform, &timer);
alexander3c798932021-03-26 21:42:19 +000033 hal_platform_init(&platform);
34
Isabella Gottardi8df12f32021-04-07 17:15:31 +010035 /* An invalid profiler shouldn't be of much use */
36 SECTION("Test invalid profiler") {
37 arm::app::Profiler profilerInvalid{nullptr, "test_invalid"};
38 REQUIRE(false == profilerInvalid.StartProfiling());
39 REQUIRE(false == profilerInvalid.StopProfiling());
40 }
alexander3c798932021-03-26 21:42:19 +000041
Isabella Gottardi8df12f32021-04-07 17:15:31 +010042 SECTION("Test valid profiler") {
43 arm::app::Profiler profilerValid{&platform, "test_valid"};
44 REQUIRE(true == profilerValid.StartProfiling());
45 REQUIRE(true == profilerValid.StopProfiling());
46 std::vector<arm::app::ProfileResult> results;
47 profilerValid.GetAllResultsAndReset(results);
48 REQUIRE(results.size() == 1);
49 REQUIRE(results[0].name == "test_valid");
50 /* Abuse should still fail: */
51 REQUIRE(false == profilerValid.StopProfiling()); /* We need to start it first */
52 REQUIRE(true == profilerValid.StartProfiling()); /* Should be able to start it fine */
53 REQUIRE(false == profilerValid.StartProfiling()); /* Can't restart it without resetting */
54 profilerValid.Reset();
55 REQUIRE(true == profilerValid.StartProfiling()); /* Can start it again now.. */
56 REQUIRE(true == profilerValid.StopProfiling());
57 }
alexander3c798932021-03-26 21:42:19 +000058
Isabella Gottardi8df12f32021-04-07 17:15:31 +010059 SECTION("Test multiple profilers") {
60 arm::app::Profiler profilerValid{&platform, "one"};
61 REQUIRE(true == profilerValid.StartProfiling());
62 REQUIRE(true == profilerValid.StopProfiling());
alexander3c798932021-03-26 21:42:19 +000063
Isabella Gottardi8df12f32021-04-07 17:15:31 +010064 REQUIRE(true == profilerValid.StartProfiling("two"));
65 REQUIRE(true == profilerValid.StopProfiling());
66 REQUIRE(true == profilerValid.StartProfiling("two"));
67 REQUIRE(true == profilerValid.StopProfiling());
alexander3c798932021-03-26 21:42:19 +000068
Isabella Gottardi8df12f32021-04-07 17:15:31 +010069 std::vector<arm::app::ProfileResult> results;
70 profilerValid.GetAllResultsAndReset(results);
71 REQUIRE(results.size() == 2);
72 REQUIRE(results[0].name == "one");
73 REQUIRE(results[0].samplesNum == 1);
74 REQUIRE(results[1].name == "two");
75 REQUIRE(results[1].samplesNum == 2);
76 }
77
78#if defined (CPU_PROFILE_ENABLED)
79 SECTION("Test CPU profiler") {
80
81 arm::app::Profiler profilerCPU{&platform, "test cpu"};
82 std::vector<arm::app::ProfileResult> results;
83 profilerCPU.StartProfiling();
84 profilerCPU.StopProfiling();
85 profilerCPU.GetAllResultsAndReset(results);
86 REQUIRE(results.size() == 1);
87 bool foundTime = false;
88 bool foundCPU_ACTIVE = false;
89 for(arm::app::Statistics& stat: results[0].data) {
90
91 if (!foundTime) {
92 foundTime = stat.name == "Time";
93 }
94
95 if (!foundCPU_ACTIVE) {
96 foundCPU_ACTIVE = stat.name == "CPU ACTIVE";
97 }
98
99 }
100 REQUIRE(foundTime);
101 REQUIRE(foundCPU_ACTIVE);
102 }
103#endif /* defined (CPU_PROFILE_ENABLED) */
104}