ArmNN
 22.08
ProfilingConnectionDumpToFileDecoratorTests.cpp File Reference
#include <client/src/ProfilingConnectionDumpToFileDecorator.hpp>
#include <Runtime.hpp>
#include <armnnUtils/Filesystem.hpp>
#include <common/include/IgnoreUnused.hpp>
#include <common/include/NumericCast.hpp>
#include <fstream>
#include <sstream>
#include <doctest/doctest.h>

Go to the source code of this file.

Functions

 TEST_SUITE ("ProfilingConnectionDumpToFileDecoratorTests")
 

Function Documentation

◆ TEST_SUITE()

TEST_SUITE ( "ProfilingConnectionDumpToFileDecoratorTests"  )

Definition at line 81 of file ProfilingConnectionDumpToFileDecoratorTests.cpp.

References armnnUtils::Filesystem::NamedTempFile().

82 {
83 TEST_CASE("DumpIncomingInvalidFile")
84 {
85  ProfilingOptions options;
86  options.m_IncomingCaptureFile = "/";
87  options.m_OutgoingCaptureFile = "";
88  ProfilingConnectionDumpToFileDecorator decorator(std::make_unique<DummyProfilingConnection>(), options, false);
89  CHECK_THROWS_AS(decorator.ReadPacket(0), arm::pipe::ProfilingException);
90 }
91 
92 TEST_CASE("DumpIncomingInvalidFileIgnoreErrors")
93 {
94  ProfilingOptions options;
95  options.m_IncomingCaptureFile = "/";
96  options.m_OutgoingCaptureFile = "";
97  ProfilingConnectionDumpToFileDecorator decorator(std::make_unique<DummyProfilingConnection>(), options, true);
98  CHECK_NOTHROW(decorator.ReadPacket(0));
99 }
100 
101 TEST_CASE("DumpIncomingValidFile")
102 {
103  fs::path fileName = armnnUtils::Filesystem::NamedTempFile("Armnn-DumpIncomingValidFileTest-TempFile");
104 
105  ProfilingOptions options;
106  options.m_IncomingCaptureFile = fileName.string();
107  options.m_OutgoingCaptureFile = "";
108 
109  ProfilingConnectionDumpToFileDecorator decorator(std::make_unique<DummyProfilingConnection>(), options, false);
110 
111  // NOTE: unique_ptr is needed here because operator=() is deleted for Packet
112  std::unique_ptr<Packet> packet;
113  CHECK_NOTHROW(packet = std::make_unique<Packet>(decorator.ReadPacket(0)));
114 
115  decorator.Close();
116 
117  std::vector<char> data = ReadDumpFile(options.m_IncomingCaptureFile);
118  const char* packetData = reinterpret_cast<const char*>(packet->GetData());
119 
120  // check if the data read back from the dump file matches the original
121  constexpr unsigned int bytesToSkip = 2u * sizeof(uint32_t); // skip header and packet length
122  int diff = std::strncmp(data.data() + bytesToSkip, packetData, g_DataLength);
123  CHECK(diff == 0);
124  fs::remove(fileName);
125 }
126 
127 TEST_CASE("DumpOutgoingInvalidFile")
128 {
129  ProfilingOptions options;
130  options.m_IncomingCaptureFile = "";
131  options.m_OutgoingCaptureFile = "/";
132  ProfilingConnectionDumpToFileDecorator decorator(std::make_unique<DummyProfilingConnection>(), options, false);
133  CHECK_THROWS_AS(decorator.WritePacket(g_DataPtr, g_DataLength), arm::pipe::ProfilingException);
134 }
135 
136 TEST_CASE("DumpOutgoingInvalidFileIgnoreErrors")
137 {
138  ProfilingOptions options;
139  options.m_IncomingCaptureFile = "";
140  options.m_OutgoingCaptureFile = "/";
141 
142  ProfilingConnectionDumpToFileDecorator decorator(std::make_unique<DummyProfilingConnection>(), options, true);
143  CHECK_NOTHROW(decorator.WritePacket(g_DataPtr, g_DataLength));
144 
145  bool success = decorator.WritePacket(g_DataPtr, g_DataLength);
146  CHECK(!success);
147 }
148 
149 TEST_CASE("DumpOutgoingValidFile")
150 {
151  fs::path fileName = armnnUtils::Filesystem::NamedTempFile("Armnn-DumpOutgoingValidFileTest-TempFile");
152 
153  ProfilingOptions options;
154  options.m_IncomingCaptureFile = "";
155  options.m_OutgoingCaptureFile = fileName.string();
156 
157  ProfilingConnectionDumpToFileDecorator decorator(std::make_unique<DummyProfilingConnection>(), options, false);
158 
159  bool success = false;
160  CHECK_NOTHROW(success = decorator.WritePacket(g_DataPtr, g_DataLength));
161  CHECK(success);
162 
163  decorator.Close();
164 
165  std::vector<char> data = ReadDumpFile(options.m_OutgoingCaptureFile);
166 
167  // check if the data read back from the dump file matches the original
168  int diff = std::strncmp(data.data(), g_Data.data(), g_DataLength);
169  CHECK(diff == 0);
170  fs::remove(fileName);
171 }
172 
173 }
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:24