MetricsQueueTest.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <AWSMetricsConstant.h>
  9. #include <AWSMetricsGemMock.h>
  10. #include <Framework/JsonWriter.h>
  11. #include <MetricsEvent.h>
  12. #include <MetricsQueue.h>
  13. #include <AzTest/AzTest.h>
  14. #include <AzCore/UnitTest/TestTypes.h>
  15. #include <AzFramework/StringFunc/StringFunc.h>
  16. namespace AWSMetrics
  17. {
  18. class MetricsQueueTest
  19. : public AWSMetricsGemAllocatorFixture
  20. {
  21. public:
  22. const int NumTestMetrics = 10;
  23. const AZStd::string AttrName = "name";
  24. const AZStd::string AttrValue = "value";
  25. };
  26. TEST_F(MetricsQueueTest, AddMetrics_SingleMetrics_Success)
  27. {
  28. MetricsQueue queue;
  29. int numMetrics = queue.GetNumMetrics();
  30. queue.AddMetrics(MetricsEvent());
  31. ASSERT_EQ(queue.GetNumMetrics(), ++numMetrics);
  32. }
  33. TEST_F(MetricsQueueTest, AppendMetrics_EmptyQueue_Success)
  34. {
  35. MetricsQueue queue;
  36. int numMetrics = queue.GetNumMetrics();
  37. MetricsQueue anotherQueue;
  38. for (int index = 0; index < NumTestMetrics; ++index)
  39. {
  40. anotherQueue.AddMetrics(MetricsEvent());
  41. }
  42. queue.AppendMetrics(anotherQueue);
  43. ASSERT_EQ(queue.GetNumMetrics(), numMetrics + NumTestMetrics);
  44. }
  45. TEST_F(MetricsQueueTest, AppendMetrics_NoneEmptyQueue_Success)
  46. {
  47. MetricsQueue queue;
  48. queue.AddMetrics(MetricsEvent());
  49. int numMetrics = queue.GetNumMetrics();
  50. MetricsQueue anotherQueue;
  51. for (int index = 0; index < NumTestMetrics; ++index)
  52. {
  53. anotherQueue.AddMetrics(MetricsEvent());
  54. }
  55. queue.AppendMetrics(anotherQueue);
  56. ASSERT_EQ(queue.GetNumMetrics(), numMetrics + NumTestMetrics);
  57. }
  58. TEST_F(MetricsQueueTest, PushMetricsToFront_EmptyQueue_Success)
  59. {
  60. MetricsQueue queue;
  61. int numMetrics = queue.GetNumMetrics();
  62. MetricsQueue anotherQueue;
  63. for (int index = 0; index < NumTestMetrics; ++index)
  64. {
  65. anotherQueue.AddMetrics(MetricsEvent());
  66. }
  67. queue.PushMetricsToFront(anotherQueue);
  68. ASSERT_EQ(queue.GetNumMetrics(), numMetrics + NumTestMetrics);
  69. }
  70. TEST_F(MetricsQueueTest, PushMetricsToFront_NoneEmptyQueue_Success)
  71. {
  72. MetricsQueue queue;
  73. MetricsEvent metrics;
  74. metrics.AddAttribute(MetricsAttribute(AttrName, AttrValue));
  75. queue.AddMetrics(metrics);
  76. int numMetrics = queue.GetNumMetrics();
  77. MetricsQueue anotherQueue;
  78. for (int index = 0; index < NumTestMetrics; ++index)
  79. {
  80. anotherQueue.AddMetrics(MetricsEvent());
  81. }
  82. queue.PushMetricsToFront(anotherQueue);
  83. ASSERT_EQ(queue.GetNumMetrics(), numMetrics + NumTestMetrics);
  84. ASSERT_EQ(queue[NumTestMetrics].GetNumAttributes(), 1);
  85. }
  86. TEST_F(MetricsQueueTest, FilterMetricsByPriority_ReachMaxCapacity_FilterOutLowerPriorityMetrics)
  87. {
  88. MetricsQueue queue;
  89. for (int index = 0; index < NumTestMetrics; ++index)
  90. {
  91. MetricsEvent metrics;
  92. metrics.AddAttribute(MetricsAttribute(AttrName, AttrValue));
  93. metrics.SetEventPriority(index % 2);
  94. // Use the number of failures to check the order of metrics events later.
  95. // Older events will have less number of failures than the newer ones based on the settings in this test.
  96. for (int numRetries = 0; numRetries < index; ++numRetries)
  97. {
  98. metrics.MarkFailedSubmission();
  99. }
  100. queue.AddMetrics(metrics);
  101. }
  102. int maxCapacity = static_cast<int>(queue[0].GetSizeInBytes() * NumTestMetrics / 2);
  103. ASSERT_EQ(queue.FilterMetricsByPriority(maxCapacity), NumTestMetrics / 2);
  104. ASSERT_EQ(queue.GetNumMetrics(), NumTestMetrics / 2);
  105. for (int index = 0; index < queue.GetNumMetrics(); ++index)
  106. {
  107. ASSERT_EQ(queue[index].GetEventPriority(), 0);
  108. if (index > 0 && queue[index].GetEventPriority() == queue[index - 1].GetEventPriority())
  109. {
  110. // Check the order of metrics events in the queue.
  111. // Newer events should be placed in front of the older ones.
  112. ASSERT_LT(queue[index].GetNumFailures(), queue[index - 1].GetNumFailures());
  113. }
  114. }
  115. }
  116. TEST_F(MetricsQueueTest, ClearMetrics_NoneEmptyQueue_Success)
  117. {
  118. MetricsQueue queue;
  119. for (int index = 0; index < NumTestMetrics; ++index)
  120. {
  121. queue.AddMetrics(MetricsEvent());
  122. }
  123. ASSERT_EQ(queue.GetNumMetrics(), NumTestMetrics);
  124. queue.ClearMetrics();
  125. ASSERT_EQ(queue.GetNumMetrics(), 0);
  126. }
  127. TEST_F(MetricsQueueTest, SerializeToJsonForLocalFile_DefaultandCustomMetricsAttributes_Success)
  128. {
  129. MetricsQueue queue;
  130. MetricsEvent metrics;
  131. metrics.AddAttribute(MetricsAttribute(AttrName, AttrValue));
  132. metrics.AddAttribute(MetricsAttribute(AwsMetricsAttributeKeyEventName, AttrValue));
  133. queue.AddMetrics(metrics);
  134. AZStd::string serializedQueue = AZStd::string::format("[{\"%s\":\"%s\",\"%s\":{\"%s\":\"%s\"}}]",
  135. AwsMetricsAttributeKeyEventName, AttrValue.c_str(), AwsMetricsAttributeKeyEventData, AttrName.c_str(), AttrValue.c_str());
  136. ASSERT_EQ(queue.SerializeToJson(), serializedQueue);
  137. }
  138. TEST_F(MetricsQueueTest, SerializeToJsonForServiceApi_DefaultandCustomMetricsAttributes_Success)
  139. {
  140. MetricsQueue queue;
  141. MetricsEvent metrics;
  142. metrics.AddAttribute(MetricsAttribute(AttrName, AttrValue));
  143. metrics.AddAttribute(MetricsAttribute(AwsMetricsAttributeKeyEventName, AttrValue));
  144. queue.AddMetrics(metrics);
  145. std::ostream stream(nullptr);
  146. AWSCore::JsonOutputStream jsonStream{ stream };
  147. AWSCore::JsonWriter writer{ jsonStream };
  148. ASSERT_TRUE(queue.SerializeToJson(writer));
  149. }
  150. TEST_F(MetricsQueueTest, ReadFromJson_DefaultandCustomMetricsAttributes_Success)
  151. {
  152. MetricsQueue queue;
  153. AZStd::string testFilePath = GetDefaultTestFilePath();
  154. AZStd::string serializedQueue = AZStd::string::format("[{\"%s\":\"%s\",\"%s\":{\"%s\":\"%s\"}}]",
  155. AwsMetricsAttributeKeyEventName, AttrValue.c_str(), AwsMetricsAttributeKeyEventData, AttrName.c_str(), AttrValue.c_str());
  156. ASSERT_TRUE(CreateFile(testFilePath, serializedQueue));
  157. ASSERT_TRUE(queue.ReadFromJson(testFilePath));
  158. ASSERT_EQ(queue.GetNumMetrics(), 1);
  159. ASSERT_TRUE(RemoveFile(testFilePath));
  160. }
  161. TEST_F(MetricsQueueTest, ReadFromJson_InvalidJsonFilePath_Fail)
  162. {
  163. MetricsQueue queue;
  164. AZStd::string testFilePath = GetDefaultTestFilePath();
  165. AZ_TEST_START_TRACE_SUPPRESSION;
  166. ASSERT_FALSE(queue.ReadFromJson(testFilePath));
  167. AZ_TEST_STOP_TRACE_SUPPRESSION(1);
  168. }
  169. TEST_F(MetricsQueueTest, ReadFromJson_InvalidJsonFormat_Fail)
  170. {
  171. MetricsQueue queue;
  172. AZStd::string testFilePath = GetDefaultTestFilePath();
  173. AZStd::string serializedQueue = AZStd::string::format("{\"%s\":\"%s\",\"%s\":{\"%s\":\"%s\"}}",
  174. AwsMetricsAttributeKeyEventName, AttrValue.c_str(), AwsMetricsAttributeKeyEventData, AttrName.c_str(), AttrValue.c_str());
  175. ASSERT_TRUE(CreateFile(testFilePath, serializedQueue));
  176. AZ_TEST_START_TRACE_SUPPRESSION;
  177. ASSERT_FALSE(queue.ReadFromJson(testFilePath));
  178. AZ_TEST_STOP_TRACE_SUPPRESSION(1);
  179. ASSERT_TRUE(RemoveFile(testFilePath));
  180. }
  181. TEST_F(MetricsQueueTest, PopBufferedEventsByServiceLimits_QueueSizeExceedsLimits_BufferedEventsAddedToNewQueue)
  182. {
  183. MetricsEvent metrics;
  184. metrics.AddAttribute(MetricsAttribute(AttrName, AttrValue));
  185. int sizeOfEachMetrics = static_cast<int>(metrics.GetSizeInBytes());
  186. MetricsQueue queue;
  187. queue.AddMetrics(metrics);
  188. for (int index = 0; index < NumTestMetrics - 1; ++index)
  189. {
  190. MetricsEvent newMetrics;
  191. newMetrics.AddAttribute(MetricsAttribute(AttrName, AttrValue));
  192. queue.AddMetrics(newMetrics);
  193. }
  194. MetricsQueue anotherQueue;
  195. // Payload size limit is hit.
  196. queue.PopBufferedEventsByServiceLimits(anotherQueue, sizeOfEachMetrics, NumTestMetrics + 1);
  197. ASSERT_EQ(queue.GetNumMetrics(), NumTestMetrics - 1);
  198. ASSERT_EQ(queue.GetSizeInBytes(), sizeOfEachMetrics * (NumTestMetrics - 1));
  199. ASSERT_EQ(anotherQueue.GetNumMetrics(), 1);
  200. ASSERT_EQ(anotherQueue.GetSizeInBytes(), sizeOfEachMetrics);
  201. // Records count limit is hit.
  202. queue.PopBufferedEventsByServiceLimits(anotherQueue, sizeOfEachMetrics * NumTestMetrics, 1);
  203. ASSERT_EQ(queue.GetNumMetrics(), NumTestMetrics - 2);
  204. ASSERT_EQ(queue.GetSizeInBytes(), sizeOfEachMetrics * (NumTestMetrics - 2));
  205. ASSERT_EQ(anotherQueue.GetNumMetrics(), 2);
  206. ASSERT_EQ(anotherQueue.GetSizeInBytes(), sizeOfEachMetrics * 2);
  207. }
  208. }