summaryrefslogtreecommitdiff
path: root/neozip/test/benchmarks/benchmark_compress.cc
blob: df042f71531ccd150b06cf889080bbef6ea45beb (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
/* benchmark_compress.cc -- benchmark compress()
 * Copyright (C) 2024-2025 Hans Kristian Rosbach
 * For conditions of distribution and use, see copyright notice in zlib.h
 */

#include <stdio.h>
#include <assert.h>
#include <benchmark/benchmark.h>

extern "C" {
#  include "zbuild.h"
#  include "zutil_p.h"
#  if defined(ZLIB_COMPAT)
#    include "zlib.h"
#  else
#    include "zlib-ng.h"
#  endif
#  include "test/compressible_data_p.h"
}

#define MAX_SIZE (64 * 1024)

class compress_bench: public benchmark::Fixture {
private:
    uint8_t *inbuff;
    uint8_t *outbuff;

public:
    void SetUp(::benchmark::State& state) {
        outbuff = (uint8_t *)malloc(MAX_SIZE + 16);
        if (outbuff == NULL) {
            state.SkipWithError("malloc failed");
            return;
        }

        // Initialize input buffer with highly compressible data, interspersed
        // with small amounts of random data and 3-byte matches.
        inbuff = gen_compressible_data(MAX_SIZE);
        if (inbuff == NULL) {
            free(outbuff);
            outbuff = NULL;
            state.SkipWithError("gen_compressible_data() failed");
            return;
        }
    }

    void Bench(benchmark::State& state) {
        int err = 0;

        for (auto _ : state) {
            z_uintmax_t compressed_size = MAX_SIZE + 16;
            err = PREFIX(compress)(outbuff, &compressed_size, inbuff, (size_t)state.range(0));
            if (err != Z_OK) {
                fprintf(stderr, "compress() failed with error %d\n", err);
                abort();
            }

            // Prevent the result from being optimized away
            benchmark::DoNotOptimize(err);
        }
    }

    void TearDown(const ::benchmark::State&) {
        free(inbuff);
        free(outbuff);
    }
};

#define BENCHMARK_COMPRESS(name) \
    BENCHMARK_DEFINE_F(compress_bench, name)(benchmark::State& state) { \
        Bench(state); \
    } \
    BENCHMARK_REGISTER_F(compress_bench, name)->Arg(1)->Arg(16)->Arg(48)->Arg(256)->Arg(1<<10)->Arg(4<<10)->Arg(16<<10)->Arg(64<<10);

BENCHMARK_COMPRESS(compress_bench);