diff --git a/src/command/command.c b/src/command/command.c index a07defaaf..deb78345e 100644 --- a/src/command/command.c +++ b/src/command/command.c @@ -207,10 +207,10 @@ cmdEnd(int code, const String *errorMessage) MEM_CONTEXT_TEMP_BEGIN() { // Output statistics if there are any - const KeyValue *statKv = statToKv(); + const String *const statJson = statToJson(); - if (!varLstEmpty(kvKeyList(statKv))) - LOG_DETAIL_FMT("statistics: %s", strZ(jsonFromKv(statKv))); + if (statJson != NULL) + LOG_DETAIL_FMT("statistics: %s", strZ(statJson)); // Basic info on command end String *info = strCatFmt(strNew(), "%s command end: ", strZ(cfgCommandRoleName())); diff --git a/src/common/stat.c b/src/common/stat.c index 54279b11d..a00d61fef 100644 --- a/src/common/stat.c +++ b/src/common/stat.c @@ -6,13 +6,9 @@ Statistics Collector #include "common/debug.h" #include "common/memContext.h" #include "common/stat.h" +#include "common/type/json.h" #include "common/type/list.h" -/*********************************************************************************************************************************** -Stat output constants -***********************************************************************************************************************************/ -VARIANT_STRDEF_EXTERN(STAT_VALUE_TOTAL_VAR, STAT_VALUE_TOTAL); - /*********************************************************************************************************************************** Cumulative statistics ***********************************************************************************************************************************/ @@ -106,21 +102,36 @@ statInc(const String *key) } /**********************************************************************************************************************************/ -KeyValue * -statToKv(void) +String * +statToJson(void) { FUNCTION_TEST_VOID(); ASSERT(statLocalData.memContext != NULL); - KeyValue *result = kvNew(); + String *result = NULL; - for (unsigned int statIdx = 0; statIdx < lstSize(statLocalData.stat); statIdx++) + if (!lstEmpty(statLocalData.stat)) { - Stat *stat = lstGet(statLocalData.stat, statIdx); + MEM_CONTEXT_TEMP_BEGIN() + { + KeyValue *const kv = kvNew(); - KeyValue *statKv = kvPutKv(result, VARSTR(stat->key)); - kvPut(statKv, STAT_VALUE_TOTAL_VAR, VARUINT64(stat->total)); + for (unsigned int statIdx = 0; statIdx < lstSize(statLocalData.stat); statIdx++) + { + const Stat *const stat = lstGet(statLocalData.stat, statIdx); + + KeyValue *const statKv = kvPutKv(kv, VARSTR(stat->key)); + kvPut(statKv, VARSTRDEF("total"), VARUINT64(stat->total)); + } + + MEM_CONTEXT_PRIOR_BEGIN() + { + result = jsonFromKv(kv); + } + MEM_CONTEXT_PRIOR_END(); + } + MEM_CONTEXT_TEMP_END(); } FUNCTION_TEST_RETURN(result); diff --git a/src/common/stat.h b/src/common/stat.h index 6b6c93124..88ba67cc9 100644 --- a/src/common/stat.h +++ b/src/common/stat.h @@ -12,13 +12,7 @@ iterations of a loop would likely be a bad idea. #ifndef COMMON_STAT_H #define COMMON_STAT_H -#include "common/type/variant.h" - -/*********************************************************************************************************************************** -Statistics output constants -***********************************************************************************************************************************/ -#define STAT_VALUE_TOTAL "total" - VARIANT_DECLARE(STAT_VALUE_TOTAL_VAR); +#include "common/type/string.h" /*********************************************************************************************************************************** Functions @@ -29,7 +23,7 @@ void statInit(void); // Increment stat by one void statInc(const String *key); -// Output stats to a KeyValue -KeyValue *statToKv(void); +// Output stats to JSON +String *statToJson(void); #endif diff --git a/test/src/module/common/ioHttpTest.c b/test/src/module/common/ioHttpTest.c index 47dd0edbf..dcc06080d 100644 --- a/test/src/module/common/ioHttpTest.c +++ b/test/src/module/common/ioHttpTest.c @@ -782,7 +782,7 @@ testRun(void) // ------------------------------------------------------------------------------------------------------------------------- TEST_TITLE("statistics exist"); - TEST_RESULT_BOOL(varLstEmpty(kvKeyList(statToKv())), false, "check"); + TEST_RESULT_PTR_NE(statToJson(), NULL, "check"); } FUNCTION_HARNESS_RETURN_VOID(); diff --git a/test/src/module/common/ioTlsTest.c b/test/src/module/common/ioTlsTest.c index 9d5e1cf8d..0dbf962b9 100644 --- a/test/src/module/common/ioTlsTest.c +++ b/test/src/module/common/ioTlsTest.c @@ -903,7 +903,7 @@ testRun(void) // ------------------------------------------------------------------------------------------------------------------------- TEST_TITLE("stastistics exist"); - TEST_RESULT_BOOL(varLstEmpty(kvKeyList(statToKv())), false, "check"); + TEST_RESULT_PTR_NE(statToJson(), NULL, "check"); } FUNCTION_HARNESS_RETURN_VOID(); diff --git a/test/src/module/common/statTest.c b/test/src/module/common/statTest.c index a30c1b5e9..2fcec0352 100644 --- a/test/src/module/common/statTest.c +++ b/test/src/module/common/statTest.c @@ -19,6 +19,8 @@ testRun(void) TEST_RESULT_UINT(lstSize(statLocalData.stat), 0, "stat list is empty"); + TEST_RESULT_STR_Z(statToJson(), NULL, "no stats yet"); + TEST_RESULT_VOID(statInc(statTlsClient), "inc tls.client"); TEST_RESULT_UINT(lstSize(statLocalData.stat), 1, "stat list has one stat"); TEST_RESULT_VOID(statInc(statTlsClient), "inc tls.client"); @@ -26,7 +28,8 @@ testRun(void) TEST_RESULT_VOID(statInc(statHttpSession), "inc http.session"); TEST_RESULT_UINT(lstSize(statLocalData.stat), 2, "stat list has two stats"); - TEST_RESULT_STR_Z(jsonFromKv(statToKv()), "{\"http.session\":{\"total\":1},\"tls.client\":{\"total\":2}}", "stat output"); + TEST_RESULT_STR_Z( + statToJson(), "{\"http.session\":{\"total\":1},\"tls.client\":{\"total\":2}}", "stat output"); } FUNCTION_HARNESS_RETURN_VOID(); diff --git a/test/src/module/performance/typeTest.c b/test/src/module/performance/typeTest.c index 37a21e68b..d20b5e18e 100644 --- a/test/src/module/performance/typeTest.c +++ b/test/src/module/performance/typeTest.c @@ -352,18 +352,6 @@ testRun(void) } TEST_LOG_FMT("completed in %ums", (unsigned int)(timeMSec() - timeBegin)); - - // ------------------------------------------------------------------------------------------------------------------------- - TEST_TITLE("check stats have correct values"); - - KeyValue *statKv = statToKv(); - - for (unsigned int statIdx = 0; statIdx < TEST_STAT_TOTAL; statIdx++) - { - TEST_RESULT_UINT( - varUInt64(kvGet(varKv(kvGet(statKv, VARSTR(statList[statIdx]))), STAT_VALUE_TOTAL_VAR)), runTotal, - strZ(strNewFmt("check stat %u", statIdx))); - } } // *****************************************************************************************************************************