blob: f14a681925ea9f32023e4358eb56a045d83f8edc [file] [log] [blame]
Yann Collet32fb4072017-08-18 16:52:05 -07001/*
Elliott Hughes44aba642023-09-12 20:18:59 +00002 * Copyright (c) Meta Platforms, Inc. and affiliates.
Yann Collet4ded9e52016-08-30 10:04:33 -07003 * All rights reserved.
4 *
Yann Collet32fb4072017-08-18 16:52:05 -07005 * 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).
Yann Collet3128e032017-09-08 00:09:23 -07008 * You may select, at your option, one of the above-listed licenses.
Yann Collet4ded9e52016-08-30 10:04:33 -07009 */
Yann Collet4856a002015-01-24 01:58:16 +010010
Yann Colletd38063f2018-11-13 11:01:59 -080011 /* benchzstd :
12 * benchmark Zstandard compression / decompression
13 * over a set of files or buffers
14 * and display progress result and final summary
15 */
16
George Lu01d940b2018-06-11 10:59:05 -040017#if defined (__cplusplus)
18extern "C" {
19#endif
Yann Collet4856a002015-01-24 01:58:16 +010020
Yann Colletd38063f2018-11-13 11:01:59 -080021#ifndef BENCH_ZSTD_H_3242387
22#define BENCH_ZSTD_H_3242387
Yann Collet4856a002015-01-24 01:58:16 +010023
Yann Collet1f9ec132018-08-23 16:03:30 -070024/* === Dependencies === */
Yann Colletd9465012016-12-06 16:49:23 -080025#include <stddef.h> /* size_t */
Przemyslaw Skibinski897b8bb2016-12-13 13:03:41 +010026#define ZSTD_STATIC_LINKING_ONLY /* ZSTD_compressionParameters */
W. Felix Handte7dcca6b2020-05-01 16:20:40 -040027#include "../lib/zstd.h" /* ZSTD_compressionParameters */
Yann Collet4856a002015-01-24 01:58:16 +010028
Yann Collet1f9ec132018-08-23 16:03:30 -070029
30/* === Constants === */
31
32#define MB_UNIT 1000000
33
34
35/* === Benchmark functions === */
36
37/* Creates a variant `typeName`, able to express "error or valid result".
38 * Functions with return type `typeName`
39 * must first check if result is valid, using BMK_isSuccessful_*(),
40 * and only then can extract `baseType`.
George Lu0d1ee222018-06-15 16:21:08 -040041 */
Yann Collet1f9ec132018-08-23 16:03:30 -070042#define VARIANT_ERROR_RESULT(baseType, variantName) \
Yann Collet2e45bad2018-08-23 14:21:18 -070043 \
44typedef struct { \
45 baseType internal_never_use_directly; \
46 int tag; \
47} variantName
48
George Lu20f4f322018-06-12 15:54:43 -040049
George Lu0e808d62018-06-04 16:32:37 -070050typedef struct {
51 size_t cSize;
Yann Collet77e805e2018-08-21 18:19:27 -070052 unsigned long long cSpeed; /* bytes / sec */
53 unsigned long long dSpeed;
Yann Colletefbc3e82018-10-04 14:27:13 -070054 size_t cMem; /* memory usage during compression */
Yann Collet2e45bad2018-08-23 14:21:18 -070055} BMK_benchResult_t;
George Lu0e808d62018-06-04 16:32:37 -070056
Yann Collet1f9ec132018-08-23 16:03:30 -070057VARIANT_ERROR_RESULT(BMK_benchResult_t, BMK_benchOutcome_t);
Yann Collet2e45bad2018-08-23 14:21:18 -070058
59/* check first if the return structure represents an error or a valid result */
Yann Collet1f9ec132018-08-23 16:03:30 -070060int BMK_isSuccessful_benchOutcome(BMK_benchOutcome_t outcome);
Yann Collet2e45bad2018-08-23 14:21:18 -070061
62/* extract result from variant type.
63 * note : this function will abort() program execution if result is not valid
Yann Collet1f9ec132018-08-23 16:03:30 -070064 * check result validity first, by using BMK_isSuccessful_benchOutcome()
Yann Collet2e45bad2018-08-23 14:21:18 -070065 */
Yann Collet1f9ec132018-08-23 16:03:30 -070066BMK_benchResult_t BMK_extract_benchResult(BMK_benchOutcome_t outcome);
Yann Collet2e45bad2018-08-23 14:21:18 -070067
George Lu20f4f322018-06-12 15:54:43 -040068
Yann Collet77e805e2018-08-21 18:19:27 -070069/*! BMK_benchFiles() -- called by zstdcli */
70/* Loads files from fileNamesTable into memory,
Yann Collet1f9ec132018-08-23 16:03:30 -070071 * and an optional dictionary from dictFileName (can be NULL),
Yann Collet77e805e2018-08-21 18:19:27 -070072 * then uses benchMem().
Yann Collet1f9ec132018-08-23 16:03:30 -070073 * fileNamesTable - name of files to benchmark.
74 * nbFiles - number of files (size of fileNamesTable), must be > 0.
75 * dictFileName - name of dictionary file to load.
76 * cLevel - compression level to benchmark, errors if invalid.
77 * compressionParams - advanced compression Parameters.
78 * displayLevel - what gets printed:
Yann Collet77e805e2018-08-21 18:19:27 -070079 * 0 : no display;
80 * 1 : errors;
81 * 2 : + result + interaction + warnings;
Yann Collet1f9ec132018-08-23 16:03:30 -070082 * 3 : + information;
83 * 4 : + debug
Elliott Hughes44aba642023-09-12 20:18:59 +000084 * @return: 0 on success, !0 on error
George Lu20f4f322018-06-12 15:54:43 -040085 */
Elliott Hughes44aba642023-09-12 20:18:59 +000086int BMK_benchFiles(
87 const char* const * fileNamesTable, unsigned nbFiles,
88 const char* dictFileName,
89 int cLevel, const ZSTD_compressionParameters* compressionParams,
90 int displayLevel);
George Lu0d1ee222018-06-15 16:21:08 -040091
George Lud6121ad2018-06-22 17:25:16 -070092
93typedef enum {
94 BMK_both = 0,
95 BMK_decodeOnly = 1,
96 BMK_compressOnly = 2
97} BMK_mode_t;
98
99typedef struct {
Yann Colletb8701102019-01-25 15:11:50 -0800100 BMK_mode_t mode; /* 0: all, 1: compress only 2: decode only */
101 unsigned nbSeconds; /* default timing is in nbSeconds */
102 size_t blockSize; /* Maximum size of each block*/
103 int nbWorkers; /* multithreading */
104 unsigned realTime; /* real time priority */
105 int additionalParam; /* used by python speed benchmark */
106 int ldmFlag; /* enables long distance matching */
107 int ldmMinMatch; /* below: parameters for long distance matching, see zstd.1.md */
108 int ldmHashLog;
109 int ldmBucketSizeLog;
110 int ldmHashRateLog;
senhuang42b5c35d72021-09-20 09:04:07 -0400111 ZSTD_paramSwitch_e literalCompressionMode;
Nick Terrell46944232020-11-02 17:52:29 -0800112 int useRowMatchFinder; /* use row-based matchfinder if possible */
George Lud6121ad2018-06-22 17:25:16 -0700113} BMK_advancedParams_t;
114
115/* returns default parameters used by nonAdvanced functions */
116BMK_advancedParams_t BMK_initAdvancedParams(void);
117
Yann Collet77e805e2018-08-21 18:19:27 -0700118/*! BMK_benchFilesAdvanced():
119 * Same as BMK_benchFiles(),
120 * with more controls, provided through advancedParams_t structure */
Elliott Hughes44aba642023-09-12 20:18:59 +0000121int 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);
George Lu20f4f322018-06-12 15:54:43 -0400126
Yann Collet77e805e2018-08-21 18:19:27 -0700127/*! BMK_syntheticTest() -- called from zstdcli */
128/* Generates a sample with datagen, using compressibility argument */
129/* cLevel - compression level to benchmark, errors if invalid
130 * compressibility - determines compressibility of sample
131 * compressionParams - basic compression Parameters
132 * displayLevel - see benchFiles
133 * adv - see advanced_Params_t
Elliott Hughes44aba642023-09-12 20:18:59 +0000134 * @return: 0 on success, !0 on error
George Lua8eea992018-06-19 10:58:22 -0700135 */
Elliott Hughes44aba642023-09-12 20:18:59 +0000136int BMK_syntheticTest(int cLevel, double compressibility,
137 const ZSTD_compressionParameters* compressionParams,
138 int displayLevel, const BMK_advancedParams_t* adv);
George Lua8eea992018-06-19 10:58:22 -0700139
Yann Collet2e45bad2018-08-23 14:21:18 -0700140
141
142/* === Benchmark Zstandard in a memory-to-memory scenario === */
143
Yann Collet77e805e2018-08-21 18:19:27 -0700144/** BMK_benchMem() -- core benchmarking function, called in paramgrill
145 * applies ZSTD_compress_generic() and ZSTD_decompress_generic() on data in srcBuffer
146 * with specific compression parameters provided by other arguments using benchFunction
147 * (cLevel, comprParams + adv in advanced Mode) */
148/* srcBuffer - data source, expected to be valid compressed data if in Decode Only Mode
149 * srcSize - size of data in srcBuffer
150 * fileSizes - srcBuffer is considered cut into 1+ segments, to compress separately.
151 * note : sum(fileSizes) must be == srcSize. (<== ensure it's properly checked)
152 * nbFiles - nb of segments
153 * cLevel - compression level
154 * comprParams - basic compression parameters
155 * dictBuffer - a dictionary if used, null otherwise
156 * dictBufferSize - size of dictBuffer, 0 otherwise
Josh Sorefa880ca22019-04-12 14:18:11 -0400157 * displayLevel - see BMK_benchFiles
Yann Collet77e805e2018-08-21 18:19:27 -0700158 * displayName - name used by display
Yann Collet1f9ec132018-08-23 16:03:30 -0700159 * @return:
160 * a variant, which expresses either an error, or a valid result.
161 * Use BMK_isSuccessful_benchOutcome() to check if function was successful.
162 * If yes, extract the valid result with BMK_extract_benchResult(),
163 * it will contain :
164 * .cSpeed: compression speed in bytes per second,
165 * .dSpeed: decompression speed in bytes per second,
166 * .cSize : compressed size, in bytes
167 * .cMem : memory budget required for the compression context
George Lu20f4f322018-06-12 15:54:43 -0400168 */
Yann Collet2e45bad2018-08-23 14:21:18 -0700169BMK_benchOutcome_t BMK_benchMem(const void* srcBuffer, size_t srcSize,
George Lu01d940b2018-06-11 10:59:05 -0400170 const size_t* fileSizes, unsigned nbFiles,
Yann Collet77e805e2018-08-21 18:19:27 -0700171 int cLevel, const ZSTD_compressionParameters* comprParams,
George Lu01d940b2018-06-11 10:59:05 -0400172 const void* dictBuffer, size_t dictBufferSize,
George Lu01d940b2018-06-11 10:59:05 -0400173 int displayLevel, const char* displayName);
George Lu0e808d62018-06-04 16:32:37 -0700174
Yann Colletd38063f2018-11-13 11:01:59 -0800175
Elliott Hughes44aba642023-09-12 20:18:59 +0000176/* BMK_benchMemAdvanced() : used by Paramgrill
177 * same as BMK_benchMem() with following additional options :
George Lu7b5b3d72018-07-16 16:16:31 -0700178 * dstBuffer - destination buffer to write compressed output in, NULL if none provided.
179 * dstCapacity - capacity of destination buffer, give 0 if dstBuffer = NULL
Yann Collet77e805e2018-08-21 18:19:27 -0700180 * adv = see advancedParams_t
George Lu7b5b3d72018-07-16 16:16:31 -0700181 */
Yann Collet2e45bad2018-08-23 14:21:18 -0700182BMK_benchOutcome_t BMK_benchMemAdvanced(const void* srcBuffer, size_t srcSize,
Yann Collet77e805e2018-08-21 18:19:27 -0700183 void* dstBuffer, size_t dstCapacity,
George Lu20f4f322018-06-12 15:54:43 -0400184 const size_t* fileSizes, unsigned nbFiles,
Yann Collet77e805e2018-08-21 18:19:27 -0700185 int cLevel, const ZSTD_compressionParameters* comprParams,
George Lu20f4f322018-06-12 15:54:43 -0400186 const void* dictBuffer, size_t dictBufferSize,
George Lu20f4f322018-06-12 15:54:43 -0400187 int displayLevel, const char* displayName,
188 const BMK_advancedParams_t* adv);
189
Yann Collet2e45bad2018-08-23 14:21:18 -0700190
191
Yann Colletd38063f2018-11-13 11:01:59 -0800192#endif /* BENCH_ZSTD_H_3242387 */
George Lu01d940b2018-06-11 10:59:05 -0400193
194#if defined (__cplusplus)
195}
196#endif