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 | { |
| 28 | hal_platform platform; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 29 | platform_timer timer {}; |
| 30 | |
| 31 | /* Initialise the HAL and platform. */ |
Kshitij Sisodia | 68fdd11 | 2022-04-06 13:03:20 +0100 | [diff] [blame^] | 32 | hal_init(&platform, &timer); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 33 | hal_platform_init(&platform); |
| 34 | |
Isabella Gottardi | 8df12f3 | 2021-04-07 17:15:31 +0100 | [diff] [blame] | 35 | /* 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 | } |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 41 | |
Isabella Gottardi | 8df12f3 | 2021-04-07 17:15:31 +0100 | [diff] [blame] | 42 | 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 | } |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 58 | |
Isabella Gottardi | 8df12f3 | 2021-04-07 17:15:31 +0100 | [diff] [blame] | 59 | SECTION("Test multiple profilers") { |
| 60 | arm::app::Profiler profilerValid{&platform, "one"}; |
| 61 | REQUIRE(true == profilerValid.StartProfiling()); |
| 62 | REQUIRE(true == profilerValid.StopProfiling()); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 63 | |
Isabella Gottardi | 8df12f3 | 2021-04-07 17:15:31 +0100 | [diff] [blame] | 64 | REQUIRE(true == profilerValid.StartProfiling("two")); |
| 65 | REQUIRE(true == profilerValid.StopProfiling()); |
| 66 | REQUIRE(true == profilerValid.StartProfiling("two")); |
| 67 | REQUIRE(true == profilerValid.StopProfiling()); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 68 | |
Isabella Gottardi | 8df12f3 | 2021-04-07 17:15:31 +0100 | [diff] [blame] | 69 | 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 | } |