Skip to content

Commit fa57d25

Browse files
authored
Merge pull request #2696 from scottchiefbaker/human_size_output
Make the CLI summary data use a human readable format instead of raw bytes
2 parents 8a3bdfa + 23b6c67 commit fa57d25

File tree

4 files changed

+63
-9
lines changed

4 files changed

+63
-9
lines changed

programs/benchzstd.c

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,8 @@ BMK_benchMemAdvancedNoAlloc(
388388
# define NB_MARKS 4
389389
const char* marks[NB_MARKS] = { " |", " /", " =", " \\" };
390390
U32 markNb = 0;
391+
char inputSizeStr[8] = "";
392+
char outputSizeStr[8] = "";
391393
int compressionCompleted = (adv->mode == BMK_decodeOnly);
392394
int decompressionCompleted = (adv->mode == BMK_compressOnly);
393395
BMK_benchParams_t cbp, dbp;
@@ -429,8 +431,10 @@ BMK_benchMemAdvancedNoAlloc(
429431
dctxprep.dictBuffer = dictBuffer;
430432
dctxprep.dictBufferSize = dictBufferSize;
431433

434+
humanSize((unsigned)srcSize, inputSizeStr);
435+
432436
DISPLAYLEVEL(2, "\r%70s\r", ""); /* blank line */
433-
DISPLAYLEVEL(2, "%2s-%-17.17s :%10u ->\r", marks[markNb], displayName, (unsigned)srcSize);
437+
DISPLAYLEVEL(2, "%2s-%-17.17s : %s -> \r", marks[markNb], displayName, inputSizeStr);
434438

435439
while (!(compressionCompleted && decompressionCompleted)) {
436440
if (!compressionCompleted) {
@@ -451,9 +455,13 @@ BMK_benchMemAdvancedNoAlloc(
451455
} }
452456

453457
{ int const ratioAccuracy = (ratio < 10.) ? 3 : 2;
454-
DISPLAYLEVEL(2, "%2s-%-17.17s :%10u ->%10u (%5.*f),%6.*f MB/s\r",
458+
459+
humanSize((unsigned)srcSize, inputSizeStr);
460+
humanSize((unsigned)cSize, outputSizeStr);
461+
462+
DISPLAYLEVEL(2, "%2s-%-17.17s : %s -> %s (%5.*f),%6.*f MB/s\r",
455463
marks[markNb], displayName,
456-
(unsigned)srcSize, (unsigned)cSize,
464+
inputSizeStr, outputSizeStr,
457465
ratioAccuracy, ratio,
458466
benchResult.cSpeed < (10 MB) ? 2 : 1, (double)benchResult.cSpeed / MB_UNIT);
459467
}
@@ -474,9 +482,13 @@ BMK_benchMemAdvancedNoAlloc(
474482
}
475483

476484
{ int const ratioAccuracy = (ratio < 10.) ? 3 : 2;
477-
DISPLAYLEVEL(2, "%2s-%-17.17s :%10u ->%10u (%5.*f),%6.*f MB/s ,%6.1f MB/s \r",
485+
486+
humanSize((unsigned)srcSize, inputSizeStr);
487+
humanSize((unsigned)cSize, outputSizeStr);
488+
489+
DISPLAYLEVEL(2, "%2s-%-17.17s : %s -> %s (%5.*f),%6.*f MB/s ,%6.1f MB/s \r",
478490
marks[markNb], displayName,
479-
(unsigned)srcSize, (unsigned)cSize,
491+
inputSizeStr, outputSizeStr,
480492
ratioAccuracy, ratio,
481493
benchResult.cSpeed < (10 MB) ? 2 : 1, (double)benchResult.cSpeed / MB_UNIT,
482494
(double)benchResult.dSpeed / MB_UNIT);

programs/fileio.c

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1544,6 +1544,9 @@ FIO_compressFilename_internal(FIO_ctx_t* const fCtx,
15441544
U64 readsize = 0;
15451545
U64 compressedfilesize = 0;
15461546
U64 const fileSize = UTIL_getFileSize(srcFileName);
1547+
char inputSizeStr[8] = "";
1548+
char outputSizeStr[8] = "";
1549+
15471550
DISPLAYLEVEL(5, "%s: %llu bytes \n", srcFileName, (unsigned long long)fileSize);
15481551

15491552
/* compression format selection */
@@ -1598,10 +1601,13 @@ FIO_compressFilename_internal(FIO_ctx_t* const fCtx,
15981601
(unsigned long long)readsize, (unsigned long long) compressedfilesize,
15991602
dstFileName);
16001603
} else {
1601-
DISPLAYLEVEL(2,"%-20s :%6.2f%% (%6llu => %6llu bytes, %s) \n",
1604+
humanSize((unsigned long long) readsize, inputSizeStr);
1605+
humanSize((unsigned long long) compressedfilesize, outputSizeStr);
1606+
1607+
DISPLAYLEVEL(2,"%-20s :%6.2f%% (%s => %s, %s) \n",
16021608
srcFileName,
16031609
(double)compressedfilesize / (double)readsize * 100,
1604-
(unsigned long long)readsize, (unsigned long long) compressedfilesize,
1610+
inputSizeStr, outputSizeStr,
16051611
dstFileName);
16061612
}
16071613
}
@@ -1835,6 +1841,8 @@ int FIO_compressMultipleFilenames(FIO_ctx_t* const fCtx,
18351841
{
18361842
int status;
18371843
int error = 0;
1844+
char inputSizeStr[8] = "";
1845+
char outputSizeStr[8] = "";
18381846
cRess_t ress = FIO_createCResources(prefs, dictFileName,
18391847
FIO_getLargestFileSize(inFileNamesTable, (unsigned)fCtx->nbFilesTotal),
18401848
compressionLevel, comprParams);
@@ -1890,10 +1898,13 @@ int FIO_compressMultipleFilenames(FIO_ctx_t* const fCtx,
18901898
}
18911899

18921900
if (fCtx->nbFilesProcessed >= 1 && fCtx->nbFilesTotal > 1 && fCtx->totalBytesInput != 0) {
1901+
humanSize((unsigned long long) fCtx->totalBytesInput, inputSizeStr);
1902+
humanSize((unsigned long long) fCtx->totalBytesOutput, outputSizeStr);
1903+
18931904
DISPLAYLEVEL(2, "\r%79s\r", "");
1894-
DISPLAYLEVEL(2, "%d files compressed : %.2f%% (%6zu => %6zu bytes)\n", fCtx->nbFilesProcessed,
1905+
DISPLAYLEVEL(2, "%3d files compressed : %.2f%% (%s => %s bytes)\n", fCtx->nbFilesProcessed,
18951906
(double)fCtx->totalBytesOutput/((double)fCtx->totalBytesInput)*100,
1896-
fCtx->totalBytesInput, fCtx->totalBytesOutput);
1907+
inputSizeStr, outputSizeStr);
18971908
}
18981909

18991910
FIO_freeCResources(&ress);

programs/util.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,31 @@ int UTIL_requireUserConfirmation(const char* prompt, const char* abortMsg,
121121
* Functions
122122
***************************************/
123123

124+
/*
125+
* Take a size in bytes and output a human readable string. Maximum
126+
* buffer size is 8 but it's usually 7. Example: "123.4G"
127+
*/
128+
char* humanSize(unsigned long long size, char* str) {
129+
if (size > 1152921504606846976L) {
130+
snprintf(str, 7, "%.1fE", (float)size / 1152921504606846976L);
131+
} else if (size > 1125899906842624L) {
132+
snprintf(str, 7, "%.1fP", (float)size / 1125899906842624L);
133+
} else if (size > 1099511627776L) {
134+
snprintf(str, 7, "%.1fT", (float)size / 1099511627776L);
135+
} else if (size > 1073741824L) {
136+
snprintf(str, 7, "%.1fG", (float)size / 1073741824L);
137+
} else if (size > 1048576L) {
138+
snprintf(str, 7, "%.1fM", (float)size / 1048576L);
139+
} else if (size > 1024) {
140+
snprintf(str, 7, "%.1fK", (float)size / 1024);
141+
} else if (size <= 1024) {
142+
snprintf(str, 7, "%uB", (unsigned)size);
143+
}
144+
145+
return str;
146+
}
147+
148+
124149
int UTIL_stat(const char* filename, stat_t* statbuf)
125150
{
126151
#if defined(_MSC_VER)

programs/util.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,12 @@ int UTIL_requireUserConfirmation(const char* prompt, const char* abortMsg, const
122122
#define STRDUP(s) strdup(s)
123123
#endif
124124

125+
/*
126+
* Take a size in bytes and output a human readable string. Maximum
127+
* buffer size is 8 but it's usually 7. Example: "123.4G"
128+
*/
129+
char* humanSize(unsigned long long size, char* str);
130+
125131
/**
126132
* Calls platform's equivalent of stat() on filename and writes info to statbuf.
127133
* Returns success (1) or failure (0).

0 commit comments

Comments
 (0)