]> git.ipfire.org Git - thirdparty/zlib-ng.git/commitdiff
Improve benchmark_compress and benchmark_uncompress.
authorHans Kristian Rosbach <hk-git@circlestorm.org>
Sat, 27 Dec 2025 21:53:46 +0000 (22:53 +0100)
committerHans Kristian Rosbach <hk-github@circlestorm.org>
Wed, 7 Jan 2026 06:46:48 +0000 (07:46 +0100)
- These now use the same generated data as benchmark_inflate.
- benchmark_uncompress now also uses level 9 for compression, so that
  we also get 3-byte matches to uncompress.
- Improve error checking
- Unify code with benchmark_inflate

test/benchmarks/benchmark_compress.cc
test/benchmarks/benchmark_uncompress.cc

index 27048cd969d612a6d86535fb28c8277797104ef7..2408cb2d06d48d62823a2b2a53c1172d0fd0f7b4 100644 (file)
@@ -1,5 +1,5 @@
 /* benchmark_compress.cc -- benchmark compress()
- * Copyright (C) 2024 Hans Kristian Rosbach
+ * Copyright (C) 2024-2025 Hans Kristian Rosbach
  * For conditions of distribution and use, see copyright notice in zlib.h
  */
 
@@ -15,30 +15,32 @@ extern "C" {
 #  else
 #    include "zlib-ng.h"
 #  endif
+#  include "compressible_data_p.h"
 }
 
 #define MAX_SIZE (32 * 1024)
 
 class compress_bench: public benchmark::Fixture {
 private:
-    size_t maxlen;
     uint8_t *inbuff;
     uint8_t *outbuff;
 
 public:
-    void SetUp(const ::benchmark::State&) {
-        const char teststr[42] = "Hello hello World broken Test tast mello.";
-        maxlen = MAX_SIZE;
-
-        inbuff = (uint8_t *)zng_alloc(MAX_SIZE + 1);
-        assert(inbuff != NULL);
-
-        outbuff = (uint8_t *)zng_alloc(MAX_SIZE + 1);
-        assert(outbuff != NULL);
+    void SetUp(::benchmark::State& state) {
+        outbuff = (uint8_t *)malloc(MAX_SIZE + 16);
+        if (outbuff == NULL) {
+            state.SkipWithError("malloc failed");
+            return;
+        }
 
-        int pos = 0;
-        for (int32_t i = 0; i < MAX_SIZE - 42 ; i+=42){
-           pos += sprintf((char *)inbuff+pos, "%s", teststr);
+        // 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;
         }
     }
 
@@ -46,15 +48,20 @@ public:
         int err = 0;
 
         for (auto _ : state) {
-            err = PREFIX(compress)(outbuff, &maxlen, inbuff, (size_t)state.range(0));
+            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();
+            }
         }
 
         benchmark::DoNotOptimize(err);
     }
 
     void TearDown(const ::benchmark::State&) {
-        zng_free(inbuff);
-        zng_free(outbuff);
+        free(inbuff);
+        free(outbuff);
     }
 };
 
index 0e9b75682c1445fd40111211eacbd16b2b50a0be..d6f31bc6074968dc861fb9a84e3bdea338c06e1e 100644 (file)
@@ -1,5 +1,5 @@
 /* benchmark_uncompress.cc -- benchmark uncompress()
- * Copyright (C) 2024 Hans Kristian Rosbach
+ * Copyright (C) 2024-2025 Hans Kristian Rosbach
  * For conditions of distribution and use, see copyright notice in zlib.h
  */
 
@@ -15,6 +15,7 @@ extern "C" {
 #  else
 #    include "zlib-ng.h"
 #  endif
+#  include "compressible_data_p.h"
 }
 
 #define MAX_SIZE (1024 * 1024)
@@ -22,39 +23,39 @@ extern "C" {
 
 class uncompress_bench: public benchmark::Fixture {
 private:
-    size_t maxlen;
     uint8_t *inbuff;
     uint8_t *outbuff;
     uint8_t *compressed_buff[NUM_TESTS];
     z_uintmax_t compressed_sizes[NUM_TESTS];
-    size_t sizes[NUM_TESTS] = {1, 64, 1024, 16384, 128*1024, 1024*1024};
+    uint32_t sizes[NUM_TESTS] = {1, 64, 1024, 16384, 128*1024, 1024*1024};
 
 public:
-    void SetUp(const ::benchmark::State&) {
-        const char teststr[42] = "Hello hello World broken Test tast mello.";
-        maxlen = MAX_SIZE;
-
-        inbuff = (uint8_t *)zng_alloc(MAX_SIZE + 1);
-        assert(inbuff != NULL);
-
-        outbuff = (uint8_t *)zng_alloc(MAX_SIZE + 1);
-        assert(outbuff != NULL);
+    void SetUp(::benchmark::State& state) {
+        outbuff = (uint8_t *)malloc(MAX_SIZE + 16);
+        if (outbuff == NULL) {
+            state.SkipWithError("malloc failed");
+            return;
+        }
 
-        // Initialize input buffer
-        int pos = 0;
-        for (int32_t i = 0; i < MAX_SIZE - 42 ; i+=42){
-           pos += sprintf((char *)inbuff+pos, "%s", teststr);
+        // 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 (size_t i = 0; i < NUM_TESTS; ++i) {
-            compressed_buff[i] = (uint8_t *)zng_alloc(MAX_SIZE + 1);
+        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 = maxlen;
-            int err = PREFIX(compress)(compressed_buff[i], &compressed_size, inbuff, sizes[i]);
+            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, "Compression failed with error %d\n", err);
+                fprintf(stderr, "compress() failed with error %d\n", err);
                 abort();
             }
             compressed_sizes[i] = compressed_size;
@@ -62,24 +63,26 @@ public:
     }
 
     void Bench(benchmark::State& state) {
-        int err = 0;
+        int err;
 
         for (auto _ : state) {
             int index = 0;
-            while (sizes[index] != (size_t)state.range(0)) ++index;
+            while (sizes[index] != (uint32_t)state.range(0)) ++index;
 
-            z_uintmax_t out_size = maxlen;
+            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();
+            }
         }
-
-        benchmark::DoNotOptimize(err);
     }
 
     void TearDown(const ::benchmark::State&) {
-        zng_free(inbuff);
-        zng_free(outbuff);
+        free(inbuff);
+        free(outbuff);
 
-        for (size_t i = 0; i < NUM_TESTS; ++i) {
+        for (int i = 0; i < NUM_TESTS; ++i) {
             zng_free(compressed_buff[i]);
         }
     }