benchzstd.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. * Copyright (c) Meta Platforms, Inc. and affiliates.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under both the BSD-style license (found in the
  6. * LICENSE file in the root directory of this source tree) and the GPLv2 (found
  7. * in the COPYING file in the root directory of this source tree).
  8. * You may select, at your option, one of the above-listed licenses.
  9. */
  10. /* benchzstd :
  11. * benchmark Zstandard compression / decompression
  12. * over a set of files or buffers
  13. * and display progress result and final summary
  14. */
  15. #ifndef BENCH_ZSTD_H_3242387
  16. #define BENCH_ZSTD_H_3242387
  17. /* === Dependencies === */
  18. #include <stddef.h> /* size_t */
  19. #define ZSTD_STATIC_LINKING_ONLY /* ZSTD_compressionParameters */
  20. #include "../lib/zstd.h" /* ZSTD_compressionParameters */
  21. /* === Constants === */
  22. #define MB_UNIT 1000000
  23. /* === Benchmark functions === */
  24. /* Creates a variant `typeName`, able to express "error or valid result".
  25. * Functions with return type `typeName`
  26. * must first check if result is valid, using BMK_isSuccessful_*(),
  27. * and only then can extract `baseType`.
  28. */
  29. #define VARIANT_ERROR_RESULT(baseType, variantName) \
  30. \
  31. typedef struct { \
  32. baseType internal_never_use_directly; \
  33. int tag; \
  34. } variantName
  35. typedef struct {
  36. size_t cSize;
  37. unsigned long long cSpeed; /* bytes / sec */
  38. unsigned long long dSpeed;
  39. size_t cMem; /* memory usage during compression */
  40. } BMK_benchResult_t;
  41. VARIANT_ERROR_RESULT(BMK_benchResult_t, BMK_benchOutcome_t);
  42. /* check first if the return structure represents an error or a valid result */
  43. int BMK_isSuccessful_benchOutcome(BMK_benchOutcome_t outcome);
  44. /* extract result from variant type.
  45. * note : this function will abort() program execution if result is not valid
  46. * check result validity first, by using BMK_isSuccessful_benchOutcome()
  47. */
  48. BMK_benchResult_t BMK_extract_benchResult(BMK_benchOutcome_t outcome);
  49. /*! BMK_benchFiles() -- called by zstdcli */
  50. /* Loads files from fileNamesTable into memory,
  51. * and an optional dictionary from dictFileName (can be NULL),
  52. * then uses benchMem().
  53. * fileNamesTable - name of files to benchmark.
  54. * nbFiles - number of files (size of fileNamesTable), must be > 0.
  55. * dictFileName - name of dictionary file to load.
  56. * cLevel - compression level to benchmark, errors if invalid.
  57. * compressionParams - advanced compression Parameters.
  58. * displayLevel - what gets printed:
  59. * 0 : no display;
  60. * 1 : errors;
  61. * 2 : + result + interaction + warnings;
  62. * 3 : + information;
  63. * 4 : + debug
  64. * @return: 0 on success, !0 on error
  65. */
  66. int BMK_benchFiles(
  67. const char* const * fileNamesTable, unsigned nbFiles,
  68. const char* dictFileName,
  69. int cLevel, const ZSTD_compressionParameters* compressionParams,
  70. int displayLevel);
  71. typedef enum {
  72. BMK_both = 0,
  73. BMK_decodeOnly = 1,
  74. BMK_compressOnly = 2
  75. } BMK_mode_t;
  76. typedef struct {
  77. BMK_mode_t mode; /* 0: all, 1: compress only 2: decode only */
  78. unsigned nbSeconds; /* default timing is in nbSeconds */
  79. size_t blockSize; /* Maximum size of each block*/
  80. size_t targetCBlockSize;/* Approximative size of compressed blocks */
  81. int nbWorkers; /* multithreading */
  82. unsigned realTime; /* real time priority */
  83. int additionalParam; /* used by python speed benchmark */
  84. int ldmFlag; /* enables long distance matching */
  85. int ldmMinMatch; /* below: parameters for long distance matching, see zstd.1.md */
  86. int ldmHashLog;
  87. int ldmBucketSizeLog;
  88. int ldmHashRateLog;
  89. ZSTD_ParamSwitch_e literalCompressionMode;
  90. int useRowMatchFinder; /* use row-based matchfinder if possible */
  91. } BMK_advancedParams_t;
  92. /* returns default parameters used by nonAdvanced functions */
  93. BMK_advancedParams_t BMK_initAdvancedParams(void);
  94. /*! BMK_benchFilesAdvanced():
  95. * Same as BMK_benchFiles(),
  96. * with more controls, provided through advancedParams_t structure */
  97. int BMK_benchFilesAdvanced(
  98. const char* const * fileNamesTable, unsigned nbFiles,
  99. const char* dictFileName,
  100. int startCLevel, int endCLevel,
  101. const ZSTD_compressionParameters* compressionParams,
  102. int displayLevel, const BMK_advancedParams_t* adv);
  103. /*! BMK_syntheticTest() -- called from zstdcli */
  104. /* Generates a sample with datagen, using @compressibility argument
  105. * @cLevel - compression level to benchmark, errors if invalid
  106. * @compressibility - determines compressibility of sample, range [0.0 - 1.0]
  107. * if @compressibility < 0.0, uses the lorem ipsum generator
  108. * @compressionParams - basic compression Parameters
  109. * @displayLevel - see benchFiles
  110. * @adv - see advanced_Params_t
  111. * @return: 0 on success, !0 on error
  112. */
  113. int BMK_syntheticTest(double compressibility,
  114. int startingCLevel, int endCLevel,
  115. const ZSTD_compressionParameters* compressionParams,
  116. int displayLevel, const BMK_advancedParams_t* adv);
  117. /* === Benchmark Zstandard in a memory-to-memory scenario === */
  118. /** BMK_benchMem() -- core benchmarking function, called in paramgrill
  119. * applies ZSTD_compress_generic() and ZSTD_decompress_generic() on data in srcBuffer
  120. * with specific compression parameters provided by other arguments using benchFunction
  121. * (cLevel, comprParams + adv in advanced Mode) */
  122. /* srcBuffer - data source, expected to be valid compressed data if in Decode Only Mode
  123. * srcSize - size of data in srcBuffer
  124. * fileSizes - srcBuffer is considered cut into 1+ segments, to compress separately.
  125. * note : sum(fileSizes) must be == srcSize. (<== ensure it's properly checked)
  126. * nbFiles - nb of segments
  127. * cLevel - compression level
  128. * comprParams - basic compression parameters
  129. * dictBuffer - a dictionary if used, null otherwise
  130. * dictBufferSize - size of dictBuffer, 0 otherwise
  131. * displayLevel - see BMK_benchFiles
  132. * displayName - name used by display
  133. * @return:
  134. * a variant, which expresses either an error, or a valid result.
  135. * Use BMK_isSuccessful_benchOutcome() to check if function was successful.
  136. * If yes, extract the valid result with BMK_extract_benchResult(),
  137. * it will contain :
  138. * .cSpeed: compression speed in bytes per second,
  139. * .dSpeed: decompression speed in bytes per second,
  140. * .cSize : compressed size, in bytes
  141. * .cMem : memory budget required for the compression context
  142. */
  143. BMK_benchOutcome_t BMK_benchMem(const void* srcBuffer, size_t srcSize,
  144. const size_t* fileSizes, unsigned nbFiles,
  145. int cLevel, const ZSTD_compressionParameters* comprParams,
  146. const void* dictBuffer, size_t dictBufferSize,
  147. int displayLevel, const char* displayName);
  148. /* BMK_benchMemAdvanced() : used by Paramgrill
  149. * same as BMK_benchMem() with following additional options :
  150. * dstBuffer - destination buffer to write compressed output in, NULL if none provided.
  151. * dstCapacity - capacity of destination buffer, give 0 if dstBuffer = NULL
  152. * adv = see advancedParams_t
  153. */
  154. BMK_benchOutcome_t BMK_benchMemAdvanced(const void* srcBuffer, size_t srcSize,
  155. void* dstBuffer, size_t dstCapacity,
  156. const size_t* fileSizes, unsigned nbFiles,
  157. int cLevel, const ZSTD_compressionParameters* comprParams,
  158. const void* dictBuffer, size_t dictBufferSize,
  159. int displayLevel, const char* displayName,
  160. const BMK_advancedParams_t* adv);
  161. #endif /* BENCH_ZSTD_H_3242387 */