alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
| 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"); |
| 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 | } |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 52 | |
Isabella Gottardi | 8df12f3 | 2021-04-07 17:15:31 +0100 | [diff] [blame] | 53 | SECTION("Test multiple profilers") { |
Kshitij Sisodia | 4cc4021 | 2022-04-08 09:54:53 +0100 | [diff] [blame] | 54 | arm::app::Profiler profilerValid{"one"}; |
Isabella Gottardi | 8df12f3 | 2021-04-07 17:15:31 +0100 | [diff] [blame] | 55 | REQUIRE(true == profilerValid.StartProfiling()); |
| 56 | REQUIRE(true == profilerValid.StopProfiling()); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 57 | |
Isabella Gottardi | 8df12f3 | 2021-04-07 17:15:31 +0100 | [diff] [blame] | 58 | REQUIRE(true == profilerValid.StartProfiling("two")); |
| 59 | REQUIRE(true == profilerValid.StopProfiling()); |
| 60 | REQUIRE(true == profilerValid.StartProfiling("two")); |
| 61 | REQUIRE(true == profilerValid.StopProfiling()); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 62 | |
Isabella Gottardi | 8df12f3 | 2021-04-07 17:15:31 +0100 | [diff] [blame] | 63 | 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 Sisodia | 4cc4021 | 2022-04-08 09:54:53 +0100 | [diff] [blame] | 75 | arm::app::Profiler profilerCPU{"test cpu"}; |
Isabella Gottardi | 8df12f3 | 2021-04-07 17:15:31 +0100 | [diff] [blame] | 76 | 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 | } |