1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
|
#include <assert.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>
#include "lz4.h"
#include "lz4frame.h"
#include "lz4frame_static.h"
#define LZ4F_CHECK(x) { typeof(x) _x = (x); if (LZ4F_isError(_x)) { fprintf(stderr, "Error!: %s\n", LZ4F_getErrorName(_x)); return 0; } }
typedef struct {
size_t iter;
LZ4_stream_t *ctx;
LZ4F_cctx *cctx;
char *obuf;
size_t osize;
const char* ibuf;
size_t isize;
size_t num_ibuf;
const LZ4F_CDict* cdict;
LZ4F_preferences_t* prefs;
const LZ4F_compressOptions_t* options;
} bench_params_t;
size_t compress_frame(bench_params_t *p) {
size_t iter = p->iter;
LZ4F_cctx *cctx = p->cctx;
char *obuf = p->obuf;
size_t osize = p->osize;
const char* ibuf = p->ibuf;
size_t isize = p->isize;
size_t num_ibuf = p->num_ibuf;
const LZ4F_CDict* cdict = p->cdict;
LZ4F_preferences_t* prefs = p->prefs;
size_t oused;
prefs->frameInfo.contentSize = isize;
oused = LZ4F_compressFrame_usingCDict(
cctx,
obuf,
osize,
ibuf + ((iter * 2654435761U) % num_ibuf) * isize,
isize,
cdict,
prefs);
LZ4F_CHECK(oused);
return oused;
}
size_t compress_begin(bench_params_t *p) {
size_t iter = p->iter;
LZ4F_cctx *cctx = p->cctx;
char *obuf = p->obuf;
size_t osize = p->osize;
const char* ibuf = p->ibuf;
size_t isize = p->isize;
size_t num_ibuf = p->num_ibuf;
const LZ4F_CDict* cdict = p->cdict;
LZ4F_preferences_t* prefs = p->prefs;
const LZ4F_compressOptions_t* options = p->options;
char *oend = obuf + osize;
size_t oused;
prefs->frameInfo.contentSize = isize;
oused = LZ4F_compressBegin_usingCDict(cctx, obuf, oend - obuf, cdict, prefs);
LZ4F_CHECK(oused);
obuf += oused;
oused = LZ4F_compressUpdate(
cctx,
obuf,
oend - obuf,
ibuf + ((iter * 2654435761U) % num_ibuf) * isize,
isize,
options);
LZ4F_CHECK(oused);
obuf += oused;
oused = LZ4F_compressEnd(cctx, obuf, oend - obuf, options);
LZ4F_CHECK(oused);
return obuf - p->obuf;
}
size_t compress_default(bench_params_t *p) {
size_t iter = p->iter;
char *obuf = p->obuf;
size_t osize = p->osize;
const char* ibuf = p->ibuf;
size_t isize = p->isize;
size_t num_ibuf = p->num_ibuf;
char *oend = obuf + osize;
size_t oused;
oused = LZ4_compress_default(ibuf + ((iter * 2654435761U) % num_ibuf) * isize, obuf, isize, oend - obuf);
obuf += oused;
return obuf - p->obuf;
}
size_t compress_extState(bench_params_t *p) {
size_t iter = p->iter;
LZ4_stream_t *ctx = p->ctx;
char *obuf = p->obuf;
size_t osize = p->osize;
const char* ibuf = p->ibuf;
size_t isize = p->isize;
size_t num_ibuf = p->num_ibuf;
char *oend = obuf + osize;
size_t oused;
oused = LZ4_compress_fast_safeExtState(ctx, ibuf + ((iter * 2654435761U) % num_ibuf) * isize, obuf, isize, oend - obuf, 0);
obuf += oused;
return obuf - p->obuf;
}
uint64_t bench(
size_t (*fun)(bench_params_t *),
uint64_t repetitions,
bench_params_t *params,
size_t *osizePtr
) {
struct timespec start, end;
size_t i, osize = 0;
if (clock_gettime(CLOCK_MONOTONIC_RAW, &start)) return 0;
for (i = 0; i < repetitions; i++) {
size_t o;
params->iter = i;
o = fun(params);
if (!o) return 0;
osize += o;
}
if (clock_gettime(CLOCK_MONOTONIC_RAW, &end)) return 0;
*osizePtr = osize / repetitions;
return (1000 * 1000 * 1000 * end.tv_sec + end.tv_nsec) - (1000 * 1000 * 1000 * start.tv_sec + start.tv_nsec);
}
int main(int argc, char *argv[]) {
struct stat st;
size_t bytes_read;
const char *dict_fn;
size_t dict_size;
char *dict_buf;
FILE *dict_file;
const char *in_fn;
size_t in_size;
size_t num_in_buf;
size_t cur_in_buf;
char *in_buf;
FILE *in_file;
size_t out_size;
char *out_buf;
LZ4_stream_t *ctx;
LZ4F_cctx *cctx;
LZ4F_CDict *cdict;
LZ4F_preferences_t prefs;
LZ4F_compressOptions_t options;
size_t out_used;
uint64_t time_taken;
uint64_t repetitions;
bench_params_t params;
if (argc != 3) return 1;
dict_fn = argv[1];
in_fn = argv[2];
if (stat(dict_fn, &st)) return 1;
dict_size = st.st_size;
dict_buf = (char *)malloc(dict_size);
if (!dict_buf) return 1;
dict_file = fopen(dict_fn, "r");
bytes_read = fread(dict_buf, 1, dict_size, dict_file);
if (bytes_read != dict_size) return 1;
if (stat(in_fn, &st)) return 1;
in_size = st.st_size;
num_in_buf = 256 * 1024 * 1024 / in_size;
if (num_in_buf == 0) {
num_in_buf = 1;
}
in_buf = (char *)malloc(in_size * num_in_buf);
if (!in_buf) return 1;
in_file = fopen(in_fn, "r");
bytes_read = fread(in_buf, 1, in_size, in_file);
if (bytes_read != in_size) return 1;
for(cur_in_buf = 1; cur_in_buf < num_in_buf; cur_in_buf++) {
memcpy(in_buf + cur_in_buf * in_size, in_buf, in_size);
}
if (in_size <= 1024) {
repetitions = 100000;
} else
if (in_size <= 16384) {
repetitions = 10000;
} else
if (in_size <= 131072) {
repetitions = 1000;
} else
if (in_size <= 1048576) {
repetitions = 100;
} else {
repetitions = 50;
}
memset(&prefs, 0, sizeof(prefs));
prefs.autoFlush = 1;
if (in_size < 64 * 1024)
prefs.frameInfo.blockMode = LZ4F_blockIndependent;
prefs.frameInfo.contentSize = in_size;
memset(&options, 0, sizeof(options));
options.stableSrc = 1;
out_size = LZ4F_compressFrameBound(in_size, &prefs);
out_buf = (char *)malloc(out_size);
if (!out_buf) return 1;
if (LZ4F_isError(LZ4F_createCompressionContext(&cctx, LZ4F_VERSION))) return 1;
if (cctx == NULL) return 1;
ctx = LZ4_createStream();
if (ctx == NULL) return 1;
cdict = LZ4F_createCDict(dict_buf, dict_size);
if (!cdict) return 1;
fprintf(stderr, "dict size: %zd\n", dict_size);
fprintf(stderr, "input size: %zd\n", in_size);
params.ctx = ctx;
params.cctx = cctx;
params.obuf = out_buf;
params.osize = out_size;
params.ibuf = in_buf;
params.isize = in_size;
params.num_ibuf = num_in_buf;
params.cdict = NULL;
params.prefs = &prefs;
params.options = &options;
time_taken = bench(compress_default, repetitions, ¶ms, &out_used);
fprintf(stderr, "LZ4_compress_default : %8ld B -> %8ld B, %9ld ns/iter, %6.1lf MB/s\n", in_size, out_used, time_taken / repetitions, ((double) 1000 * in_size * repetitions) / time_taken);
time_taken = bench(compress_extState, repetitions, ¶ms, &out_used);
fprintf(stderr, "LZ4_compress_fast_extState : %8ld B -> %8ld B, %9ld ns/iter, %6.1lf MB/s\n", in_size, out_used, time_taken / repetitions, ((double) 1000 * in_size * repetitions) / time_taken);
time_taken = bench(compress_frame, repetitions, ¶ms, &out_used);
fprintf(stderr, "LZ4F_compressFrame : %8ld B -> %8ld B, %9ld ns/iter, %6.1lf MB/s\n", in_size, out_used, time_taken / repetitions, ((double) 1000 * in_size * repetitions) / time_taken);
time_taken = bench(compress_begin, repetitions, ¶ms, &out_used);
fprintf(stderr, "LZ4F_compressBegin : %8ld B -> %8ld B, %9ld ns/iter, %6.1lf MB/s\n", in_size, out_used, time_taken / repetitions, ((double) 1000 * in_size * repetitions) / time_taken);
params.cdict = cdict;
time_taken = bench(compress_frame, repetitions, ¶ms, &out_used);
fprintf(stderr, "LZ4F_compressFrame_usingCDict: %8ld B -> %8ld B, %9ld ns/iter, %6.1lf MB/s\n", in_size, out_used, time_taken / repetitions, ((double) 1000 * in_size * repetitions) / time_taken);
time_taken = bench(compress_begin, repetitions, ¶ms, &out_used);
fprintf(stderr, "LZ4F_compressBegin_usingCDict: %8ld B -> %8ld B, %9ld ns/iter, %6.1lf MB/s\n", in_size, out_used, time_taken / repetitions, ((double) 1000 * in_size * repetitions) / time_taken);
return 0;
}
|