summaryrefslogtreecommitdiff
path: root/neozip/test/benchmarks/benchmark_uncompress.cc
blob: 6a82c05d018082f4538ca476530053bd5d2c9262 (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
/* benchmark_uncompress.cc -- benchmark uncompress()
 * 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 (1024 * 1024)
#define NUM_TESTS 6

class uncompress_bench: public benchmark::Fixture {
private:
    uint8_t *inbuff;
    uint8_t *outbuff;
    uint8_t *compressed_buff[NUM_TESTS];
    z_uintmax_t compressed_sizes[NUM_TESTS];
    uint32_t sizes[NUM_TESTS] = {1, 64, 1024, 16384, 128*1024, 1024*1024};

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

        // Compress data into different buffers
        for (int i = 0; i < NUM_TESTS; ++i) {
            compressed_buff[i] = (uint8_t *)zng_alloc(sizes[i] + 64);
            assert(compressed_buff[i] != NULL);

            z_uintmax_t compressed_size = sizes[i] + 64;
            int err = PREFIX(compress2)(compressed_buff[i], &compressed_size, inbuff, sizes[i], Z_BEST_COMPRESSION);
            if (err != Z_OK) {
                fprintf(stderr, "compress() failed with error %d\n", err);
                abort();
            }
            compressed_sizes[i] = compressed_size;
        }
    }

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

        for (auto _ : state) {
            int index = 0;
            while (sizes[index] != (uint32_t)state.range(0)) ++index;

            z_uintmax_t out_size = MAX_SIZE;
            err = PREFIX(uncompress)(outbuff, &out_size, compressed_buff[index], compressed_sizes[index]);
            if (err != Z_OK) {
                fprintf(stderr, "uncompress() failed with error %d\n", err);
                abort();
            }
        }
    }

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

        for (int i = 0; i < NUM_TESTS; ++i) {
            zng_free(compressed_buff[i]);
        }
    }
};

#define BENCHMARK_UNCOMPRESS(name) \
    BENCHMARK_DEFINE_F(uncompress_bench, name)(benchmark::State& state) { \
        Bench(state); \
    } \
    BENCHMARK_REGISTER_F(uncompress_bench, name)->Arg(1)->Arg(64)->Arg(1024)->Arg(16<<10)->Arg(128<<10)->Arg(1024<<10);

BENCHMARK_UNCOMPRESS(uncompress_bench);