Skip to content

Commit e769da1

Browse files
authored
Merge pull request #3526 from facebook/bench_zstd_api
Simplify benchmark unit invocation API from CLI
2 parents 6bedef8 + 1e38e07 commit e769da1

File tree

4 files changed

+58
-65
lines changed

4 files changed

+58
-65
lines changed

programs/benchzstd.c

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -697,22 +697,26 @@ static BMK_benchOutcome_t BMK_benchCLevel(const void* srcBuffer, size_t benchedS
697697
displayLevel, displayName, adv);
698698
}
699699

700-
BMK_benchOutcome_t BMK_syntheticTest(int cLevel, double compressibility,
701-
const ZSTD_compressionParameters* compressionParams,
702-
int displayLevel, const BMK_advancedParams_t* adv)
700+
int BMK_syntheticTest(int cLevel, double compressibility,
701+
const ZSTD_compressionParameters* compressionParams,
702+
int displayLevel, const BMK_advancedParams_t* adv)
703703
{
704704
char name[20] = {0};
705705
size_t const benchedSize = 10000000;
706706
void* srcBuffer;
707707
BMK_benchOutcome_t res;
708708

709709
if (cLevel > ZSTD_maxCLevel()) {
710-
RETURN_ERROR(15, BMK_benchOutcome_t, "Invalid Compression Level");
710+
DISPLAYLEVEL(1, "Invalid Compression Level");
711+
return 15;
711712
}
712713

713714
/* Memory allocation */
714715
srcBuffer = malloc(benchedSize);
715-
if (!srcBuffer) RETURN_ERROR(21, BMK_benchOutcome_t, "not enough memory");
716+
if (!srcBuffer) {
717+
DISPLAYLEVEL(1, "allocation error : not enough memory");
718+
return 16;
719+
}
716720

717721
/* Fill input buffer */
718722
RDG_genBuffer(srcBuffer, benchedSize, compressibility, 0.0, 0);
@@ -728,7 +732,7 @@ BMK_benchOutcome_t BMK_syntheticTest(int cLevel, double compressibility,
728732
/* clean up */
729733
free(srcBuffer);
730734

731-
return res;
735+
return !BMK_isSuccessful_benchOutcome(res);
732736
}
733737

734738

@@ -790,7 +794,7 @@ static int BMK_loadFiles(void* buffer, size_t bufferSize,
790794
return 0;
791795
}
792796

793-
BMK_benchOutcome_t BMK_benchFilesAdvanced(
797+
int BMK_benchFilesAdvanced(
794798
const char* const * fileNamesTable, unsigned nbFiles,
795799
const char* dictFileName, int cLevel,
796800
const ZSTD_compressionParameters* compressionParams,
@@ -805,38 +809,47 @@ BMK_benchOutcome_t BMK_benchFilesAdvanced(
805809
U64 const totalSizeToLoad = UTIL_getTotalFileSize(fileNamesTable, nbFiles);
806810

807811
if (!nbFiles) {
808-
RETURN_ERROR(14, BMK_benchOutcome_t, "No Files to Benchmark");
812+
DISPLAYLEVEL(1, "No Files to Benchmark");
813+
return 13;
809814
}
810815

811816
if (cLevel > ZSTD_maxCLevel()) {
812-
RETURN_ERROR(15, BMK_benchOutcome_t, "Invalid Compression Level");
817+
DISPLAYLEVEL(1, "Invalid Compression Level");
818+
return 14;
813819
}
814820

815821
if (totalSizeToLoad == UTIL_FILESIZE_UNKNOWN) {
816-
RETURN_ERROR(9, BMK_benchOutcome_t, "Error loading files");
822+
DISPLAYLEVEL(1, "Error loading files");
823+
return 15;
817824
}
818825

819826
fileSizes = (size_t*)calloc(nbFiles, sizeof(size_t));
820-
if (!fileSizes) RETURN_ERROR(12, BMK_benchOutcome_t, "not enough memory for fileSizes");
827+
if (!fileSizes) {
828+
DISPLAYLEVEL(1, "not enough memory for fileSizes");
829+
return 16;
830+
}
821831

822832
/* Load dictionary */
823833
if (dictFileName != NULL) {
824834
U64 const dictFileSize = UTIL_getFileSize(dictFileName);
825835
if (dictFileSize == UTIL_FILESIZE_UNKNOWN) {
826836
DISPLAYLEVEL(1, "error loading %s : %s \n", dictFileName, strerror(errno));
827837
free(fileSizes);
828-
RETURN_ERROR(9, BMK_benchOutcome_t, "benchmark aborted");
838+
DISPLAYLEVEL(1, "benchmark aborted");
839+
return 17;
829840
}
830841
if (dictFileSize > 64 MB) {
831842
free(fileSizes);
832-
RETURN_ERROR(10, BMK_benchOutcome_t, "dictionary file %s too large", dictFileName);
843+
DISPLAYLEVEL(1, "dictionary file %s too large", dictFileName);
844+
return 18;
833845
}
834846
dictBufferSize = (size_t)dictFileSize;
835847
dictBuffer = malloc(dictBufferSize);
836848
if (dictBuffer==NULL) {
837849
free(fileSizes);
838-
RETURN_ERROR(11, BMK_benchOutcome_t, "not enough memory for dictionary (%u bytes)",
850+
DISPLAYLEVEL(1, "not enough memory for dictionary (%u bytes)",
839851
(unsigned)dictBufferSize);
852+
return 19;
840853
}
841854

842855
{ int const errorCode = BMK_loadFiles(dictBuffer, dictBufferSize,
@@ -858,7 +871,8 @@ BMK_benchOutcome_t BMK_benchFilesAdvanced(
858871
if (!srcBuffer) {
859872
free(dictBuffer);
860873
free(fileSizes);
861-
RETURN_ERROR(12, BMK_benchOutcome_t, "not enough memory");
874+
DISPLAYLEVEL(1, "not enough memory for srcBuffer");
875+
return 20;
862876
}
863877

864878
/* Load input buffer */
@@ -886,12 +900,11 @@ BMK_benchOutcome_t BMK_benchFilesAdvanced(
886900
free(srcBuffer);
887901
free(dictBuffer);
888902
free(fileSizes);
889-
return res;
903+
return !BMK_isSuccessful_benchOutcome(res);
890904
}
891905

892906

893-
BMK_benchOutcome_t BMK_benchFiles(
894-
const char* const * fileNamesTable, unsigned nbFiles,
907+
int BMK_benchFiles(const char* const * fileNamesTable, unsigned nbFiles,
895908
const char* dictFileName,
896909
int cLevel, const ZSTD_compressionParameters* compressionParams,
897910
int displayLevel)

programs/benchzstd.h

Lines changed: 17 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -81,21 +81,13 @@ BMK_benchResult_t BMK_extract_benchResult(BMK_benchOutcome_t outcome);
8181
* 2 : + result + interaction + warnings;
8282
* 3 : + information;
8383
* 4 : + debug
84-
* @return:
85-
* a variant, which expresses either an error, or a valid result.
86-
* Use BMK_isSuccessful_benchOutcome() to check if function was successful.
87-
* If yes, extract the valid result with BMK_extract_benchResult(),
88-
* it will contain :
89-
* .cSpeed: compression speed in bytes per second,
90-
* .dSpeed: decompression speed in bytes per second,
91-
* .cSize : compressed size, in bytes
92-
* .cMem : memory budget required for the compression context
84+
* @return: 0 on success, !0 on error
9385
*/
94-
BMK_benchOutcome_t BMK_benchFiles(
95-
const char* const * fileNamesTable, unsigned nbFiles,
96-
const char* dictFileName,
97-
int cLevel, const ZSTD_compressionParameters* compressionParams,
98-
int displayLevel);
86+
int BMK_benchFiles(
87+
const char* const * fileNamesTable, unsigned nbFiles,
88+
const char* dictFileName,
89+
int cLevel, const ZSTD_compressionParameters* compressionParams,
90+
int displayLevel);
9991

10092

10193
typedef enum {
@@ -126,11 +118,11 @@ BMK_advancedParams_t BMK_initAdvancedParams(void);
126118
/*! BMK_benchFilesAdvanced():
127119
* Same as BMK_benchFiles(),
128120
* with more controls, provided through advancedParams_t structure */
129-
BMK_benchOutcome_t BMK_benchFilesAdvanced(
130-
const char* const * fileNamesTable, unsigned nbFiles,
131-
const char* dictFileName,
132-
int cLevel, const ZSTD_compressionParameters* compressionParams,
133-
int displayLevel, const BMK_advancedParams_t* adv);
121+
int BMK_benchFilesAdvanced(
122+
const char* const * fileNamesTable, unsigned nbFiles,
123+
const char* dictFileName,
124+
int cLevel, const ZSTD_compressionParameters* compressionParams,
125+
int displayLevel, const BMK_advancedParams_t* adv);
134126

135127
/*! BMK_syntheticTest() -- called from zstdcli */
136128
/* Generates a sample with datagen, using compressibility argument */
@@ -139,20 +131,11 @@ BMK_benchOutcome_t BMK_benchFilesAdvanced(
139131
* compressionParams - basic compression Parameters
140132
* displayLevel - see benchFiles
141133
* adv - see advanced_Params_t
142-
* @return:
143-
* a variant, which expresses either an error, or a valid result.
144-
* Use BMK_isSuccessful_benchOutcome() to check if function was successful.
145-
* If yes, extract the valid result with BMK_extract_benchResult(),
146-
* it will contain :
147-
* .cSpeed: compression speed in bytes per second,
148-
* .dSpeed: decompression speed in bytes per second,
149-
* .cSize : compressed size, in bytes
150-
* .cMem : memory budget required for the compression context
134+
* @return: 0 on success, !0 on error
151135
*/
152-
BMK_benchOutcome_t BMK_syntheticTest(
153-
int cLevel, double compressibility,
154-
const ZSTD_compressionParameters* compressionParams,
155-
int displayLevel, const BMK_advancedParams_t* adv);
136+
int BMK_syntheticTest(int cLevel, double compressibility,
137+
const ZSTD_compressionParameters* compressionParams,
138+
int displayLevel, const BMK_advancedParams_t* adv);
156139

157140

158141

@@ -190,8 +173,8 @@ BMK_benchOutcome_t BMK_benchMem(const void* srcBuffer, size_t srcSize,
190173
int displayLevel, const char* displayName);
191174

192175

193-
/* BMK_benchMemAdvanced() : same as BMK_benchMem()
194-
* with following additional options :
176+
/* BMK_benchMemAdvanced() : used by Paramgrill
177+
* same as BMK_benchMem() with following additional options :
195178
* dstBuffer - destination buffer to write compressed output in, NULL if none provided.
196179
* dstCapacity - capacity of destination buffer, give 0 if dstBuffer = NULL
197180
* adv = see advancedParams_t

programs/zstdcli.c

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737

3838
#include "fileio.h" /* stdinmark, stdoutmark, ZSTD_EXTENSION */
3939
#ifndef ZSTD_NOBENCH
40-
# include "benchzstd.h" /* BMK_benchFiles */
40+
# include "benchzstd.h" /* BMK_benchFilesAdvanced */
4141
#endif
4242
#ifndef ZSTD_NODICT
4343
# include "dibio.h" /* ZDICT_cover_params_t, DiB_trainFromFiles() */
@@ -1395,18 +1395,15 @@ int main(int argCount, const char* argv[])
13951395
int c;
13961396
DISPLAYLEVEL(3, "Benchmarking %s \n", filenames->fileNames[i]);
13971397
for(c = cLevel; c <= cLevelLast; c++) {
1398-
BMK_benchOutcome_t const bo = BMK_benchFilesAdvanced(&filenames->fileNames[i], 1, dictFileName, c, &compressionParams, g_displayLevel, &benchParams);
1399-
if (!BMK_isSuccessful_benchOutcome(bo)) return 1;
1398+
operationResult = BMK_benchFilesAdvanced(&filenames->fileNames[i], 1, dictFileName, c, &compressionParams, g_displayLevel, &benchParams);
14001399
} }
14011400
} else {
14021401
for(; cLevel <= cLevelLast; cLevel++) {
1403-
BMK_benchOutcome_t const bo = BMK_benchFilesAdvanced(filenames->fileNames, (unsigned)filenames->tableSize, dictFileName, cLevel, &compressionParams, g_displayLevel, &benchParams);
1404-
if (!BMK_isSuccessful_benchOutcome(bo)) return 1;
1402+
operationResult = BMK_benchFilesAdvanced(filenames->fileNames, (unsigned)filenames->tableSize, dictFileName, cLevel, &compressionParams, g_displayLevel, &benchParams);
14051403
} }
14061404
} else {
14071405
for(; cLevel <= cLevelLast; cLevel++) {
1408-
BMK_benchOutcome_t const bo = BMK_syntheticTest(cLevel, compressibility, &compressionParams, g_displayLevel, &benchParams);
1409-
if (!BMK_isSuccessful_benchOutcome(bo)) return 1;
1406+
operationResult = BMK_syntheticTest(cLevel, compressibility, &compressionParams, g_displayLevel, &benchParams);
14101407
} }
14111408

14121409
#else

zlibWrapper/examples/zwrapbench.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
#include "datagen.h" /* RDG_genBuffer */
2727
#include "xxhash.h"
2828

29-
#include "zstd_zlibwrapper.h"
29+
#include "../zstd_zlibwrapper.h"
3030

3131

3232

@@ -109,17 +109,17 @@ static unsigned g_nbIterations = NBLOOPS;
109109
static size_t g_blockSize = 0;
110110
int g_additionalParam = 0;
111111

112-
void BMK_setNotificationLevel(unsigned level) { g_displayLevel=level; }
112+
static void BMK_setNotificationLevel(unsigned level) { g_displayLevel=level; }
113113

114-
void BMK_setAdditionalParam(int additionalParam) { g_additionalParam=additionalParam; }
114+
static void BMK_setAdditionalParam(int additionalParam) { g_additionalParam=additionalParam; }
115115

116-
void BMK_SetNbIterations(unsigned nbLoops)
116+
static void BMK_SetNbIterations(unsigned nbLoops)
117117
{
118118
g_nbIterations = nbLoops;
119119
DISPLAYLEVEL(3, "- test >= %u seconds per compression / decompression -\n", g_nbIterations);
120120
}
121121

122-
void BMK_SetBlockSize(size_t blockSize)
122+
static void BMK_SetBlockSize(size_t blockSize)
123123
{
124124
g_blockSize = blockSize;
125125
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
798798
}
799799

800800

801-
int BMK_benchFiles(const char** fileNamesTable, unsigned nbFiles,
801+
static int BMK_benchFiles(const char** fileNamesTable, unsigned nbFiles,
802802
const char* dictFileName, int cLevel, int cLevelLast)
803803
{
804804
double const compressibility = (double)g_compressibilityDefault / 100;

0 commit comments

Comments
 (0)