ArmNN
 21.08
ProfilingConnectionDumpToFileDecoratorTests.cpp
Go to the documentation of this file.
1 //
2 // Copyright © 2019 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
6 #include "../ProfilingConnectionDumpToFileDecorator.hpp"
8 #include <Runtime.hpp>
11 
12 #include <fstream>
13 #include <sstream>
14 
15 #include <doctest/doctest.h>
16 
17 using namespace armnn::profiling;
18 
19 namespace
20 {
21 
22 const std::vector<char> g_Data = { 'd', 'u', 'm', 'm', 'y' };
23 const uint32_t g_DataLength = armnn::numeric_cast<uint32_t>(g_Data.size());
24 const unsigned char* g_DataPtr = reinterpret_cast<const unsigned char*>(g_Data.data());
25 
26 class DummyProfilingConnection : public IProfilingConnection
27 {
28 public:
29  DummyProfilingConnection()
30  : m_Open(true)
31  , m_PacketData(std::make_unique<unsigned char[]>(g_DataLength))
32  {
33  // populate packet data and construct packet
34  std::memcpy(m_PacketData.get(), g_DataPtr, g_DataLength);
35  m_Packet = std::make_unique<arm::pipe::Packet>(0u, g_DataLength, m_PacketData);
36  }
37 
38  ~DummyProfilingConnection() = default;
39 
40  bool IsOpen() const override
41  {
42  return m_Open;
43  }
44 
45  void Close() override
46  {
47  m_Open = false;
48  }
49 
50  bool WritePacket(const unsigned char* buffer, uint32_t length) override
51  {
52  armnn::IgnoreUnused(buffer);
53  armnn::IgnoreUnused(length);
54  return true;
55  }
56 
57  arm::pipe::Packet ReadPacket(uint32_t timeout) override
58  {
59  armnn::IgnoreUnused(timeout);
60  return std::move(*m_Packet);
61  }
62 
63 private:
64  bool m_Open;
65  std::unique_ptr<unsigned char[]> m_PacketData;
66  std::unique_ptr<arm::pipe::Packet> m_Packet;
67 };
68 
69 std::vector<char> ReadDumpFile(const std::string& dumpFileName)
70 {
71  std::ifstream input(dumpFileName, std::ios::binary);
72  return std::vector<char>(std::istreambuf_iterator<char>(input), {});
73 }
74 
75 } // anonymous namespace
76 
77 TEST_SUITE("ProfilingConnectionDumpToFileDecoratorTests")
78 {
79 TEST_CASE("DumpIncomingInvalidFile")
80 {
82  options.m_IncomingCaptureFile = "/";
83  options.m_OutgoingCaptureFile = "";
84  ProfilingConnectionDumpToFileDecorator decorator(std::make_unique<DummyProfilingConnection>(), options, false);
85  CHECK_THROWS_AS(decorator.ReadPacket(0), armnn::RuntimeException);
86 }
87 
88 TEST_CASE("DumpIncomingInvalidFileIgnoreErrors")
89 {
91  options.m_IncomingCaptureFile = "/";
92  options.m_OutgoingCaptureFile = "";
93  ProfilingConnectionDumpToFileDecorator decorator(std::make_unique<DummyProfilingConnection>(), options, true);
94  CHECK_NOTHROW(decorator.ReadPacket(0));
95 }
96 
97 TEST_CASE("DumpIncomingValidFile")
98 {
99  fs::path fileName = armnnUtils::Filesystem::NamedTempFile("Armnn-DumpIncomingValidFileTest-TempFile");
100 
102  options.m_IncomingCaptureFile = fileName.string();
103  options.m_OutgoingCaptureFile = "";
104 
105  ProfilingConnectionDumpToFileDecorator decorator(std::make_unique<DummyProfilingConnection>(), options, false);
106 
107  // NOTE: unique_ptr is needed here because operator=() is deleted for Packet
108  std::unique_ptr<arm::pipe::Packet> packet;
109  CHECK_NOTHROW(packet = std::make_unique<arm::pipe::Packet>(decorator.ReadPacket(0)));
110 
111  decorator.Close();
112 
113  std::vector<char> data = ReadDumpFile(options.m_IncomingCaptureFile);
114  const char* packetData = reinterpret_cast<const char*>(packet->GetData());
115 
116  // check if the data read back from the dump file matches the original
117  constexpr unsigned int bytesToSkip = 2u * sizeof(uint32_t); // skip header and packet length
118  int diff = std::strncmp(data.data() + bytesToSkip, packetData, g_DataLength);
119  CHECK(diff == 0);
120  fs::remove(fileName);
121 }
122 
123 TEST_CASE("DumpOutgoingInvalidFile")
124 {
126  options.m_IncomingCaptureFile = "";
127  options.m_OutgoingCaptureFile = "/";
128  ProfilingConnectionDumpToFileDecorator decorator(std::make_unique<DummyProfilingConnection>(), options, false);
129  CHECK_THROWS_AS(decorator.WritePacket(g_DataPtr, g_DataLength), armnn::RuntimeException);
130 }
131 
132 TEST_CASE("DumpOutgoingInvalidFileIgnoreErrors")
133 {
135  options.m_IncomingCaptureFile = "";
136  options.m_OutgoingCaptureFile = "/";
137 
138  ProfilingConnectionDumpToFileDecorator decorator(std::make_unique<DummyProfilingConnection>(), options, true);
139  CHECK_NOTHROW(decorator.WritePacket(g_DataPtr, g_DataLength));
140 
141  bool success = decorator.WritePacket(g_DataPtr, g_DataLength);
142  CHECK(!success);
143 }
144 
145 TEST_CASE("DumpOutgoingValidFile")
146 {
147  fs::path fileName = armnnUtils::Filesystem::NamedTempFile("Armnn-DumpOutgoingValidFileTest-TempFile");
148 
150  options.m_IncomingCaptureFile = "";
151  options.m_OutgoingCaptureFile = fileName.string();
152 
153  ProfilingConnectionDumpToFileDecorator decorator(std::make_unique<DummyProfilingConnection>(), options, false);
154 
155  bool success = false;
156  CHECK_NOTHROW(success = decorator.WritePacket(g_DataPtr, g_DataLength));
157  CHECK(success);
158 
159  decorator.Close();
160 
161  std::vector<char> data = ReadDumpFile(options.m_OutgoingCaptureFile);
162 
163  // check if the data read back from the dump file matches the original
164  int diff = std::strncmp(data.data(), g_Data.data(), g_DataLength);
165  CHECK(diff == 0);
166  fs::remove(fileName);
167 }
168 
169 }
TEST_SUITE("TestConstTensorLayerVisitor")
bool WritePacket(const unsigned char *buffer, uint32_t length) override
void IgnoreUnused(Ts &&...)
std::enable_if_t< std::is_unsigned< Source >::value &&std::is_unsigned< Dest >::value, Dest > numeric_cast(Source source)
Definition: NumericCast.hpp:35
fs::path NamedTempFile(const char *fileName)
Returns a path to a file in the system temporary folder. If the file existed it will be deleted...
Definition: Filesystem.cpp:23