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 /redfish-core/include | |
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 'redfish-core/include')
-rw-r--r-- | redfish-core/include/utils/json_utils.hpp | 14 |
1 files changed, 14 insertions, 0 deletions
diff --git a/redfish-core/include/utils/json_utils.hpp b/redfish-core/include/utils/json_utils.hpp index 6e18157fc0..642ca802eb 100644 --- a/redfish-core/include/utils/json_utils.hpp +++ b/redfish-core/include/utils/json_utils.hpp @@ -16,6 +16,7 @@ #pragma once #include "error_messages.hpp" +#include "http_connection.hpp" #include "http_request.hpp" #include "http_response.hpp" #include "human_sort.hpp" @@ -673,5 +674,18 @@ inline void sortJsonArrayByOData(nlohmann::json::array_t& array) std::sort(array.begin(), array.end(), ODataObjectLess()); } +// Returns the estimated size of the JSON value +// The implementation walks through every key and every value, accumulates the +// total size of keys and values. +// Ideally, we should use a custom allocator that nlohmann JSON supports. + +// Assumption made: +// 1. number: 8 characters +// 2. boolean: 5 characters (False) +// 3. string: len(str) + 2 characters (quote) +// 4. bytes: len(bytes) characters +// 5. null: 4 characters (null) +uint64_t getEstimatedJsonSize(const nlohmann::json& root); + } // namespace json_util } // namespace redfish |