1
0
mirror of https://github.com/pgbackrest/pgbackrest.git synced 2025-07-07 00:35:37 +02:00

Print total runtime in milliseconds at command end.

This commit is contained in:
David Steele
2018-05-18 12:03:39 -04:00
parent 52bc073234
commit 72a9684d4e
5 changed files with 54 additions and 2 deletions

View File

@ -22,6 +22,10 @@
<p>Use <code>time_t</code> instead of <code>__time_t</code> for better portability.</p> <p>Use <code>time_t</code> instead of <code>__time_t</code> for better portability.</p>
</release-item> </release-item>
<release-item>
<p>Print total runtime in milliseconds at command end.</p>
</release-item>
</release-improvement-list> </release-improvement-list>
<release-development-list> <release-development-list>

View File

@ -1,15 +1,35 @@
/*********************************************************************************************************************************** /***********************************************************************************************************************************
Common Command Routines Common Command Routines
***********************************************************************************************************************************/ ***********************************************************************************************************************************/
#include <inttypes.h>
#include <string.h> #include <string.h>
#include "common/assert.h" #include "common/assert.h"
#include "common/debug.h" #include "common/debug.h"
#include "common/log.h" #include "common/log.h"
#include "common/memContext.h" #include "common/memContext.h"
#include "common/time.h"
#include "config/config.h" #include "config/config.h"
#include "version.h" #include "version.h"
/***********************************************************************************************************************************
Track time command started
***********************************************************************************************************************************/
static TimeMSec timeBegin;
/***********************************************************************************************************************************
Capture time at the very start of main so total time is more accurate
***********************************************************************************************************************************/
void
cmdInit()
{
FUNCTION_DEBUG_VOID(logLevelTrace);
timeBegin = timeMSec();
FUNCTION_DEBUG_RESULT_VOID();
}
/*********************************************************************************************************************************** /***********************************************************************************************************************************
Begin the command Begin the command
***********************************************************************************************************************************/ ***********************************************************************************************************************************/
@ -163,7 +183,12 @@ cmdEnd(int code, const String *errorMessage)
String *info = strNewFmt("%s command end: ", cfgCommandName(cfgCommand())); String *info = strNewFmt("%s command end: ", cfgCommandName(cfgCommand()));
if (code == 0) if (code == 0)
{
strCat(info, "completed successfully"); strCat(info, "completed successfully");
if (cfgOptionValid(cfgOptLogTimestamp) && cfgOptionBool(cfgOptLogTimestamp))
strCatFmt(info, " (%" PRIu64 "ms)", timeMSec() - timeBegin);
}
else else
strCat(info, strPtr(errorMessage)); strCat(info, strPtr(errorMessage));
@ -172,5 +197,8 @@ cmdEnd(int code, const String *errorMessage)
MEM_CONTEXT_TEMP_END(); MEM_CONTEXT_TEMP_END();
} }
// Reset timeBegin in case there is another command following this one
timeBegin = timeMSec();
FUNCTION_DEBUG_RESULT_VOID(); FUNCTION_DEBUG_RESULT_VOID();
} }

View File

@ -9,6 +9,8 @@ Common Command Routines
/*********************************************************************************************************************************** /***********************************************************************************************************************************
Functions Functions
***********************************************************************************************************************************/ ***********************************************************************************************************************************/
void cmdInit();
void cmdBegin(bool logOption); void cmdBegin(bool logOption);
void cmdEnd(int code, const String *errorMessage); void cmdEnd(int code, const String *errorMessage);

View File

@ -28,6 +28,9 @@ main(int argListSize, const char *argList[])
FUNCTION_DEBUG_PARAM(CHARPY, argList); FUNCTION_DEBUG_PARAM(CHARPY, argList);
FUNCTION_DEBUG_END(); FUNCTION_DEBUG_END();
// Initialize command with the start time
cmdInit();
volatile bool result = 0; volatile bool result = 0;
volatile bool error = false; volatile bool error = false;

View File

@ -18,6 +18,8 @@ testRun()
// ***************************************************************************************************************************** // *****************************************************************************************************************************
if (testBegin("cmdBegin() and cmdEnd()")) if (testBegin("cmdBegin() and cmdEnd()"))
{ {
cmdInit();
// ------------------------------------------------------------------------------------------------------------------------- // -------------------------------------------------------------------------------------------------------------------------
cfgInit(); cfgInit();
cfgCommandSet(cfgCmdArchiveGet); cfgCommandSet(cfgCmdArchiveGet);
@ -95,13 +97,26 @@ testRun()
TEST_RESULT_VOID(cmdEnd(0, NULL), "command end no logging"); TEST_RESULT_VOID(cmdEnd(0, NULL), "command end no logging");
logInit(logLevelInfo, logLevelOff, logLevelOff, false); logInit(logLevelInfo, logLevelOff, logLevelOff, false);
// -------------------------------------------------------------------------------------------------------------------------
TEST_RESULT_VOID(cmdEnd(25, strNew("aborted with exception [025]")), "command end with error");
testLogResult("P00 INFO: archive-get command end: aborted with exception [025]");
// ------------------------------------------------------------------------------------------------------------------------- // -------------------------------------------------------------------------------------------------------------------------
TEST_RESULT_VOID(cmdEnd(0, NULL), "command end with success"); TEST_RESULT_VOID(cmdEnd(0, NULL), "command end with success");
testLogResult("P00 INFO: archive-get command end: completed successfully"); testLogResult("P00 INFO: archive-get command end: completed successfully");
// Make sure time output is covered but don't do expect testing since the output is variable
// ------------------------------------------------------------------------------------------------------------------------- // -------------------------------------------------------------------------------------------------------------------------
TEST_RESULT_VOID(cmdEnd(25, strNew("aborted with exception [025]")), "command end with error"); cfgOptionValidSet(cfgOptLogTimestamp, true);
testLogResult("P00 INFO: archive-get command end: aborted with exception [025]"); cfgOptionSet(cfgOptLogTimestamp, cfgSourceParam, varNewBool(false));
TEST_RESULT_VOID(cmdEnd(0, NULL), "command end with success");
testLogResult("P00 INFO: archive-get command end: completed successfully");
cfgOptionSet(cfgOptLogTimestamp, cfgSourceParam, varNewBool(true));
TEST_RESULT_VOID(cmdEnd(0, NULL), "command end with success");
testLogResultRegExp("P00 INFO\\: archive-get command end: completed successfully \\([0-9]+ms\\)");
} }
FUNCTION_HARNESS_RESULT_VOID(); FUNCTION_HARNESS_RESULT_VOID();