benchzstd.h 7.7 KB

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