1
0
mirror of https://github.com/facebook/zstd.git synced 2025-03-06 16:56:49 +02:00

bench: minor api update, for consistency

BMK_benchTimedFn()
BMK_isCompleted_TimedFn() uses TimedFnState
This commit is contained in:
Yann Collet 2018-08-26 21:30:18 -07:00
parent c3a4baaf6e
commit 01dcd0fd17
4 changed files with 43 additions and 40 deletions

View File

@ -278,17 +278,25 @@ static size_t local_defaultDecompress(
int BMK_isSuccessful_runOutcome(BMK_runOutcome_t outcome)
{
return outcome.tag < 2;
return outcome.tag == 0;
}
/* warning : this function will stop program execution if outcome is invalid !
* check outcome validity first, using BMK_isValid_runResult() */
BMK_runTime_t BMK_extract_runTime(BMK_runOutcome_t outcome)
{
assert(outcome.tag < 2);
assert(outcome.tag == 0);
return outcome.internal_never_use_directly;
}
static BMK_runOutcome_t BMK_runOutcome_error(void)
{
BMK_runOutcome_t b;
memset(&b, 0, sizeof(b));
b.tag = 1;
return b;
}
static BMK_runOutcome_t BMK_setValid_runTime(BMK_runTime_t runTime)
{
BMK_runOutcome_t outcome;
@ -382,6 +390,7 @@ BMK_timedFnState_t* BMK_createTimedFnState(unsigned nbSeconds) {
void BMK_resetTimedFnState(BMK_timedFnState_t* r, unsigned nbSeconds) {
r->timeSpent_ns = 0;
r->timeBudget_ns = (U64)nbSeconds * TIMELOOP_NANOSEC;
if (!nbSeconds) r->timeBudget_ns = 1;
r->fastestRun.nanoSecPerRun = (U64)(-1LL);
r->fastestRun.sumOfReturn = (size_t)(-1LL);
r->nbLoops = 1;
@ -395,15 +404,15 @@ void BMK_freeTimedFnState(BMK_timedFnState_t* state) {
/* Tells if nb of seconds set in timedFnState for all runs is spent.
* note : this function will return 1 if BMK_benchFunctionTimed() has actually errored. */
int BMK_isCompleted_runOutcome(BMK_runOutcome_t outcome)
int BMK_isCompleted_TimedFn(const BMK_timedFnState_t* timedFnState)
{
return (outcome.tag >= 1);
return (timedFnState->timeSpent_ns >= timedFnState->timeBudget_ns);
}
#define MINUSABLETIME (TIMELOOP_NANOSEC / 2) /* 0.5 seconds */
BMK_runOutcome_t BMK_benchFunctionTimed(
BMK_runOutcome_t BMK_benchTimedFn(
BMK_timedFnState_t* cont,
BMK_benchFn_t benchFn, void* benchPayload,
BMK_initFn_t initFn, void* initPayload,
@ -413,11 +422,8 @@ BMK_runOutcome_t BMK_benchFunctionTimed(
size_t* blockResults)
{
int completed = 0;
BMK_runOutcome_t r;
BMK_runTime_t bestRunTime = cont->fastestRun;
r.tag = 2; /* error by default */
while (!completed) {
BMK_runOutcome_t runResult;
@ -438,8 +444,7 @@ BMK_runOutcome_t BMK_benchFunctionTimed(
cont->nbLoops);
if(!BMK_isSuccessful_runOutcome(runResult)) { /* error : move out */
r.tag = 2;
return r;
return BMK_runOutcome_error();
}
{ BMK_runTime_t const newRunTime = BMK_extract_runTime(runResult);
@ -471,9 +476,7 @@ BMK_runOutcome_t BMK_benchFunctionTimed(
}
} /* while (!completed) */
r.tag = (cont->timeSpent_ns >= cont->timeBudget_ns); /* report if time budget is spent */
r.internal_never_use_directly = bestRunTime;
return r;
return BMK_setValid_runTime(bestRunTime);
}
@ -624,13 +627,13 @@ static BMK_benchOutcome_t BMK_benchMemAdvancedNoAlloc(
if (!compressionCompleted) {
BMK_runOutcome_t const cOutcome =
BMK_benchFunctionTimed( timeStateCompress,
&local_defaultCompress, cctx,
&local_initCCtx, &cctxprep,
nbBlocks,
srcPtrs, srcSizes,
cPtrs, cCapacities,
cSizes);
BMK_benchTimedFn( timeStateCompress,
&local_defaultCompress, cctx,
&local_initCCtx, &cctxprep,
nbBlocks,
srcPtrs, srcSizes,
cPtrs, cCapacities,
cSizes);
if (!BMK_isSuccessful_runOutcome(cOutcome)) {
return BMK_benchOutcome_error();
@ -654,18 +657,18 @@ static BMK_benchOutcome_t BMK_benchMemAdvancedNoAlloc(
ratioAccuracy, ratio,
benchResult.cSpeed < (10 MB) ? 2 : 1, (double)benchResult.cSpeed / MB_UNIT);
}
compressionCompleted = BMK_isCompleted_runOutcome(cOutcome);
compressionCompleted = BMK_isCompleted_TimedFn(timeStateCompress);
}
if(!decompressionCompleted) {
BMK_runOutcome_t const dOutcome =
BMK_benchFunctionTimed(timeStateDecompress,
&local_defaultDecompress, dctx,
&local_initDCtx, &dctxprep,
nbBlocks,
(const void *const *)cPtrs, cSizes,
resPtrs, resSizes,
NULL);
BMK_benchTimedFn(timeStateDecompress,
&local_defaultDecompress, dctx,
&local_initDCtx, &dctxprep,
nbBlocks,
(const void *const *)cPtrs, cSizes,
resPtrs, resSizes,
NULL);
if(!BMK_isSuccessful_runOutcome(dOutcome)) {
return BMK_benchOutcome_error();
@ -686,7 +689,7 @@ static BMK_benchOutcome_t BMK_benchMemAdvancedNoAlloc(
benchResult.cSpeed < (10 MB) ? 2 : 1, (double)benchResult.cSpeed / MB_UNIT,
(double)benchResult.dSpeed / MB_UNIT);
}
decompressionCompleted = BMK_isCompleted_runOutcome(dOutcome);
decompressionCompleted = BMK_isCompleted_TimedFn(timeStateDecompress);
}
} /* while (!(compressionCompleted && decompressionCompleted)) */

View File

@ -265,16 +265,16 @@ void BMK_resetTimedFnState(BMK_timedFnState_t* timedFnState, unsigned nbSeconds)
void BMK_freeTimedFnState(BMK_timedFnState_t* state);
/* BMK_benchFunctionTimed() :
/* BMK_benchTimedFn() :
* Similar to BMK_benchFunction(),
* tries to find automatically `nbLoops`, so that each run lasts approximately 1 second.
* Note : minimum `nbLoops` is 1, a run may last more than 1 second if benchFn is slow.
* Most arguments are the same as BMK_benchFunction()
* Usage - initialize a timedFnState, selecting a total nbSeconds allocated for _all_ benchmarks run
* call BMK_benchFunctionTimed() repetitively, collecting intermediate results (each run is supposed to last about 1 seconds)
* Check if time budget is spent using BMK_isCompleted_runOutcome()
* call BMK_benchTimedFn() repetitively, collecting intermediate results (each run is supposed to last about 1 seconds)
* Check if time budget is spent using BMK_isCompleted_TimedFn()
*/
BMK_runOutcome_t BMK_benchFunctionTimed(
BMK_runOutcome_t BMK_benchTimedFn(
BMK_timedFnState_t* timedFnState,
BMK_benchFn_t benchFn, void* benchPayload,
BMK_initFn_t initFn, void* initPayload,
@ -286,7 +286,7 @@ BMK_runOutcome_t BMK_benchFunctionTimed(
/* Tells if total nb of benchmark runs has exceeded amount of time set in timedFnState
*/
int BMK_isCompleted_runOutcome(BMK_runOutcome_t outcome);
int BMK_isCompleted_TimedFn(const BMK_timedFnState_t* timedFnState);

View File

@ -521,7 +521,7 @@ static size_t benchMem(U32 benchNb,
for (;;) {
void* const dstBuffv = dstBuff;
BMK_runOutcome_t const bOutcome =
BMK_benchFunctionTimed( tfs,
BMK_benchTimedFn( tfs,
benchFunction, buff2,
NULL, NULL, /* initFn */
1, /* blockCount */
@ -544,7 +544,7 @@ static size_t benchMem(U32 benchNb,
(unsigned)newResult.sumOfReturn );
}
if ( BMK_isCompleted_runOutcome(bOutcome) ) break;
if ( BMK_isCompleted_TimedFn(tfs) ) break;
}
BMK_freeTimedFnState(tfs);
}

View File

@ -1441,7 +1441,7 @@ BMK_benchMemInvertible( buffers_t buf, contexts_t ctx,
dctxprep.dictBufferSize = dictBufferSize;
while(!compressionCompleted) {
BMK_runOutcome_t const cOutcome = BMK_benchFunctionTimed(timeStateCompress,
BMK_runOutcome_t const cOutcome = BMK_benchTimedFn(timeStateCompress,
&local_defaultCompress, cctx,
&local_initCCtx, &cctxprep,
nbBlocks,
@ -1461,11 +1461,11 @@ BMK_benchMemInvertible( buffers_t buf, contexts_t ctx,
bResult.cSpeed = (srcSize * TIMELOOP_NANOSEC) / rResult.nanoSecPerRun;
bResult.cSize = rResult.sumOfReturn;
}
compressionCompleted = BMK_isCompleted_runOutcome(cOutcome);
compressionCompleted = BMK_isCompleted_TimedFn(timeStateCompress);
}
while (!decompressionCompleted) {
BMK_runOutcome_t const dOutcome = BMK_benchFunctionTimed(timeStateDecompress,
BMK_runOutcome_t const dOutcome = BMK_benchTimedFn(timeStateDecompress,
&local_defaultDecompress, dctx,
&local_initDCtx, &dctxprep,
nbBlocks,
@ -1484,7 +1484,7 @@ BMK_benchMemInvertible( buffers_t buf, contexts_t ctx,
{ BMK_runTime_t const rResult = BMK_extract_runTime(dOutcome);
bResult.dSpeed = (srcSize * TIMELOOP_NANOSEC) / rResult.nanoSecPerRun;
}
decompressionCompleted = BMK_isCompleted_runOutcome(dOutcome);
decompressionCompleted = BMK_isCompleted_TimedFn(timeStateDecompress);
}
BMK_freeTimedFnState(timeStateCompress);