blob: 1435dde6c1e2118a7e7f20f406aba6a6b2356ede [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;
29 data_acq_module data_acq {};
30 data_psn_module data_psn {};
31 platform_timer timer {};
32
33 /* Initialise the HAL and platform. */
34 hal_init(&platform, &data_acq, &data_psn, &timer);
35 hal_platform_init(&platform);
36
Isabella Gottardi8df12f32021-04-07 17:15:31 +010037 /* An invalid profiler shouldn't be of much use */
38 SECTION("Test invalid profiler") {
39 arm::app::Profiler profilerInvalid{nullptr, "test_invalid"};
40 REQUIRE(false == profilerInvalid.StartProfiling());
41 REQUIRE(false == profilerInvalid.StopProfiling());
42 }
alexander3c798932021-03-26 21:42:19 +000043
Isabella Gottardi8df12f32021-04-07 17:15:31 +010044 SECTION("Test valid profiler") {
45 arm::app::Profiler profilerValid{&platform, "test_valid"};
46 REQUIRE(true == profilerValid.StartProfiling());
47 REQUIRE(true == profilerValid.StopProfiling());
48 std::vector<arm::app::ProfileResult> results;
49 profilerValid.GetAllResultsAndReset(results);
50 REQUIRE(results.size() == 1);
51 REQUIRE(results[0].name == "test_valid");
52 /* Abuse should still fail: */
53 REQUIRE(false == profilerValid.StopProfiling()); /* We need to start it first */
54 REQUIRE(true == profilerValid.StartProfiling()); /* Should be able to start it fine */
55 REQUIRE(false == profilerValid.StartProfiling()); /* Can't restart it without resetting */
56 profilerValid.Reset();
57 REQUIRE(true == profilerValid.StartProfiling()); /* Can start it again now.. */
58 REQUIRE(true == profilerValid.StopProfiling());
59 }
alexander3c798932021-03-26 21:42:19 +000060
Isabella Gottardi8df12f32021-04-07 17:15:31 +010061 SECTION("Test multiple profilers") {
62 arm::app::Profiler profilerValid{&platform, "one"};
63 REQUIRE(true == profilerValid.StartProfiling());
64 REQUIRE(true == profilerValid.StopProfiling());
alexander3c798932021-03-26 21:42:19 +000065
Isabella Gottardi8df12f32021-04-07 17:15:31 +010066 REQUIRE(true == profilerValid.StartProfiling("two"));
67 REQUIRE(true == profilerValid.StopProfiling());
68 REQUIRE(true == profilerValid.StartProfiling("two"));
69 REQUIRE(true == profilerValid.StopProfiling());
alexander3c798932021-03-26 21:42:19 +000070
Isabella Gottardi8df12f32021-04-07 17:15:31 +010071 std::vector<arm::app::ProfileResult> results;
72 profilerValid.GetAllResultsAndReset(results);
73 REQUIRE(results.size() == 2);
74 REQUIRE(results[0].name == "one");
75 REQUIRE(results[0].samplesNum == 1);
76 REQUIRE(results[1].name == "two");
77 REQUIRE(results[1].samplesNum == 2);
78 }
79
80#if defined (CPU_PROFILE_ENABLED)
81 SECTION("Test CPU profiler") {
82
83 arm::app::Profiler profilerCPU{&platform, "test cpu"};
84 std::vector<arm::app::ProfileResult> results;
85 profilerCPU.StartProfiling();
86 profilerCPU.StopProfiling();
87 profilerCPU.GetAllResultsAndReset(results);
88 REQUIRE(results.size() == 1);
89 bool foundTime = false;
90 bool foundCPU_ACTIVE = false;
91 for(arm::app::Statistics& stat: results[0].data) {
92
93 if (!foundTime) {
94 foundTime = stat.name == "Time";
95 }
96
97 if (!foundCPU_ACTIVE) {
98 foundCPU_ACTIVE = stat.name == "CPU ACTIVE";
99 }
100
101 }
102 REQUIRE(foundTime);
103 REQUIRE(foundCPU_ACTIVE);
104 }
105#endif /* defined (CPU_PROFILE_ENABLED) */
106}