ArmNN
 21.02
ProfilingGuidTest.cpp
Go to the documentation of this file.
1 //
2 // Copyright © 2019 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
6 #include <armnn/Types.hpp>
7 
10 
11 #include <set>
12 
13 #include <boost/test/unit_test.hpp>
14 #include <fmt/format.h>
15 #include <thread>
16 
17 using namespace armnn::profiling;
18 
19 BOOST_AUTO_TEST_SUITE(ProfilingGuidTests)
20 
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 
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;
85  for ( int i = 0; i < 100000; ++i )
86  {
87  std::stringstream ss;
88  ss << i;
89  std::string str = ss.str();
90  ProfilingStaticGuid guid = generator.GenerateStaticId(str.c_str());
91  if (guids.find(guid) != guids.end())
92  {
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  {
102  BOOST_ERROR(fmt::format("GUID collision occurred: {} -> {}", str, guid));
103  }
104  break;
105  }
106  guids.insert(guid);
107  }
108 }
109 
110 BOOST_AUTO_TEST_CASE(StaticGuidGeneratorTest)
111 {
112  ProfilingGuidGenerator generator;
113 
114  ProfilingStaticGuid staticGuid0 = generator.GenerateStaticId("name");
116  BOOST_TEST(staticGuid0 != generator.GenerateStaticId("Name"));
117 
118  ProfilingStaticGuid staticGuid1 = generator.GenerateStaticId("type");
120  BOOST_TEST(staticGuid1 != generator.GenerateStaticId("Type"));
121 
122  ProfilingStaticGuid staticGuid2 = generator.GenerateStaticId("index");
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 
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 
BOOST_AUTO_TEST_SUITE(TensorflowLiteParser)
void CheckStaticGuid(uint64_t guid, uint64_t expectedGuid)
Strongly typed guids to distinguish between those generated at runtime, and those that are statically...
Definition: Types.hpp:335
ProfilingStaticGuid GenerateStaticId(const std::string &str) override
Create a ProfilingStaticGuid based on a hash of the string.
static ARMNN_DLLEXPORT ProfilingStaticGuid NAME_GUID
ProfilingDynamicGuid NextGuid() override
Return the next random Guid in the sequence.
BOOST_AUTO_TEST_CASE(CheckConvolution2dLayer)
BOOST_AUTO_TEST_SUITE_END()
static ARMNN_DLLEXPORT ProfilingStaticGuid INDEX_GUID
static ARMNN_DLLEXPORT ProfilingStaticGuid TYPE_GUID
void CheckDynamicGuid(uint64_t guid, uint64_t expectedGuid)