Files
o3de/Gems/AWSMetrics/Code/Tests/AWSMetricsServiceApiTest.cpp
T
lumberyard-employee-dm 098005afbc AZStd::basic_string improvements (#6438)
* AZStd::basic_string improvements

The AZStd::basic_string class has a better implementation of the Short
String Optimization, which increases the amount of characters that can
be stored in a `basic_string<char>` from 15 characters to 22
characters(not-including null-terminating characters). For a
`basic_string<wchar_t>` on Windows the amount of characters that can be
stored increases from 7 to 10. Using `basic_string<wchar_t>` on Unix
platforms SSO character amount from 3 to 4 characters.

An additional benefit is that the size of the AZStd::basic_string class
has been reduced from 40 bytes to 32 bytes when using the
AZStd::allocator.
When using a stateless allocator with no non static data members such as
AZStd::stateless_allocator, the size of the AZStd::basic_string is 24
bytes.

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Corrected comments and updated type alias to usings for AZStd::basic_string

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Added Benchmarks for the basic_string and basic_fixed_string class

The benchmarks currently measure the speed of the `assign` overloads.
A benchmark has also been added to compare the speed swapping two
`basic_string` instances by 3 memcpy vs 3 pointer swap operations

Speed up string operation when in the iterator overload cases of the
`assign`, `append`, `insert` and `replace` function.
The code was always performing the logic to copy over a string that is
overlapping, without actually checking if the string was overlapping in
the first place.

Added an `az_builtin_is_constant_evaluated` macro that allows use of the
C++20 `std::is_constant_evaluated` feature to determine if an operation
is being performed at compile time vs run time.

That macro is being used to speed up the char_trait operations at run
time, by using the faster standard library functions.
For example char_traits::move now uses "memmove" at runtime, instead of
a for loop.

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>

* Simplified string logic in AWSMetricsServiceApiTest.

Signed-off-by: lumberyard-employee-dm <56135373+lumberyard-employee-dm@users.noreply.github.com>
2022-01-10 10:03:31 -06:00

110 lines
4.3 KiB
C++

/*
* 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 <AWSMetricsServiceApi.h>
#include <AWSMetricsConstant.h>
#include <MetricsEventBuilder.h>
#include <Framework/JsonObjectHandler.h>
#include <AzCore/UnitTest/TestTypes.h>
namespace AWSMetrics
{
class JsonReaderMock
: public AWSCore::JsonReader
{
public:
virtual ~JsonReaderMock() = default;
MOCK_METHOD0(Ignore, bool());
MOCK_METHOD1(Accept, bool(bool& target));
MOCK_METHOD1(Accept, bool(AZStd::string& target));
MOCK_METHOD1(Accept, bool(int& target));
MOCK_METHOD1(Accept, bool(unsigned& target));
MOCK_METHOD1(Accept, bool(int64_t& target));
MOCK_METHOD1(Accept, bool(uint64_t& target));
MOCK_METHOD1(Accept, bool(double& target));
MOCK_METHOD1(Accept, bool(AWSCore::JsonKeyHandler keyHandler));
MOCK_METHOD1(Accept, bool(AWSCore::JsonArrayHandler arrayHandler));
};
class AWSMetricsServiceApiTest
: public UnitTest::ScopedAllocatorSetupFixture
{
public:
testing::NiceMock<JsonReaderMock> JsonReader;
};
TEST_F(AWSMetricsServiceApiTest, OnJsonKey_MetricsEventSuccessResponseRecord_AcceptValidKeys)
{
ServiceAPI::MetricsEventSuccessResponseRecord responseRecord;
responseRecord.result = "ok";
EXPECT_CALL(JsonReader, Accept(responseRecord.result)).Times(1);
EXPECT_CALL(JsonReader, Accept(responseRecord.errorCode)).Times(1);
EXPECT_CALL(JsonReader, Ignore()).Times(1);
responseRecord.OnJsonKey(AwsMetricsSuccessResponseRecordKeyResult, JsonReader);
responseRecord.OnJsonKey(AwsMetricsSuccessResponseRecordKeyErrorCode, JsonReader);
responseRecord.OnJsonKey("other", JsonReader);
}
TEST_F(AWSMetricsServiceApiTest, OnJsonKeyWithEvents_MetricsEventSuccessResponseRecord_AcceptValidKeys)
{
// Verifiy that JsonReader accepts valid JSON keys in each event record from a success reponse
ServiceAPI::MetricsEventSuccessResponseRecord responseRecord;
responseRecord.result = "ok";
ServiceAPI::MetricsEventSuccessResponse response;
response.events.emplace_back(responseRecord);
response.failedRecordCount = 0;
response.total = 1;
EXPECT_CALL(JsonReader, Accept(response.failedRecordCount)).Times(1);
EXPECT_CALL(JsonReader, Accept(response.total)).Times(1);
EXPECT_CALL(JsonReader, Accept(::testing::An<AWSCore::JsonArrayHandler>())).Times(1);
EXPECT_CALL(JsonReader, Ignore()).Times(1);
response.OnJsonKey(AwsMetricsSuccessResponseKeyFailedRecordCount, JsonReader);
response.OnJsonKey(AwsMetricsSuccessResponseKeyTotal, JsonReader);
response.OnJsonKey(AwsMetricsSuccessResponseKeyEvents, JsonReader);
response.OnJsonKey("other", JsonReader);
}
TEST_F(AWSMetricsServiceApiTest, OnJsonKey_Error_AcceptValidKeys)
{
ServiceAPI::Error error;
error.message = "error message";
error.type = "404";
EXPECT_CALL(JsonReader, Accept(error.message)).Times(1);
EXPECT_CALL(JsonReader, Accept(error.type)).Times(1);
EXPECT_CALL(JsonReader, Ignore()).Times(1);
error.OnJsonKey(AwsMetricsErrorKeyMessage, JsonReader);
error.OnJsonKey(AwsMetricsErrorKeyType, JsonReader);
error.OnJsonKey("other", JsonReader);
}
TEST_F(AWSMetricsServiceApiTest, BuildRequestBody_PostProducerEventsRequest_SerializedMetricsQueue)
{
ServiceAPI::PostProducerEventsRequest request;
request.parameters.data = MetricsQueue();
request.parameters.data.AddMetrics(MetricsEventBuilder().Build());
AWSCore::RequestBuilder requestBuilder{};
EXPECT_TRUE(request.parameters.BuildRequest(requestBuilder));
std::shared_ptr<Aws::StringStream> bodyContent = requestBuilder.GetBodyContent();
ASSERT_NE(nullptr, bodyContent);
std::istreambuf_iterator<AZStd::string::value_type> eos;
AZStd::string bodyString{ std::istreambuf_iterator<AZStd::string::value_type>(*bodyContent), eos };
EXPECT_TRUE(bodyString.contains(AZStd::string::format("{\"%s\":[{\"event_timestamp\":", AwsMetricsRequestParameterKeyEvents)));
}
}