IVGCVSW-4171 Fix intermittent failure on FileOnlyProfilingDecoratorTests
[platform/upstream/armnn.git] / src / profiling / test / FileOnlyProfilingDecoratorTests.cpp
1 //
2 // Copyright © 2019 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5
6 #include "../FileOnlyProfilingConnection.hpp"
7
8 #include <ProfilingService.hpp>
9 #include <Runtime.hpp>
10
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>
15
16 #include <cstdio>
17 #include <fstream>
18 #include <sstream>
19 #include <sys/stat.h>
20
21 using namespace armnn::profiling;
22 using namespace armnn;
23
24 using namespace std::chrono_literals;
25
26 BOOST_AUTO_TEST_SUITE(FileOnlyProfilingDecoratorTests)
27
28 BOOST_AUTO_TEST_CASE(DumpOutgoingValidFileEndToEnd)
29 {
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;
40
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();
47
48     uint32_t timeout = 1000; // Wait for a maximum of 1000mSec
49     uint32_t sleepTime = 2;  // in 2mSec intervals.
50     uint32_t timeSlept = 0;
51
52     // Give the profiling service sending thread time start executing and send the stream metadata.
53     while (profilingService.GetCurrentState() != ProfilingState::WaitingForAck)
54     {
55         if (timeSlept >= timeout)
56         {
57             BOOST_FAIL("Timeout: Profiling service did not switch to WaitingForAck state");
58         }
59         std::this_thread::sleep_for(std::chrono::milliseconds(sleepTime));
60         timeSlept += sleepTime;
61     }
62
63     profilingService.Update();
64
65     timeSlept = 0;
66
67     while (profilingService.GetCurrentState() != profiling::ProfilingState::Active)
68     {
69         if (timeSlept >= timeout)
70         {
71             BOOST_FAIL("Timeout: Profiling service did not switch to Active state");
72         }
73         std::this_thread::sleep_for(std::chrono::milliseconds(sleepTime));
74         timeSlept += sleepTime;
75     }
76
77     // Minimum test here is to check that the file was created.
78     BOOST_CHECK(boost::filesystem::exists(tempPath.c_str()) == true);
79
80     // Increment a counter.
81     BOOST_CHECK(profilingService.IsCounterRegistered(0) == true);
82     profilingService.IncrementCounterValue(0);
83     BOOST_CHECK(profilingService.GetCounterValue(0) > 0);
84
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.
87
88     // Wait for 1 collection period plus a bit of overhead..
89     std::this_thread::sleep_for(std::chrono::milliseconds(150));
90
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));
96
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);
101
102     // Delete the tmp file.
103     BOOST_CHECK(remove(tempPath.c_str()) == 0);
104 }
105
106 BOOST_AUTO_TEST_SUITE_END()