Narumol Prangnawarat | 15effd8 | 2019-10-22 14:17:11 +0100 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2019 Arm Ltd. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
janeil01 | c4946c7 | 2019-11-07 09:32:28 +0000 | [diff] [blame] | 6 | #include <armnn/Types.hpp> |
Narumol Prangnawarat | 15effd8 | 2019-10-22 14:17:11 +0100 | [diff] [blame] | 7 | |
Narumol Prangnawarat | dbdd1b4 | 2019-11-15 17:38:44 +0000 | [diff] [blame] | 8 | #include "LabelsAndEventClasses.hpp" |
| 9 | #include "ProfilingGuidGenerator.hpp" |
| 10 | |
| 11 | #include <set> |
| 12 | |
Narumol Prangnawarat | 15effd8 | 2019-10-22 14:17:11 +0100 | [diff] [blame] | 13 | #include <boost/test/unit_test.hpp> |
Jan Eilers | 156113c | 2020-09-09 19:11:16 +0100 | [diff] [blame] | 14 | #include <fmt/format.h> |
Finn Williams | 38939ff | 2020-04-16 16:57:59 +0100 | [diff] [blame] | 15 | #include <thread> |
Narumol Prangnawarat | 15effd8 | 2019-10-22 14:17:11 +0100 | [diff] [blame] | 16 | |
| 17 | using namespace armnn::profiling; |
| 18 | |
| 19 | BOOST_AUTO_TEST_SUITE(ProfilingGuidTests) |
| 20 | |
| 21 | BOOST_AUTO_TEST_CASE(GuidTest) |
| 22 | { |
| 23 | ProfilingGuid guid0(0); |
| 24 | ProfilingGuid guid1(1); |
| 25 | ProfilingGuid guid2(1); |
| 26 | |
| 27 | BOOST_TEST(guid0 != guid1); |
| 28 | BOOST_TEST(guid1 == guid2); |
| 29 | BOOST_TEST(guid0 < guid1); |
| 30 | BOOST_TEST(guid0 <= guid1); |
| 31 | BOOST_TEST(guid1 <= guid2); |
| 32 | BOOST_TEST(guid1 > guid0); |
| 33 | BOOST_TEST(guid1 >= guid0); |
| 34 | BOOST_TEST(guid1 >= guid2); |
| 35 | } |
| 36 | |
| 37 | BOOST_AUTO_TEST_CASE(StaticGuidTest) |
| 38 | { |
| 39 | ProfilingStaticGuid guid0(0); |
| 40 | ProfilingStaticGuid guid1(1); |
| 41 | ProfilingStaticGuid guid2(1); |
| 42 | |
| 43 | BOOST_TEST(guid0 != guid1); |
| 44 | BOOST_TEST(guid1 == guid2); |
| 45 | BOOST_TEST(guid0 < guid1); |
| 46 | BOOST_TEST(guid0 <= guid1); |
| 47 | BOOST_TEST(guid1 <= guid2); |
| 48 | BOOST_TEST(guid1 > guid0); |
| 49 | BOOST_TEST(guid1 >= guid0); |
| 50 | BOOST_TEST(guid1 >= guid2); |
| 51 | } |
| 52 | |
| 53 | BOOST_AUTO_TEST_CASE(DynamicGuidTest) |
| 54 | { |
| 55 | ProfilingDynamicGuid guid0(0); |
| 56 | ProfilingDynamicGuid guid1(1); |
| 57 | ProfilingDynamicGuid guid2(1); |
| 58 | |
| 59 | BOOST_TEST(guid0 != guid1); |
| 60 | BOOST_TEST(guid1 == guid2); |
| 61 | BOOST_TEST(guid0 < guid1); |
| 62 | BOOST_TEST(guid0 <= guid1); |
| 63 | BOOST_TEST(guid1 <= guid2); |
| 64 | BOOST_TEST(guid1 > guid0); |
| 65 | BOOST_TEST(guid1 >= guid0); |
| 66 | BOOST_TEST(guid1 >= guid2); |
| 67 | } |
| 68 | |
Narumol Prangnawarat | dbdd1b4 | 2019-11-15 17:38:44 +0000 | [diff] [blame] | 69 | void CheckStaticGuid(uint64_t guid, uint64_t expectedGuid) |
| 70 | { |
| 71 | BOOST_TEST(guid == expectedGuid); |
| 72 | BOOST_TEST(guid >= MIN_STATIC_GUID); |
| 73 | } |
| 74 | |
| 75 | void CheckDynamicGuid(uint64_t guid, uint64_t expectedGuid) |
| 76 | { |
| 77 | BOOST_TEST(guid == expectedGuid); |
| 78 | BOOST_TEST(guid < MIN_STATIC_GUID); |
| 79 | } |
| 80 | |
| 81 | BOOST_AUTO_TEST_CASE(StaticGuidGeneratorCollisionTest) |
| 82 | { |
| 83 | ProfilingGuidGenerator generator; |
| 84 | std::set<uint64_t> guids; |
Colm Donelan | 2ba48d2 | 2019-11-29 09:10:59 +0000 | [diff] [blame] | 85 | for ( int i = 0; i < 100000; ++i ) |
Narumol Prangnawarat | dbdd1b4 | 2019-11-15 17:38:44 +0000 | [diff] [blame] | 86 | { |
Matteo Martincigh | 1571700 | 2019-11-22 14:13:02 +0000 | [diff] [blame] | 87 | std::stringstream ss; |
| 88 | ss << i; |
| 89 | std::string str = ss.str(); |
Narumol Prangnawarat | dbdd1b4 | 2019-11-15 17:38:44 +0000 | [diff] [blame] | 90 | ProfilingStaticGuid guid = generator.GenerateStaticId(str.c_str()); |
| 91 | if (guids.find(guid) != guids.end()) |
| 92 | { |
Colm Donelan | 94c9788 | 2020-05-12 17:17:59 +0100 | [diff] [blame] | 93 | // If we're running on a 32bit system it is more likely to get a GUID clash over 1 million executions. |
| 94 | // We can generally detect this when the GUID turns out to be MIN_STATIC_GUID. Output a warning |
| 95 | // message rather than error in this case. |
| 96 | if (guid == ProfilingGuid(armnn::profiling::MIN_STATIC_GUID)) |
| 97 | { |
| 98 | BOOST_WARN("MIN_STATIC_GUID returned more than once from GenerateStaticId."); |
| 99 | } |
| 100 | else |
| 101 | { |
Jan Eilers | 156113c | 2020-09-09 19:11:16 +0100 | [diff] [blame] | 102 | BOOST_ERROR(fmt::format("GUID collision occurred: {} -> {}", str, guid)); |
Colm Donelan | 94c9788 | 2020-05-12 17:17:59 +0100 | [diff] [blame] | 103 | } |
Matteo Martincigh | 1571700 | 2019-11-22 14:13:02 +0000 | [diff] [blame] | 104 | break; |
Narumol Prangnawarat | dbdd1b4 | 2019-11-15 17:38:44 +0000 | [diff] [blame] | 105 | } |
| 106 | guids.insert(guid); |
| 107 | } |
Narumol Prangnawarat | dbdd1b4 | 2019-11-15 17:38:44 +0000 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | BOOST_AUTO_TEST_CASE(StaticGuidGeneratorTest) |
| 111 | { |
| 112 | ProfilingGuidGenerator generator; |
| 113 | |
| 114 | ProfilingStaticGuid staticGuid0 = generator.GenerateStaticId("name"); |
| 115 | CheckStaticGuid(staticGuid0, LabelsAndEventClasses::NAME_GUID); |
| 116 | BOOST_TEST(staticGuid0 != generator.GenerateStaticId("Name")); |
| 117 | |
| 118 | ProfilingStaticGuid staticGuid1 = generator.GenerateStaticId("type"); |
| 119 | CheckStaticGuid(staticGuid1, LabelsAndEventClasses::TYPE_GUID); |
| 120 | BOOST_TEST(staticGuid1 != generator.GenerateStaticId("Type")); |
| 121 | |
| 122 | ProfilingStaticGuid staticGuid2 = generator.GenerateStaticId("index"); |
| 123 | CheckStaticGuid(staticGuid2, LabelsAndEventClasses::INDEX_GUID); |
| 124 | BOOST_TEST(staticGuid2 != generator.GenerateStaticId("Index")); |
| 125 | } |
| 126 | |
| 127 | BOOST_AUTO_TEST_CASE(DynamicGuidGeneratorTest) |
| 128 | { |
| 129 | ProfilingGuidGenerator generator; |
| 130 | |
| 131 | for (unsigned int i = 0; i < 100; ++i) |
| 132 | { |
| 133 | ProfilingDynamicGuid guid = generator.NextGuid(); |
| 134 | CheckDynamicGuid(guid, i); |
| 135 | } |
| 136 | } |
| 137 | |
Finn Williams | 38939ff | 2020-04-16 16:57:59 +0100 | [diff] [blame] | 138 | BOOST_AUTO_TEST_CASE (ProfilingGuidThreadTest) |
| 139 | { |
| 140 | ProfilingGuidGenerator profilingGuidGenerator; |
| 141 | |
| 142 | auto guidGenerator = [&profilingGuidGenerator]() |
| 143 | { |
| 144 | for (int i = 0; i < 1000; ++i) |
| 145 | { |
| 146 | profilingGuidGenerator.NextGuid(); |
| 147 | } |
| 148 | }; |
| 149 | |
| 150 | std::thread t1(guidGenerator); |
| 151 | std::thread t2(guidGenerator); |
| 152 | std::thread t3(guidGenerator); |
| 153 | |
| 154 | t1.join(); |
| 155 | t2.join(); |
| 156 | t3.join(); |
| 157 | |
| 158 | uint64_t guid = profilingGuidGenerator.NextGuid(); |
| 159 | BOOST_CHECK(guid == 3000u); |
| 160 | } |
| 161 | |
Narumol Prangnawarat | 15effd8 | 2019-10-22 14:17:11 +0100 | [diff] [blame] | 162 | BOOST_AUTO_TEST_SUITE_END() |