diff options
author | Nan Zhou <nanzhoumails@gmail.com> | 2022-08-16 03:44:20 +0300 |
---|---|---|
committer | Ed Tanous <edtanous@google.com> | 2023-06-21 20:08:20 +0300 |
commit | 8a7c4b475469c8811dffe265992b903060aad65f (patch) | |
tree | d8b8bd3ba1c3129cc8232abd92c139a7d2586c9d /test/redfish-core | |
parent | 6fde5971ff53f43ece1aa3977b0689390934b06e (diff) | |
download | bmcweb-8a7c4b475469c8811dffe265992b903060aad65f.tar.xz |
json utils: add getEstimatedJsonSize
Add a utility function which estimates the size of the JSON tree.
It is used in the children change to limit the reponse size of expand
query.
Tested:
1. unit test passed;
2. tested on hardware, the following are real sizes and estimation
```
Real payload size, Estimation, query
15.69 KB, 10.21 KB, redfish/v1?$expand=.($levels=1)
95.76 KB, 62.11 KB, redfish/v1?$expand=.($levels=2)
117.14 KB, 72.71 KB, redfish/v1?$expand=.($levels=3)
127.65 KB, 77.64 KB, redfish/v1?$expand=.($levels=4)
```
Signed-off-by: Nan Zhou <nanzhoumails@gmail.com>
Change-Id: Iae26d6732a6ec63ecc59eacf657b4bf33c07c046
Diffstat (limited to 'test/redfish-core')
-rw-r--r-- | test/redfish-core/include/utils/json_utils_test.cpp | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/test/redfish-core/include/utils/json_utils_test.cpp b/test/redfish-core/include/utils/json_utils_test.cpp index 3fca4e72b1..5485bba91e 100644 --- a/test/redfish-core/include/utils/json_utils_test.cpp +++ b/test/redfish-core/include/utils/json_utils_test.cpp @@ -406,6 +406,60 @@ TEST(SortJsonArrayByKey, SortedByStringValueOnSuccessArrayIsSorted) R"({"@odata.id" : "/redfish/v1/20"})"_json, R"({"@odata.id" : "/redfish/v1/100"})"_json)); } +TEST(GetEstimatedJsonSize, NumberIs8Bytpes) +{ + EXPECT_EQ(getEstimatedJsonSize(nlohmann::json(123)), 8); + EXPECT_EQ(getEstimatedJsonSize(nlohmann::json(77777777777)), 8); + EXPECT_EQ(getEstimatedJsonSize(nlohmann::json(3.14)), 8); +} + +TEST(GetEstimatedJsonSize, BooleanIs5Byte) +{ + EXPECT_EQ(getEstimatedJsonSize(nlohmann::json(true)), 5); + EXPECT_EQ(getEstimatedJsonSize(nlohmann::json(false)), 5); +} + +TEST(GetEstimatedJsonSize, NullIs4Byte) +{ + EXPECT_EQ(getEstimatedJsonSize(nlohmann::json()), 4); +} + +TEST(GetEstimatedJsonSize, StringAndBytesReturnsLengthAndQuote) +{ + EXPECT_EQ(getEstimatedJsonSize(nlohmann::json("1234")), 6); + EXPECT_EQ(getEstimatedJsonSize(nlohmann::json::binary({1, 2, 3, 4})), 4); +} + +TEST(GetEstimatedJsonSize, ArrayReturnsSum) +{ + nlohmann::json arr = {1, 3.14, "123", nlohmann::json::binary({1, 2, 3, 4})}; + EXPECT_EQ(getEstimatedJsonSize(arr), 8 + 8 + 5 + 4); +} + +TEST(GetEstimatedJsonSize, ObjectsReturnsSumWithKeyAndValue) +{ + nlohmann::json obj = R"( +{ + "key0": 123, + "key1": "123", + "key2": [1, 2, 3], + "key3": {"key4": "123"} +} +)"_json; + + uint64_t expected = 0; + // 5 keys of length 4 + expected += uint64_t(5) * 4; + // 5 colons, 5 quote pairs, and 5 spaces for 5 keys + expected += uint64_t(5) * (1 + 2 + 1); + // 2 string values of length 3 + expected += uint64_t(2) * (3 + 2); + // 1 number value + expected += 8; + // 1 array value of 3 numbers + expected += uint64_t(3) * 8; + EXPECT_EQ(getEstimatedJsonSize(obj), expected); +} } // namespace } // namespace redfish::json_util |