2 // Copyright © 2019 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
6 #include "../FileOnlyProfilingConnection.hpp"
8 #include <ProfilingService.hpp>
11 #include <boost/core/ignore_unused.hpp>
12 #include <boost/filesystem.hpp>
13 #include <boost/numeric/conversion/cast.hpp>
14 #include <boost/test/unit_test.hpp>
21 using namespace armnn::profiling;
22 using namespace armnn;
24 using namespace std::chrono_literals;
26 BOOST_AUTO_TEST_SUITE(FileOnlyProfilingDecoratorTests)
28 BOOST_AUTO_TEST_CASE(DumpOutgoingValidFileEndToEnd)
30 // Create a temporary file name.
31 boost::filesystem::path tempPath = boost::filesystem::temp_directory_path();
32 boost::filesystem::path tempFile = boost::filesystem::unique_path();
33 tempPath = tempPath / tempFile;
34 armnn::Runtime::CreationOptions::ExternalProfilingOptions options;
35 options.m_EnableProfiling = true;
36 options.m_FileOnly = true;
37 options.m_IncomingCaptureFile = "";
38 options.m_OutgoingCaptureFile = tempPath.string();
39 options.m_CapturePeriod = 100;
41 // Enable the profiling service
42 ProfilingService& profilingService = ProfilingService::Instance();
43 profilingService.ResetExternalProfilingOptions(options, true);
44 // Bring the profiling service to the "WaitingForAck" state
45 profilingService.Update();
46 profilingService.Update();
48 uint32_t timeout = 1000; // Wait for a maximum of 1000mSec
49 uint32_t sleepTime = 2; // in 2mSec intervals.
50 uint32_t timeSlept = 0;
52 // Give the profiling service sending thread time start executing and send the stream metadata.
53 while (profilingService.GetCurrentState() != ProfilingState::WaitingForAck)
55 if (timeSlept >= timeout)
57 BOOST_FAIL("Timeout: Profiling service did not switch to WaitingForAck state");
59 std::this_thread::sleep_for(std::chrono::milliseconds(sleepTime));
60 timeSlept += sleepTime;
63 profilingService.Update();
67 while (profilingService.GetCurrentState() != profiling::ProfilingState::Active)
69 if (timeSlept >= timeout)
71 BOOST_FAIL("Timeout: Profiling service did not switch to Active state");
73 std::this_thread::sleep_for(std::chrono::milliseconds(sleepTime));
74 timeSlept += sleepTime;
77 // Minimum test here is to check that the file was created.
78 BOOST_CHECK(boost::filesystem::exists(tempPath.c_str()) == true);
80 // Increment a counter.
81 BOOST_CHECK(profilingService.IsCounterRegistered(0) == true);
82 profilingService.IncrementCounterValue(0);
83 BOOST_CHECK(profilingService.GetCounterValue(0) > 0);
85 // At this point the profiling service is active and we've activated all the counters. Waiting a collection
86 // period should be enough to have some data in the file.
88 // Wait for 1 collection period plus a bit of overhead..
89 std::this_thread::sleep_for(std::chrono::milliseconds(150));
91 // In order to flush the files we need to gracefully close the profiling service.
92 options.m_EnableProfiling = false;
93 profilingService.ResetExternalProfilingOptions(options, true);
94 // Wait a short time to allow the threads to clean themselves up.
95 std::this_thread::sleep_for(std::chrono::milliseconds(500));
97 // The output file size should be greater than 0.
98 struct stat statusBuffer;
99 BOOST_CHECK(stat(tempPath.c_str(), &statusBuffer) == 0);
100 BOOST_CHECK(statusBuffer.st_size > 0);
102 // Delete the tmp file.
103 BOOST_CHECK(remove(tempPath.c_str()) == 0);
106 BOOST_AUTO_TEST_SUITE_END()