Aron Virginas-Tar | dfa1477 | 2019-09-24 18:24:47 +0100 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2019 Arm Ltd. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #include "../ProfilingConnectionDumpToFileDecorator.hpp" |
| 7 | |
| 8 | #include <fstream> |
| 9 | #include <sstream> |
| 10 | |
| 11 | #include <boost/core/ignore_unused.hpp> |
| 12 | #include <boost/numeric/conversion/cast.hpp> |
| 13 | #include <boost/test/unit_test.hpp> |
| 14 | |
| 15 | #if defined(__ANDROID__) |
| 16 | #define ARMNN_PROFILING_CONNECTION_TEST_DUMP_DIR "/data/local/tmp" |
| 17 | #else |
| 18 | #define ARMNN_PROFILING_CONNECTION_TEST_DUMP_DIR "/tmp" |
| 19 | #endif |
| 20 | |
| 21 | using namespace armnn::profiling; |
| 22 | |
| 23 | namespace |
| 24 | { |
| 25 | |
| 26 | const std::vector<char> g_Data = { 'd', 'u', 'm', 'm', 'y' }; |
| 27 | const uint32_t g_DataLength = boost::numeric_cast<uint32_t>(g_Data.size()); |
| 28 | const unsigned char* g_DataPtr = reinterpret_cast<const unsigned char*>(g_Data.data()); |
| 29 | |
| 30 | class DummyProfilingConnection : public IProfilingConnection |
| 31 | { |
| 32 | public: |
| 33 | DummyProfilingConnection() |
| 34 | : m_Open(true) |
Matteo Martincigh | 67ef2a5 | 2019-10-10 13:29:02 +0100 | [diff] [blame^] | 35 | , m_PacketData(std::make_unique<unsigned char[]>(g_DataLength)) |
Aron Virginas-Tar | dfa1477 | 2019-09-24 18:24:47 +0100 | [diff] [blame] | 36 | { |
| 37 | // populate packet data and construct packet |
| 38 | std::memcpy(m_PacketData.get(), g_DataPtr, g_DataLength); |
| 39 | m_Packet = std::make_unique<Packet>(0u, g_DataLength, m_PacketData); |
| 40 | } |
| 41 | |
| 42 | ~DummyProfilingConnection() = default; |
| 43 | |
Matteo Martincigh | 54fb957 | 2019-10-02 12:50:57 +0100 | [diff] [blame] | 44 | bool IsOpen() const override |
Aron Virginas-Tar | dfa1477 | 2019-09-24 18:24:47 +0100 | [diff] [blame] | 45 | { |
| 46 | return m_Open; |
| 47 | } |
| 48 | |
| 49 | void Close() override |
| 50 | { |
| 51 | m_Open = false; |
| 52 | } |
| 53 | |
| 54 | bool WritePacket(const unsigned char* buffer, uint32_t length) override |
| 55 | { |
| 56 | boost::ignore_unused(buffer); |
| 57 | boost::ignore_unused(length); |
| 58 | return true; |
| 59 | } |
| 60 | |
| 61 | Packet ReadPacket(uint32_t timeout) override |
| 62 | { |
| 63 | boost::ignore_unused(timeout); |
| 64 | return std::move(*m_Packet); |
| 65 | } |
| 66 | |
| 67 | private: |
Matteo Martincigh | 67ef2a5 | 2019-10-10 13:29:02 +0100 | [diff] [blame^] | 68 | bool m_Open; |
| 69 | std::unique_ptr<unsigned char[]> m_PacketData; |
Aron Virginas-Tar | dfa1477 | 2019-09-24 18:24:47 +0100 | [diff] [blame] | 70 | std::unique_ptr<Packet> m_Packet; |
| 71 | }; |
| 72 | |
| 73 | std::vector<char> ReadDumpFile(const std::string& dumpFileName) |
| 74 | { |
| 75 | std::ifstream input(dumpFileName, std::ios::binary); |
| 76 | return std::vector<char>(std::istreambuf_iterator<char>(input), {}); |
| 77 | } |
| 78 | |
| 79 | } // anonymous namespace |
| 80 | |
| 81 | BOOST_AUTO_TEST_SUITE(ProfilingConnectionDumpToFileDecoratorTests) |
| 82 | |
| 83 | BOOST_AUTO_TEST_CASE(CheckSettings) |
| 84 | { |
| 85 | ProfilingConnectionDumpToFileDecoratorSettings settings0("", ""); |
| 86 | BOOST_CHECK(settings0.m_DumpIncoming == false); |
| 87 | BOOST_CHECK(settings0.m_DumpOutgoing == false); |
| 88 | |
| 89 | ProfilingConnectionDumpToFileDecoratorSettings settings1("incomingDumpFile.dat", ""); |
| 90 | BOOST_CHECK(settings1.m_DumpIncoming == true); |
| 91 | BOOST_CHECK(settings1.m_DumpOutgoing == false); |
| 92 | |
| 93 | ProfilingConnectionDumpToFileDecoratorSettings settings2("", "outgoingDumpFile.dat"); |
| 94 | BOOST_CHECK(settings2.m_DumpIncoming == false); |
| 95 | BOOST_CHECK(settings2.m_DumpOutgoing == true); |
| 96 | |
| 97 | ProfilingConnectionDumpToFileDecoratorSettings settings3("incomingDumpFile.dat", "outgoingDumpFile.dat"); |
| 98 | BOOST_CHECK(settings3.m_DumpIncoming == true); |
| 99 | BOOST_CHECK(settings3.m_DumpOutgoing == true); |
| 100 | } |
| 101 | |
| 102 | BOOST_AUTO_TEST_CASE(DumpIncomingInvalidFile) |
| 103 | { |
| 104 | ProfilingConnectionDumpToFileDecoratorSettings settings("/", "", false); |
| 105 | ProfilingConnectionDumpToFileDecorator decorator(std::make_unique<DummyProfilingConnection>(), settings); |
| 106 | BOOST_CHECK_THROW(decorator.ReadPacket(0), armnn::RuntimeException); |
| 107 | } |
| 108 | |
| 109 | BOOST_AUTO_TEST_CASE(DumpIncomingInvalidFileIgnoreErrors) |
| 110 | { |
| 111 | ProfilingConnectionDumpToFileDecoratorSettings settings("/", "", true); |
| 112 | ProfilingConnectionDumpToFileDecorator decorator(std::make_unique<DummyProfilingConnection>(), settings); |
| 113 | BOOST_CHECK_NO_THROW(decorator.ReadPacket(0)); |
| 114 | } |
| 115 | |
| 116 | BOOST_AUTO_TEST_CASE(DumpIncomingValidFile) |
| 117 | { |
| 118 | std::stringstream fileName; |
| 119 | fileName << ARMNN_PROFILING_CONNECTION_TEST_DUMP_DIR << "/test_dump_file_incoming.dat"; |
| 120 | |
| 121 | ProfilingConnectionDumpToFileDecoratorSettings settings(fileName.str(), "", false); |
| 122 | ProfilingConnectionDumpToFileDecorator decorator(std::make_unique<DummyProfilingConnection>(), settings); |
| 123 | |
| 124 | // NOTE: unique_ptr is needed here because operator=() is deleted for Packet |
| 125 | std::unique_ptr<Packet> packet; |
| 126 | BOOST_CHECK_NO_THROW(packet = std::make_unique<Packet>(decorator.ReadPacket(0))); |
| 127 | |
| 128 | decorator.Close(); |
| 129 | |
| 130 | std::vector<char> data = ReadDumpFile(settings.m_IncomingDumpFileName); |
Matteo Martincigh | 67ef2a5 | 2019-10-10 13:29:02 +0100 | [diff] [blame^] | 131 | const char* packetData = reinterpret_cast<const char*>(packet->GetData()); |
Aron Virginas-Tar | dfa1477 | 2019-09-24 18:24:47 +0100 | [diff] [blame] | 132 | |
| 133 | // check if the data read back from the dump file matches the original |
| 134 | constexpr unsigned int bytesToSkip = 2u * sizeof(uint32_t); // skip header and packet length |
| 135 | int diff = std::strncmp(data.data() + bytesToSkip, packetData, g_DataLength); |
| 136 | BOOST_CHECK(diff == 0); |
| 137 | } |
| 138 | |
| 139 | BOOST_AUTO_TEST_CASE(DumpOutgoingInvalidFile) |
| 140 | { |
| 141 | ProfilingConnectionDumpToFileDecoratorSettings settings("", "/", false); |
| 142 | ProfilingConnectionDumpToFileDecorator decorator(std::make_unique<DummyProfilingConnection>(), settings); |
| 143 | BOOST_CHECK_THROW(decorator.WritePacket(g_DataPtr, g_DataLength), armnn::RuntimeException); |
| 144 | } |
| 145 | |
| 146 | BOOST_AUTO_TEST_CASE(DumpOutgoingInvalidFileIgnoreErrors) |
| 147 | { |
| 148 | ProfilingConnectionDumpToFileDecoratorSettings settings("", "/", true); |
| 149 | ProfilingConnectionDumpToFileDecorator decorator(std::make_unique<DummyProfilingConnection>(), settings); |
| 150 | BOOST_CHECK_NO_THROW(decorator.WritePacket(g_DataPtr, g_DataLength)); |
| 151 | |
| 152 | bool success = decorator.WritePacket(g_DataPtr, g_DataLength); |
| 153 | BOOST_CHECK(!success); |
| 154 | } |
| 155 | |
| 156 | BOOST_AUTO_TEST_CASE(DumpOutgoingValidFile) |
| 157 | { |
| 158 | std::stringstream fileName; |
| 159 | fileName << ARMNN_PROFILING_CONNECTION_TEST_DUMP_DIR << "/test_dump_file.dat"; |
| 160 | |
| 161 | ProfilingConnectionDumpToFileDecoratorSettings settings("", fileName.str(), false); |
| 162 | ProfilingConnectionDumpToFileDecorator decorator(std::make_unique<DummyProfilingConnection>(), settings); |
| 163 | |
| 164 | bool success = false; |
| 165 | BOOST_CHECK_NO_THROW(success = decorator.WritePacket(g_DataPtr, g_DataLength)); |
| 166 | BOOST_CHECK(success); |
| 167 | |
| 168 | decorator.Close(); |
| 169 | |
| 170 | std::vector<char> data = ReadDumpFile(settings.m_OutgoingDumpFileName); |
| 171 | |
| 172 | // check if the data read back from the dump file matches the original |
| 173 | int diff = std::strncmp(data.data(), g_Data.data(), g_DataLength); |
| 174 | BOOST_CHECK(diff == 0); |
| 175 | } |
| 176 | |
| 177 | BOOST_AUTO_TEST_SUITE_END() |