1
0
mirror of https://github.com/facebook/zstd.git synced 2025-07-29 11:21:22 +03:00

Merge pull request #3526 from facebook/bench_zstd_api

Simplify benchmark unit invocation API from CLI
This commit is contained in:
Yann Collet
2023-03-09 13:11:11 -08:00
committed by GitHub
4 changed files with 58 additions and 65 deletions

View File

@ -697,9 +697,9 @@ static BMK_benchOutcome_t BMK_benchCLevel(const void* srcBuffer, size_t benchedS
displayLevel, displayName, adv); displayLevel, displayName, adv);
} }
BMK_benchOutcome_t BMK_syntheticTest(int cLevel, double compressibility, int BMK_syntheticTest(int cLevel, double compressibility,
const ZSTD_compressionParameters* compressionParams, const ZSTD_compressionParameters* compressionParams,
int displayLevel, const BMK_advancedParams_t* adv) int displayLevel, const BMK_advancedParams_t* adv)
{ {
char name[20] = {0}; char name[20] = {0};
size_t const benchedSize = 10000000; size_t const benchedSize = 10000000;
@ -707,12 +707,16 @@ BMK_benchOutcome_t BMK_syntheticTest(int cLevel, double compressibility,
BMK_benchOutcome_t res; BMK_benchOutcome_t res;
if (cLevel > ZSTD_maxCLevel()) { if (cLevel > ZSTD_maxCLevel()) {
RETURN_ERROR(15, BMK_benchOutcome_t, "Invalid Compression Level"); DISPLAYLEVEL(1, "Invalid Compression Level");
return 15;
} }
/* Memory allocation */ /* Memory allocation */
srcBuffer = malloc(benchedSize); srcBuffer = malloc(benchedSize);
if (!srcBuffer) RETURN_ERROR(21, BMK_benchOutcome_t, "not enough memory"); if (!srcBuffer) {
DISPLAYLEVEL(1, "allocation error : not enough memory");
return 16;
}
/* Fill input buffer */ /* Fill input buffer */
RDG_genBuffer(srcBuffer, benchedSize, compressibility, 0.0, 0); RDG_genBuffer(srcBuffer, benchedSize, compressibility, 0.0, 0);
@ -728,7 +732,7 @@ BMK_benchOutcome_t BMK_syntheticTest(int cLevel, double compressibility,
/* clean up */ /* clean up */
free(srcBuffer); free(srcBuffer);
return res; return !BMK_isSuccessful_benchOutcome(res);
} }
@ -790,7 +794,7 @@ static int BMK_loadFiles(void* buffer, size_t bufferSize,
return 0; return 0;
} }
BMK_benchOutcome_t BMK_benchFilesAdvanced( int BMK_benchFilesAdvanced(
const char* const * fileNamesTable, unsigned nbFiles, const char* const * fileNamesTable, unsigned nbFiles,
const char* dictFileName, int cLevel, const char* dictFileName, int cLevel,
const ZSTD_compressionParameters* compressionParams, const ZSTD_compressionParameters* compressionParams,
@ -805,19 +809,25 @@ BMK_benchOutcome_t BMK_benchFilesAdvanced(
U64 const totalSizeToLoad = UTIL_getTotalFileSize(fileNamesTable, nbFiles); U64 const totalSizeToLoad = UTIL_getTotalFileSize(fileNamesTable, nbFiles);
if (!nbFiles) { if (!nbFiles) {
RETURN_ERROR(14, BMK_benchOutcome_t, "No Files to Benchmark"); DISPLAYLEVEL(1, "No Files to Benchmark");
return 13;
} }
if (cLevel > ZSTD_maxCLevel()) { if (cLevel > ZSTD_maxCLevel()) {
RETURN_ERROR(15, BMK_benchOutcome_t, "Invalid Compression Level"); DISPLAYLEVEL(1, "Invalid Compression Level");
return 14;
} }
if (totalSizeToLoad == UTIL_FILESIZE_UNKNOWN) { if (totalSizeToLoad == UTIL_FILESIZE_UNKNOWN) {
RETURN_ERROR(9, BMK_benchOutcome_t, "Error loading files"); DISPLAYLEVEL(1, "Error loading files");
return 15;
} }
fileSizes = (size_t*)calloc(nbFiles, sizeof(size_t)); fileSizes = (size_t*)calloc(nbFiles, sizeof(size_t));
if (!fileSizes) RETURN_ERROR(12, BMK_benchOutcome_t, "not enough memory for fileSizes"); if (!fileSizes) {
DISPLAYLEVEL(1, "not enough memory for fileSizes");
return 16;
}
/* Load dictionary */ /* Load dictionary */
if (dictFileName != NULL) { if (dictFileName != NULL) {
@ -825,18 +835,21 @@ BMK_benchOutcome_t BMK_benchFilesAdvanced(
if (dictFileSize == UTIL_FILESIZE_UNKNOWN) { if (dictFileSize == UTIL_FILESIZE_UNKNOWN) {
DISPLAYLEVEL(1, "error loading %s : %s \n", dictFileName, strerror(errno)); DISPLAYLEVEL(1, "error loading %s : %s \n", dictFileName, strerror(errno));
free(fileSizes); free(fileSizes);
RETURN_ERROR(9, BMK_benchOutcome_t, "benchmark aborted"); DISPLAYLEVEL(1, "benchmark aborted");
return 17;
} }
if (dictFileSize > 64 MB) { if (dictFileSize > 64 MB) {
free(fileSizes); free(fileSizes);
RETURN_ERROR(10, BMK_benchOutcome_t, "dictionary file %s too large", dictFileName); DISPLAYLEVEL(1, "dictionary file %s too large", dictFileName);
return 18;
} }
dictBufferSize = (size_t)dictFileSize; dictBufferSize = (size_t)dictFileSize;
dictBuffer = malloc(dictBufferSize); dictBuffer = malloc(dictBufferSize);
if (dictBuffer==NULL) { if (dictBuffer==NULL) {
free(fileSizes); free(fileSizes);
RETURN_ERROR(11, BMK_benchOutcome_t, "not enough memory for dictionary (%u bytes)", DISPLAYLEVEL(1, "not enough memory for dictionary (%u bytes)",
(unsigned)dictBufferSize); (unsigned)dictBufferSize);
return 19;
} }
{ int const errorCode = BMK_loadFiles(dictBuffer, dictBufferSize, { int const errorCode = BMK_loadFiles(dictBuffer, dictBufferSize,
@ -858,7 +871,8 @@ BMK_benchOutcome_t BMK_benchFilesAdvanced(
if (!srcBuffer) { if (!srcBuffer) {
free(dictBuffer); free(dictBuffer);
free(fileSizes); free(fileSizes);
RETURN_ERROR(12, BMK_benchOutcome_t, "not enough memory"); DISPLAYLEVEL(1, "not enough memory for srcBuffer");
return 20;
} }
/* Load input buffer */ /* Load input buffer */
@ -886,12 +900,11 @@ _cleanUp:
free(srcBuffer); free(srcBuffer);
free(dictBuffer); free(dictBuffer);
free(fileSizes); free(fileSizes);
return res; return !BMK_isSuccessful_benchOutcome(res);
} }
BMK_benchOutcome_t BMK_benchFiles( int BMK_benchFiles(const char* const * fileNamesTable, unsigned nbFiles,
const char* const * fileNamesTable, unsigned nbFiles,
const char* dictFileName, const char* dictFileName,
int cLevel, const ZSTD_compressionParameters* compressionParams, int cLevel, const ZSTD_compressionParameters* compressionParams,
int displayLevel) int displayLevel)

View File

@ -81,21 +81,13 @@ BMK_benchResult_t BMK_extract_benchResult(BMK_benchOutcome_t outcome);
* 2 : + result + interaction + warnings; * 2 : + result + interaction + warnings;
* 3 : + information; * 3 : + information;
* 4 : + debug * 4 : + debug
* @return: * @return: 0 on success, !0 on error
* a variant, which expresses either an error, or a valid result.
* Use BMK_isSuccessful_benchOutcome() to check if function was successful.
* If yes, extract the valid result with BMK_extract_benchResult(),
* it will contain :
* .cSpeed: compression speed in bytes per second,
* .dSpeed: decompression speed in bytes per second,
* .cSize : compressed size, in bytes
* .cMem : memory budget required for the compression context
*/ */
BMK_benchOutcome_t BMK_benchFiles( int BMK_benchFiles(
const char* const * fileNamesTable, unsigned nbFiles, const char* const * fileNamesTable, unsigned nbFiles,
const char* dictFileName, const char* dictFileName,
int cLevel, const ZSTD_compressionParameters* compressionParams, int cLevel, const ZSTD_compressionParameters* compressionParams,
int displayLevel); int displayLevel);
typedef enum { typedef enum {
@ -126,11 +118,11 @@ BMK_advancedParams_t BMK_initAdvancedParams(void);
/*! BMK_benchFilesAdvanced(): /*! BMK_benchFilesAdvanced():
* Same as BMK_benchFiles(), * Same as BMK_benchFiles(),
* with more controls, provided through advancedParams_t structure */ * with more controls, provided through advancedParams_t structure */
BMK_benchOutcome_t BMK_benchFilesAdvanced( int BMK_benchFilesAdvanced(
const char* const * fileNamesTable, unsigned nbFiles, const char* const * fileNamesTable, unsigned nbFiles,
const char* dictFileName, const char* dictFileName,
int cLevel, const ZSTD_compressionParameters* compressionParams, int cLevel, const ZSTD_compressionParameters* compressionParams,
int displayLevel, const BMK_advancedParams_t* adv); int displayLevel, const BMK_advancedParams_t* adv);
/*! BMK_syntheticTest() -- called from zstdcli */ /*! BMK_syntheticTest() -- called from zstdcli */
/* Generates a sample with datagen, using compressibility argument */ /* Generates a sample with datagen, using compressibility argument */
@ -139,20 +131,11 @@ BMK_benchOutcome_t BMK_benchFilesAdvanced(
* compressionParams - basic compression Parameters * compressionParams - basic compression Parameters
* displayLevel - see benchFiles * displayLevel - see benchFiles
* adv - see advanced_Params_t * adv - see advanced_Params_t
* @return: * @return: 0 on success, !0 on error
* a variant, which expresses either an error, or a valid result.
* Use BMK_isSuccessful_benchOutcome() to check if function was successful.
* If yes, extract the valid result with BMK_extract_benchResult(),
* it will contain :
* .cSpeed: compression speed in bytes per second,
* .dSpeed: decompression speed in bytes per second,
* .cSize : compressed size, in bytes
* .cMem : memory budget required for the compression context
*/ */
BMK_benchOutcome_t BMK_syntheticTest( int BMK_syntheticTest(int cLevel, double compressibility,
int cLevel, double compressibility, const ZSTD_compressionParameters* compressionParams,
const ZSTD_compressionParameters* compressionParams, int displayLevel, const BMK_advancedParams_t* adv);
int displayLevel, const BMK_advancedParams_t* adv);
@ -190,8 +173,8 @@ BMK_benchOutcome_t BMK_benchMem(const void* srcBuffer, size_t srcSize,
int displayLevel, const char* displayName); int displayLevel, const char* displayName);
/* BMK_benchMemAdvanced() : same as BMK_benchMem() /* BMK_benchMemAdvanced() : used by Paramgrill
* with following additional options : * same as BMK_benchMem() with following additional options :
* dstBuffer - destination buffer to write compressed output in, NULL if none provided. * dstBuffer - destination buffer to write compressed output in, NULL if none provided.
* dstCapacity - capacity of destination buffer, give 0 if dstBuffer = NULL * dstCapacity - capacity of destination buffer, give 0 if dstBuffer = NULL
* adv = see advancedParams_t * adv = see advancedParams_t

View File

@ -37,7 +37,7 @@
#include "fileio.h" /* stdinmark, stdoutmark, ZSTD_EXTENSION */ #include "fileio.h" /* stdinmark, stdoutmark, ZSTD_EXTENSION */
#ifndef ZSTD_NOBENCH #ifndef ZSTD_NOBENCH
# include "benchzstd.h" /* BMK_benchFiles */ # include "benchzstd.h" /* BMK_benchFilesAdvanced */
#endif #endif
#ifndef ZSTD_NODICT #ifndef ZSTD_NODICT
# include "dibio.h" /* ZDICT_cover_params_t, DiB_trainFromFiles() */ # include "dibio.h" /* ZDICT_cover_params_t, DiB_trainFromFiles() */
@ -1395,18 +1395,15 @@ int main(int argCount, const char* argv[])
int c; int c;
DISPLAYLEVEL(3, "Benchmarking %s \n", filenames->fileNames[i]); DISPLAYLEVEL(3, "Benchmarking %s \n", filenames->fileNames[i]);
for(c = cLevel; c <= cLevelLast; c++) { for(c = cLevel; c <= cLevelLast; c++) {
BMK_benchOutcome_t const bo = BMK_benchFilesAdvanced(&filenames->fileNames[i], 1, dictFileName, c, &compressionParams, g_displayLevel, &benchParams); operationResult = BMK_benchFilesAdvanced(&filenames->fileNames[i], 1, dictFileName, c, &compressionParams, g_displayLevel, &benchParams);
if (!BMK_isSuccessful_benchOutcome(bo)) return 1;
} } } }
} else { } else {
for(; cLevel <= cLevelLast; cLevel++) { for(; cLevel <= cLevelLast; cLevel++) {
BMK_benchOutcome_t const bo = BMK_benchFilesAdvanced(filenames->fileNames, (unsigned)filenames->tableSize, dictFileName, cLevel, &compressionParams, g_displayLevel, &benchParams); operationResult = BMK_benchFilesAdvanced(filenames->fileNames, (unsigned)filenames->tableSize, dictFileName, cLevel, &compressionParams, g_displayLevel, &benchParams);
if (!BMK_isSuccessful_benchOutcome(bo)) return 1;
} } } }
} else { } else {
for(; cLevel <= cLevelLast; cLevel++) { for(; cLevel <= cLevelLast; cLevel++) {
BMK_benchOutcome_t const bo = BMK_syntheticTest(cLevel, compressibility, &compressionParams, g_displayLevel, &benchParams); operationResult = BMK_syntheticTest(cLevel, compressibility, &compressionParams, g_displayLevel, &benchParams);
if (!BMK_isSuccessful_benchOutcome(bo)) return 1;
} } } }
#else #else

View File

@ -26,7 +26,7 @@
#include "datagen.h" /* RDG_genBuffer */ #include "datagen.h" /* RDG_genBuffer */
#include "xxhash.h" #include "xxhash.h"
#include "zstd_zlibwrapper.h" #include "../zstd_zlibwrapper.h"
@ -109,17 +109,17 @@ static unsigned g_nbIterations = NBLOOPS;
static size_t g_blockSize = 0; static size_t g_blockSize = 0;
int g_additionalParam = 0; int g_additionalParam = 0;
void BMK_setNotificationLevel(unsigned level) { g_displayLevel=level; } static void BMK_setNotificationLevel(unsigned level) { g_displayLevel=level; }
void BMK_setAdditionalParam(int additionalParam) { g_additionalParam=additionalParam; } static void BMK_setAdditionalParam(int additionalParam) { g_additionalParam=additionalParam; }
void BMK_SetNbIterations(unsigned nbLoops) static void BMK_SetNbIterations(unsigned nbLoops)
{ {
g_nbIterations = nbLoops; g_nbIterations = nbLoops;
DISPLAYLEVEL(3, "- test >= %u seconds per compression / decompression -\n", g_nbIterations); DISPLAYLEVEL(3, "- test >= %u seconds per compression / decompression -\n", g_nbIterations);
} }
void BMK_SetBlockSize(size_t blockSize) static void BMK_SetBlockSize(size_t blockSize)
{ {
g_blockSize = blockSize; g_blockSize = blockSize;
DISPLAYLEVEL(2, "using blocks of size %u KB \n", (unsigned)(blockSize>>10)); DISPLAYLEVEL(2, "using blocks of size %u KB \n", (unsigned)(blockSize>>10));
@ -798,7 +798,7 @@ static void BMK_syntheticTest(int cLevel, int cLevelLast, double compressibility
} }
int BMK_benchFiles(const char** fileNamesTable, unsigned nbFiles, static int BMK_benchFiles(const char** fileNamesTable, unsigned nbFiles,
const char* dictFileName, int cLevel, int cLevelLast) const char* dictFileName, int cLevel, int cLevelLast)
{ {
double const compressibility = (double)g_compressibilityDefault / 100; double const compressibility = (double)g_compressibilityDefault / 100;