123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266 |
- /*
- * Copyright (c) Contributors to the Open 3D Engine Project.
- * For complete copyright and license terms please see the LICENSE at the root of this distribution.
- *
- * SPDX-License-Identifier: Apache-2.0 OR MIT
- *
- */
- #include <AWSMetricsConstant.h>
- #include <AWSMetricsGemMock.h>
- #include <Framework/JsonWriter.h>
- #include <MetricsEvent.h>
- #include <MetricsQueue.h>
- #include <AzTest/AzTest.h>
- #include <AzCore/UnitTest/TestTypes.h>
- #include <AzFramework/StringFunc/StringFunc.h>
- namespace AWSMetrics
- {
- class MetricsQueueTest
- : public AWSMetricsGemAllocatorFixture
- {
- public:
- const int NumTestMetrics = 10;
- const AZStd::string AttrName = "name";
- const AZStd::string AttrValue = "value";
- };
- TEST_F(MetricsQueueTest, AddMetrics_SingleMetrics_Success)
- {
- MetricsQueue queue;
- int numMetrics = queue.GetNumMetrics();
- queue.AddMetrics(MetricsEvent());
- ASSERT_EQ(queue.GetNumMetrics(), ++numMetrics);
- }
- TEST_F(MetricsQueueTest, AppendMetrics_EmptyQueue_Success)
- {
- MetricsQueue queue;
- int numMetrics = queue.GetNumMetrics();
- MetricsQueue anotherQueue;
- for (int index = 0; index < NumTestMetrics; ++index)
- {
- anotherQueue.AddMetrics(MetricsEvent());
- }
-
- queue.AppendMetrics(anotherQueue);
- ASSERT_EQ(queue.GetNumMetrics(), numMetrics + NumTestMetrics);
- }
- TEST_F(MetricsQueueTest, AppendMetrics_NoneEmptyQueue_Success)
- {
- MetricsQueue queue;
- queue.AddMetrics(MetricsEvent());
- int numMetrics = queue.GetNumMetrics();
- MetricsQueue anotherQueue;
- for (int index = 0; index < NumTestMetrics; ++index)
- {
- anotherQueue.AddMetrics(MetricsEvent());
- }
- queue.AppendMetrics(anotherQueue);
- ASSERT_EQ(queue.GetNumMetrics(), numMetrics + NumTestMetrics);
- }
- TEST_F(MetricsQueueTest, PushMetricsToFront_EmptyQueue_Success)
- {
- MetricsQueue queue;
- int numMetrics = queue.GetNumMetrics();
- MetricsQueue anotherQueue;
- for (int index = 0; index < NumTestMetrics; ++index)
- {
- anotherQueue.AddMetrics(MetricsEvent());
- }
- queue.PushMetricsToFront(anotherQueue);
- ASSERT_EQ(queue.GetNumMetrics(), numMetrics + NumTestMetrics);
- }
- TEST_F(MetricsQueueTest, PushMetricsToFront_NoneEmptyQueue_Success)
- {
- MetricsQueue queue;
- MetricsEvent metrics;
- metrics.AddAttribute(MetricsAttribute(AttrName, AttrValue));
- queue.AddMetrics(metrics);
- int numMetrics = queue.GetNumMetrics();
- MetricsQueue anotherQueue;
- for (int index = 0; index < NumTestMetrics; ++index)
- {
- anotherQueue.AddMetrics(MetricsEvent());
- }
- queue.PushMetricsToFront(anotherQueue);
- ASSERT_EQ(queue.GetNumMetrics(), numMetrics + NumTestMetrics);
- ASSERT_EQ(queue[NumTestMetrics].GetNumAttributes(), 1);
- }
- TEST_F(MetricsQueueTest, FilterMetricsByPriority_ReachMaxCapacity_FilterOutLowerPriorityMetrics)
- {
- MetricsQueue queue;
- for (int index = 0; index < NumTestMetrics; ++index)
- {
- MetricsEvent metrics;
- metrics.AddAttribute(MetricsAttribute(AttrName, AttrValue));
- metrics.SetEventPriority(index % 2);
- // Use the number of failures to check the order of metrics events later.
- // Older events will have less number of failures than the newer ones based on the settings in this test.
- for (int numRetries = 0; numRetries < index; ++numRetries)
- {
- metrics.MarkFailedSubmission();
- }
- queue.AddMetrics(metrics);
- }
- int maxCapacity = static_cast<int>(queue[0].GetSizeInBytes() * NumTestMetrics / 2);
- ASSERT_EQ(queue.FilterMetricsByPriority(maxCapacity), NumTestMetrics / 2);
- ASSERT_EQ(queue.GetNumMetrics(), NumTestMetrics / 2);
- for (int index = 0; index < queue.GetNumMetrics(); ++index)
- {
- ASSERT_EQ(queue[index].GetEventPriority(), 0);
- if (index > 0 && queue[index].GetEventPriority() == queue[index - 1].GetEventPriority())
- {
- // Check the order of metrics events in the queue.
- // Newer events should be placed in front of the older ones.
- ASSERT_LT(queue[index].GetNumFailures(), queue[index - 1].GetNumFailures());
- }
- }
- }
- TEST_F(MetricsQueueTest, ClearMetrics_NoneEmptyQueue_Success)
- {
- MetricsQueue queue;
- for (int index = 0; index < NumTestMetrics; ++index)
- {
- queue.AddMetrics(MetricsEvent());
- }
- ASSERT_EQ(queue.GetNumMetrics(), NumTestMetrics);
- queue.ClearMetrics();
- ASSERT_EQ(queue.GetNumMetrics(), 0);
- }
- TEST_F(MetricsQueueTest, SerializeToJsonForLocalFile_DefaultandCustomMetricsAttributes_Success)
- {
- MetricsQueue queue;
- MetricsEvent metrics;
- metrics.AddAttribute(MetricsAttribute(AttrName, AttrValue));
- metrics.AddAttribute(MetricsAttribute(AwsMetricsAttributeKeyEventName, AttrValue));
- queue.AddMetrics(metrics);
- AZStd::string serializedQueue = AZStd::string::format("[{\"%s\":\"%s\",\"%s\":{\"%s\":\"%s\"}}]",
- AwsMetricsAttributeKeyEventName, AttrValue.c_str(), AwsMetricsAttributeKeyEventData, AttrName.c_str(), AttrValue.c_str());
- ASSERT_EQ(queue.SerializeToJson(), serializedQueue);
- }
- TEST_F(MetricsQueueTest, SerializeToJsonForServiceApi_DefaultandCustomMetricsAttributes_Success)
- {
- MetricsQueue queue;
- MetricsEvent metrics;
- metrics.AddAttribute(MetricsAttribute(AttrName, AttrValue));
- metrics.AddAttribute(MetricsAttribute(AwsMetricsAttributeKeyEventName, AttrValue));
- queue.AddMetrics(metrics);
- std::ostream stream(nullptr);
- AWSCore::JsonOutputStream jsonStream{ stream };
- AWSCore::JsonWriter writer{ jsonStream };
- ASSERT_TRUE(queue.SerializeToJson(writer));
- }
- TEST_F(MetricsQueueTest, ReadFromJson_DefaultandCustomMetricsAttributes_Success)
- {
- MetricsQueue queue;
- AZStd::string testFilePath = GetDefaultTestFilePath();
- AZStd::string serializedQueue = AZStd::string::format("[{\"%s\":\"%s\",\"%s\":{\"%s\":\"%s\"}}]",
- AwsMetricsAttributeKeyEventName, AttrValue.c_str(), AwsMetricsAttributeKeyEventData, AttrName.c_str(), AttrValue.c_str());
- ASSERT_TRUE(CreateFile(testFilePath, serializedQueue));
-
- ASSERT_TRUE(queue.ReadFromJson(testFilePath));
- ASSERT_EQ(queue.GetNumMetrics(), 1);
- ASSERT_TRUE(RemoveFile(testFilePath));
- }
- TEST_F(MetricsQueueTest, ReadFromJson_InvalidJsonFilePath_Fail)
- {
- MetricsQueue queue;
- AZStd::string testFilePath = GetDefaultTestFilePath();
- AZ_TEST_START_TRACE_SUPPRESSION;
- ASSERT_FALSE(queue.ReadFromJson(testFilePath));
- AZ_TEST_STOP_TRACE_SUPPRESSION(1);
- }
- TEST_F(MetricsQueueTest, ReadFromJson_InvalidJsonFormat_Fail)
- {
- MetricsQueue queue;
- AZStd::string testFilePath = GetDefaultTestFilePath();
- AZStd::string serializedQueue = AZStd::string::format("{\"%s\":\"%s\",\"%s\":{\"%s\":\"%s\"}}",
- AwsMetricsAttributeKeyEventName, AttrValue.c_str(), AwsMetricsAttributeKeyEventData, AttrName.c_str(), AttrValue.c_str());
- ASSERT_TRUE(CreateFile(testFilePath, serializedQueue));
- AZ_TEST_START_TRACE_SUPPRESSION;
- ASSERT_FALSE(queue.ReadFromJson(testFilePath));
- AZ_TEST_STOP_TRACE_SUPPRESSION(1);
- ASSERT_TRUE(RemoveFile(testFilePath));
- }
- TEST_F(MetricsQueueTest, PopBufferedEventsByServiceLimits_QueueSizeExceedsLimits_BufferedEventsAddedToNewQueue)
- {
- MetricsEvent metrics;
- metrics.AddAttribute(MetricsAttribute(AttrName, AttrValue));
- int sizeOfEachMetrics = static_cast<int>(metrics.GetSizeInBytes());
- MetricsQueue queue;
- queue.AddMetrics(metrics);
-
- for (int index = 0; index < NumTestMetrics - 1; ++index)
- {
- MetricsEvent newMetrics;
- newMetrics.AddAttribute(MetricsAttribute(AttrName, AttrValue));
- queue.AddMetrics(newMetrics);
- }
- MetricsQueue anotherQueue;
- // Payload size limit is hit.
- queue.PopBufferedEventsByServiceLimits(anotherQueue, sizeOfEachMetrics, NumTestMetrics + 1);
- ASSERT_EQ(queue.GetNumMetrics(), NumTestMetrics - 1);
- ASSERT_EQ(queue.GetSizeInBytes(), sizeOfEachMetrics * (NumTestMetrics - 1));
- ASSERT_EQ(anotherQueue.GetNumMetrics(), 1);
- ASSERT_EQ(anotherQueue.GetSizeInBytes(), sizeOfEachMetrics);
- // Records count limit is hit.
- queue.PopBufferedEventsByServiceLimits(anotherQueue, sizeOfEachMetrics * NumTestMetrics, 1);
- ASSERT_EQ(queue.GetNumMetrics(), NumTestMetrics - 2);
- ASSERT_EQ(queue.GetSizeInBytes(), sizeOfEachMetrics * (NumTestMetrics - 2));
- ASSERT_EQ(anotherQueue.GetNumMetrics(), 2);
- ASSERT_EQ(anotherQueue.GetSizeInBytes(), sizeOfEachMetrics * 2);
- }
- }
|