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()
{
// 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()));

View File

@ -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);

View File

@ -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

View File

@ -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();

View File

@ -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();

View File

@ -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();

View File

@ -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)));
}
}
// *****************************************************************************************************************************