summaryrefslogtreecommitdiffstats
path: root/tests/framebench.c
blob: 01e30b343d35027cac72c897f1f1f5162fd1f6be (plain)
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
#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>

#define LZ4_STATIC_LINKING_ONLY
#include "lz4.h"
#include "lz4hc.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 {
  const char *run_name;
  size_t iter;
  LZ4_stream_t *ctx;
  LZ4_streamHC_t *hcctx;
  LZ4F_cctx *cctx;
  LZ4F_dctx *dctx;
  const char *dictbuf;
  size_t dictsize;
  char *obuf;
  size_t osize;
  const char* ibuf;
  const char* isample;
  size_t isize;
  size_t num_ibuf;
  char *checkbuf;
  size_t checksize;
  int clevel;
  const LZ4F_CDict* cdict;
  LZ4F_preferences_t* prefs;
  const LZ4F_compressOptions_t* options;
} bench_params_t;

size_t compress_frame(bench_params_t *p) {
  LZ4F_cctx *cctx = p->cctx;
  char *obuf = p->obuf;
  size_t osize = p->osize;
  const char* isample = p->isample;
  size_t isize = p->isize;
  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,
    isample,
    isize,
    cdict,
    prefs);
  LZ4F_CHECK(oused);

  return oused;
}

size_t compress_begin(bench_params_t *p) {
  LZ4F_cctx *cctx = p->cctx;
  char *obuf = p->obuf;
  size_t osize = p->osize;
  const char* isample = p->isample;
  size_t isize = p->isize;
  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,
    isample,
    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) {
  char *obuf = p->obuf;
  size_t osize = p->osize;
  const char* isample = p->isample;
  size_t isize = p->isize;

  char *oend = obuf + osize;
  size_t oused;

  oused = LZ4_compress_default(isample, obuf, isize, oend - obuf);
  obuf += oused;

  return obuf - p->obuf;
}

size_t compress_extState(bench_params_t *p) {
  LZ4_stream_t *ctx = p->ctx;
  char *obuf = p->obuf;
  size_t osize = p->osize;
  const char* isample = p->isample;
  size_t isize = p->isize;
  int clevel = p->clevel;

  char *oend = obuf + osize;
  size_t oused;

  oused = LZ4_compress_fast_extState_fastReset(
      ctx, isample, obuf, isize, oend - obuf, clevel);
  obuf += oused;

  return obuf - p->obuf;
}

size_t compress_hc(bench_params_t *p) {
  char *obuf = p->obuf;
  size_t osize = p->osize;
  const char* isample = p->isample;
  size_t isize = p->isize;
  int clevel = p->clevel;

  char *oend = obuf + osize;
  size_t oused;

  oused = LZ4_compress_HC(
      isample, obuf, isize, oend - obuf, clevel);
  obuf += oused;

  return obuf - p->obuf;
}

size_t compress_hc_extState(bench_params_t *p) {
  LZ4_streamHC_t *hcctx = p->hcctx;
  char *obuf = p->obuf;
  size_t osize = p->osize;
  const char* isample = p->isample;
  size_t isize = p->isize;
  int clevel = p->clevel;

  char *oend = obuf + osize;
  size_t oused;

  oused = LZ4_compress_HC_extStateHC(
      hcctx,
      isample, obuf,
      isize, oend - obuf, clevel);
  obuf += oused;

  return obuf - p->obuf;
}

size_t check_lz4(bench_params_t *p, size_t csize) {
  (void)csize;
  memset(p->checkbuf, 0xFF, p->checksize);
  return LZ4_decompress_fast_usingDict(p->obuf, p->checkbuf, p->isize,
                                       p->dictbuf, p->dictsize)
      && !memcmp(p->isample, p->checkbuf, p->isize);
}

size_t check_lz4f(bench_params_t *p, size_t csize) {
  size_t cp = 0;
  size_t dp = 0;
  size_t dsize = p->checksize;
  size_t cleft = csize;
  size_t dleft = dsize;
  size_t ret;
  memset(p->checkbuf, 0xFF, p->checksize);
  LZ4F_resetDecompressionContext(p->dctx);
  do {
    ret = LZ4F_decompress_usingDict(
        p->dctx, p->checkbuf + dp, &dleft, p->obuf + cp, &cleft,
        p->dictbuf, p->dictsize, NULL);
    cp += cleft;
    dp += dleft;
    cleft = csize - cp;
    dleft = dsize - dp;
    if (LZ4F_isError(ret)) return 0;
  } while (cleft);
  return !memcmp(p->isample, p->checkbuf, p->isize);
}


uint64_t bench(
    char *bench_name,
    size_t (*fun)(bench_params_t *),
    size_t (*checkfun)(bench_params_t *, size_t),
    bench_params_t *params
) {
  struct timespec start, end;
  size_t i, osize = 0, o = 0;
  size_t time_taken = 0;
  uint64_t total_repetitions = 0;
  uint64_t repetitions = 2;

  if (clock_gettime(CLOCK_MONOTONIC_RAW, &start)) return 0;

  while (time_taken < 25 * 1000 * 1000) { // benchmark over at least 1ms
    if (total_repetitions) {
      repetitions = total_repetitions; // double previous
    }

    for (i = 0; i < repetitions; i++) {
      params->iter = i;
      if (params->num_ibuf == 1) {
        params->isample = params->ibuf;
      } else {
        params->isample = params->ibuf + ((i * 2654435761U) % ((params->num_ibuf - 1) * params->isize));
      }
      o = fun(params);
      if (!o) return 0;
      osize += o;
    }

    if (clock_gettime(CLOCK_MONOTONIC_RAW, &end)) return 0;

    time_taken = (1000 * 1000 * 1000 * end.tv_sec + end.tv_nsec) -
                 (1000 * 1000 * 1000 * start.tv_sec + start.tv_nsec);
    total_repetitions += repetitions;
  }

  o = checkfun(params, o);
  if (!o) return 0;

  fprintf(
      stderr,
      "%-19s: %-30s @ lvl %2d: %8ld B -> %8ld B, %6ld iters, %10ld ns, %10ld ns/iter, %7.2lf MB/s\n",
      params->run_name, bench_name, params->clevel,
      params->isize, osize / total_repetitions,
      total_repetitions, time_taken, time_taken / total_repetitions,
      ((double) 1000 * params->isize * total_repetitions) / time_taken
  );

  return time_taken;
}

int main(int argc, char *argv[]) {
  char *run_name;

  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;

  size_t check_size;
  char *check_buf;

  LZ4_stream_t *ctx;
  LZ4_streamHC_t *hcctx;
  LZ4F_cctx *cctx;
  LZ4F_dctx *dctx;
  LZ4F_CDict *cdict;
  LZ4F_preferences_t prefs;
  LZ4F_compressOptions_t options;

  int clevels[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};

  bench_params_t params;

  if (argc != 4) return 1;
  run_name = argv[1];
  dict_fn = argv[2];
  in_fn = argv[3];

  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);
  }

  check_size = in_size;
  check_buf = (char *)malloc(check_size);
  if (!check_buf) return 1;

  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;

  if (LZ4F_isError(LZ4F_createDecompressionContext(&dctx, LZ4F_VERSION))) return 1;
  if (cctx == NULL) return 1;

  ctx = LZ4_createStream();
  if (ctx == NULL) return 1;

  hcctx = LZ4_createStreamHC();
  if (hcctx == 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.run_name = run_name;
  params.ctx = ctx;
  params.hcctx = hcctx;
  params.cctx = cctx;
  params.dctx = dctx;
  params.dictbuf = dict_buf;
  params.dictsize = dict_size;
  params.obuf = out_buf;
  params.osize = out_size;
  params.ibuf = in_buf;
  params.isize = in_size;
  params.num_ibuf = num_in_buf;
  params.checkbuf = check_buf;
  params.checksize = check_size;
  params.clevel = 1;
  params.cdict = NULL;
  params.prefs = &prefs;
  params.options = &options;

  for (unsigned int clevelidx = 0; clevelidx < sizeof(clevels) / sizeof(clevels[0]); clevelidx++) {
    params.clevel = clevels[clevelidx];
    params.prefs->compressionLevel = clevels[clevelidx];
    params.cdict = NULL;

    bench("LZ4_compress_default"         , compress_default    , check_lz4 , &params);
    bench("LZ4_compress_fast_extState"   , compress_extState   , check_lz4 , &params);
    bench("LZ4_compress_HC"              , compress_hc         , check_lz4 , &params);
    bench("LZ4_compress_HC_extStateHC"   , compress_hc_extState, check_lz4 , &params);
    bench("LZ4F_compressFrame"           , compress_frame      , check_lz4f, &params);
    bench("LZ4F_compressBegin"           , compress_begin      , check_lz4f, &params);

    params.cdict = cdict;

    bench("LZ4F_compressFrame_usingCDict", compress_frame      , check_lz4f, &params);
    bench("LZ4F_compressBegin_usingCDict", compress_begin      , check_lz4f, &params);
  }

  return 0;
}