1
0
mirror of https://github.com/pgbackrest/pgbackrest.git synced 2024-12-12 10:04:14 +02:00

Return stats as a JSON string rather than a KeyValue object.

There is no need to process the stats so a KeyValue is overkill.

Also remove the performance tests that check the stat totals since this is covered in the unit tests.
This commit is contained in:
David Steele 2022-04-14 20:34:42 -04:00
parent e1ce731f8a
commit d103dd6238
7 changed files with 35 additions and 39 deletions

View File

@ -207,10 +207,10 @@ cmdEnd(int code, const String *errorMessage)
MEM_CONTEXT_TEMP_BEGIN() MEM_CONTEXT_TEMP_BEGIN()
{ {
// Output statistics if there are any // Output statistics if there are any
const KeyValue *statKv = statToKv(); const String *const statJson = statToJson();
if (!varLstEmpty(kvKeyList(statKv))) if (statJson != NULL)
LOG_DETAIL_FMT("statistics: %s", strZ(jsonFromKv(statKv))); LOG_DETAIL_FMT("statistics: %s", strZ(statJson));
// Basic info on command end // Basic info on command end
String *info = strCatFmt(strNew(), "%s command end: ", strZ(cfgCommandRoleName())); String *info = strCatFmt(strNew(), "%s command end: ", strZ(cfgCommandRoleName()));

View File

@ -6,13 +6,9 @@ Statistics Collector
#include "common/debug.h" #include "common/debug.h"
#include "common/memContext.h" #include "common/memContext.h"
#include "common/stat.h" #include "common/stat.h"
#include "common/type/json.h"
#include "common/type/list.h" #include "common/type/list.h"
/***********************************************************************************************************************************
Stat output constants
***********************************************************************************************************************************/
VARIANT_STRDEF_EXTERN(STAT_VALUE_TOTAL_VAR, STAT_VALUE_TOTAL);
/*********************************************************************************************************************************** /***********************************************************************************************************************************
Cumulative statistics Cumulative statistics
***********************************************************************************************************************************/ ***********************************************************************************************************************************/
@ -106,21 +102,36 @@ statInc(const String *key)
} }
/**********************************************************************************************************************************/ /**********************************************************************************************************************************/
KeyValue * String *
statToKv(void) statToJson(void)
{ {
FUNCTION_TEST_VOID(); FUNCTION_TEST_VOID();
ASSERT(statLocalData.memContext != NULL); 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)); for (unsigned int statIdx = 0; statIdx < lstSize(statLocalData.stat); statIdx++)
kvPut(statKv, STAT_VALUE_TOTAL_VAR, VARUINT64(stat->total)); {
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); FUNCTION_TEST_RETURN(result);

View File

@ -12,13 +12,7 @@ iterations of a loop would likely be a bad idea.
#ifndef COMMON_STAT_H #ifndef COMMON_STAT_H
#define COMMON_STAT_H #define COMMON_STAT_H
#include "common/type/variant.h" #include "common/type/string.h"
/***********************************************************************************************************************************
Statistics output constants
***********************************************************************************************************************************/
#define STAT_VALUE_TOTAL "total"
VARIANT_DECLARE(STAT_VALUE_TOTAL_VAR);
/*********************************************************************************************************************************** /***********************************************************************************************************************************
Functions Functions
@ -29,7 +23,7 @@ void statInit(void);
// Increment stat by one // Increment stat by one
void statInc(const String *key); void statInc(const String *key);
// Output stats to a KeyValue // Output stats to JSON
KeyValue *statToKv(void); String *statToJson(void);
#endif #endif

View File

@ -782,7 +782,7 @@ testRun(void)
// ------------------------------------------------------------------------------------------------------------------------- // -------------------------------------------------------------------------------------------------------------------------
TEST_TITLE("statistics exist"); TEST_TITLE("statistics exist");
TEST_RESULT_BOOL(varLstEmpty(kvKeyList(statToKv())), false, "check"); TEST_RESULT_PTR_NE(statToJson(), NULL, "check");
} }
FUNCTION_HARNESS_RETURN_VOID(); FUNCTION_HARNESS_RETURN_VOID();

View File

@ -903,7 +903,7 @@ testRun(void)
// ------------------------------------------------------------------------------------------------------------------------- // -------------------------------------------------------------------------------------------------------------------------
TEST_TITLE("stastistics exist"); TEST_TITLE("stastistics exist");
TEST_RESULT_BOOL(varLstEmpty(kvKeyList(statToKv())), false, "check"); TEST_RESULT_PTR_NE(statToJson(), NULL, "check");
} }
FUNCTION_HARNESS_RETURN_VOID(); FUNCTION_HARNESS_RETURN_VOID();

View File

@ -19,6 +19,8 @@ testRun(void)
TEST_RESULT_UINT(lstSize(statLocalData.stat), 0, "stat list is empty"); 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_VOID(statInc(statTlsClient), "inc tls.client");
TEST_RESULT_UINT(lstSize(statLocalData.stat), 1, "stat list has one stat"); TEST_RESULT_UINT(lstSize(statLocalData.stat), 1, "stat list has one stat");
TEST_RESULT_VOID(statInc(statTlsClient), "inc tls.client"); TEST_RESULT_VOID(statInc(statTlsClient), "inc tls.client");
@ -26,7 +28,8 @@ testRun(void)
TEST_RESULT_VOID(statInc(statHttpSession), "inc http.session"); TEST_RESULT_VOID(statInc(statHttpSession), "inc http.session");
TEST_RESULT_UINT(lstSize(statLocalData.stat), 2, "stat list has two stats"); 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(); FUNCTION_HARNESS_RETURN_VOID();

View File

@ -352,18 +352,6 @@ testRun(void)
} }
TEST_LOG_FMT("completed in %ums", (unsigned int)(timeMSec() - timeBegin)); 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)));
}
} }
// ***************************************************************************************************************************** // *****************************************************************************************************************************