alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 1 | /* |
Liam Barry | f30d741 | 2023-09-27 12:48:35 +0100 | [diff] [blame^] | 2 | * SPDX-FileCopyrightText: Copyright 2021, 2023 Arm Limited and/or its affiliates <open-source-office@arm.com> |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 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 | |
| 26 | TEST_CASE("Common: Test Profiler") |
| 27 | { |
Kshitij Sisodia | 4cc4021 | 2022-04-08 09:54:53 +0100 | [diff] [blame] | 28 | hal_platform_init(); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 29 | |
Kshitij Sisodia | 4cc4021 | 2022-04-08 09:54:53 +0100 | [diff] [blame] | 30 | SECTION("Test default construction") { |
| 31 | arm::app::Profiler profiler{}; |
| 32 | REQUIRE(true == profiler.StartProfiling()); |
| 33 | REQUIRE(true == profiler.StopProfiling()); |
Isabella Gottardi | 8df12f3 | 2021-04-07 17:15:31 +0100 | [diff] [blame] | 34 | } |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 35 | |
Isabella Gottardi | 8df12f3 | 2021-04-07 17:15:31 +0100 | [diff] [blame] | 36 | SECTION("Test valid profiler") { |
Kshitij Sisodia | 4cc4021 | 2022-04-08 09:54:53 +0100 | [diff] [blame] | 37 | arm::app::Profiler profilerValid{"test_valid"}; |
Isabella Gottardi | 8df12f3 | 2021-04-07 17:15:31 +0100 | [diff] [blame] | 38 | 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 Barry | f30d741 | 2023-09-27 12:48:35 +0100 | [diff] [blame^] | 44 | |
| 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 Gottardi | 8df12f3 | 2021-04-07 17:15:31 +0100 | [diff] [blame] | 49 | REQUIRE(false == profilerValid.StopProfiling()); /* We need to start it first */ |
Liam Barry | f30d741 | 2023-09-27 12:48:35 +0100 | [diff] [blame^] | 50 | |
Isabella Gottardi | 8df12f3 | 2021-04-07 17:15:31 +0100 | [diff] [blame] | 51 | REQUIRE(true == profilerValid.StartProfiling()); /* Should be able to start it fine */ |
| 52 | REQUIRE(false == profilerValid.StartProfiling()); /* Can't restart it without resetting */ |
Liam Barry | f30d741 | 2023-09-27 12:48:35 +0100 | [diff] [blame^] | 53 | printf("End of Invalid profiler usage common test output. \nERROR messages above this line " |
| 54 | "are expected and can be ignored.\n\n"); |
Isabella Gottardi | 8df12f3 | 2021-04-07 17:15:31 +0100 | [diff] [blame] | 55 | profilerValid.Reset(); |
| 56 | REQUIRE(true == profilerValid.StartProfiling()); /* Can start it again now.. */ |
| 57 | REQUIRE(true == profilerValid.StopProfiling()); |
| 58 | } |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 59 | |
Isabella Gottardi | 8df12f3 | 2021-04-07 17:15:31 +0100 | [diff] [blame] | 60 | SECTION("Test multiple profilers") { |
Kshitij Sisodia | 4cc4021 | 2022-04-08 09:54:53 +0100 | [diff] [blame] | 61 | arm::app::Profiler profilerValid{"one"}; |
Isabella Gottardi | 8df12f3 | 2021-04-07 17:15:31 +0100 | [diff] [blame] | 62 | REQUIRE(true == profilerValid.StartProfiling()); |
| 63 | REQUIRE(true == profilerValid.StopProfiling()); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 64 | |
Isabella Gottardi | 8df12f3 | 2021-04-07 17:15:31 +0100 | [diff] [blame] | 65 | REQUIRE(true == profilerValid.StartProfiling("two")); |
| 66 | REQUIRE(true == profilerValid.StopProfiling()); |
| 67 | REQUIRE(true == profilerValid.StartProfiling("two")); |
| 68 | REQUIRE(true == profilerValid.StopProfiling()); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 69 | |
Isabella Gottardi | 8df12f3 | 2021-04-07 17:15:31 +0100 | [diff] [blame] | 70 | 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 Sisodia | 4cc4021 | 2022-04-08 09:54:53 +0100 | [diff] [blame] | 82 | arm::app::Profiler profilerCPU{"test cpu"}; |
Isabella Gottardi | 8df12f3 | 2021-04-07 17:15:31 +0100 | [diff] [blame] | 83 | 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 | } |