blob: f89699cb992187e81b5b082e18f8a1ac413da9bb [file] [log] [blame]
Narumol Prangnawarat15effd82019-10-22 14:17:11 +01001//
2// Copyright © 2019 Arm Ltd. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
5
janeil01c4946c72019-11-07 09:32:28 +00006#include <armnn/Types.hpp>
Narumol Prangnawarat15effd82019-10-22 14:17:11 +01007
Narumol Prangnawaratdbdd1b42019-11-15 17:38:44 +00008#include "LabelsAndEventClasses.hpp"
9#include "ProfilingGuidGenerator.hpp"
10
11#include <set>
12
Narumol Prangnawarat15effd82019-10-22 14:17:11 +010013#include <boost/test/unit_test.hpp>
Jan Eilers156113c2020-09-09 19:11:16 +010014#include <fmt/format.h>
Finn Williams38939ff2020-04-16 16:57:59 +010015#include <thread>
Narumol Prangnawarat15effd82019-10-22 14:17:11 +010016
17using namespace armnn::profiling;
18
19BOOST_AUTO_TEST_SUITE(ProfilingGuidTests)
20
21BOOST_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
37BOOST_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
53BOOST_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 Prangnawaratdbdd1b42019-11-15 17:38:44 +000069void CheckStaticGuid(uint64_t guid, uint64_t expectedGuid)
70{
71 BOOST_TEST(guid == expectedGuid);
72 BOOST_TEST(guid >= MIN_STATIC_GUID);
73}
74
75void CheckDynamicGuid(uint64_t guid, uint64_t expectedGuid)
76{
77 BOOST_TEST(guid == expectedGuid);
78 BOOST_TEST(guid < MIN_STATIC_GUID);
79}
80
81BOOST_AUTO_TEST_CASE(StaticGuidGeneratorCollisionTest)
82{
83 ProfilingGuidGenerator generator;
84 std::set<uint64_t> guids;
Colm Donelan2ba48d22019-11-29 09:10:59 +000085 for ( int i = 0; i < 100000; ++i )
Narumol Prangnawaratdbdd1b42019-11-15 17:38:44 +000086 {
Matteo Martincigh15717002019-11-22 14:13:02 +000087 std::stringstream ss;
88 ss << i;
89 std::string str = ss.str();
Narumol Prangnawaratdbdd1b42019-11-15 17:38:44 +000090 ProfilingStaticGuid guid = generator.GenerateStaticId(str.c_str());
91 if (guids.find(guid) != guids.end())
92 {
Colm Donelan94c97882020-05-12 17:17:59 +010093 // 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 Eilers156113c2020-09-09 19:11:16 +0100102 BOOST_ERROR(fmt::format("GUID collision occurred: {} -> {}", str, guid));
Colm Donelan94c97882020-05-12 17:17:59 +0100103 }
Matteo Martincigh15717002019-11-22 14:13:02 +0000104 break;
Narumol Prangnawaratdbdd1b42019-11-15 17:38:44 +0000105 }
106 guids.insert(guid);
107 }
Narumol Prangnawaratdbdd1b42019-11-15 17:38:44 +0000108}
109
110BOOST_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
127BOOST_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 Williams38939ff2020-04-16 16:57:59 +0100138BOOST_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 Prangnawarat15effd82019-10-22 14:17:11 +0100162BOOST_AUTO_TEST_SUITE_END()