]> git.ipfire.org Git - thirdparty/zlib-ng.git/commitdiff
Add small compress() benchmark
authorHans Kristian Rosbach <hk-git@circlestorm.org>
Thu, 25 Jan 2024 23:13:20 +0000 (00:13 +0100)
committerHans Kristian Rosbach <hk-github@circlestorm.org>
Wed, 15 May 2024 09:28:44 +0000 (11:28 +0200)
test/benchmarks/CMakeLists.txt
test/benchmarks/benchmark_compress.cc [new file with mode: 0644]

index 616954fc0ebf9a658ac2e52c06a244d72335e423..0f812498127dbf79334dc03f23cde04378dc7828 100644 (file)
@@ -38,6 +38,7 @@ add_executable(benchmark_zlib
     benchmark_adler32_copy.cc
     benchmark_compare256.cc
     benchmark_compare256_rle.cc
+    benchmark_compress.cc
     benchmark_crc32.cc
     benchmark_main.cc
     benchmark_slidehash.cc
diff --git a/test/benchmarks/benchmark_compress.cc b/test/benchmarks/benchmark_compress.cc
new file mode 100644 (file)
index 0000000..f5eb619
--- /dev/null
@@ -0,0 +1,67 @@
+/* benchmark_compress.cc -- benchmark compress()
+ * Copyright (C) 2024 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
+}
+
+#define MAX_SIZE (32 * 1024)
+
+class compress: public benchmark::Fixture {
+private:
+    size_t maxlen;
+    uint8_t *inbuff;
+    uint8_t *outbuff;
+
+public:
+    void SetUp(const ::benchmark::State& 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);
+
+        int pos = 0;
+        for (int32_t i = 0; i < MAX_SIZE - 42 ; i+=42){
+           pos += sprintf((char *)inbuff+pos, "%s", teststr);
+        }
+    }
+
+    void Bench(benchmark::State& state) {
+        int err;
+
+        for (auto _ : state) {
+            err = PREFIX(compress)(outbuff, &maxlen, inbuff, (size_t)state.range(0));
+        }
+
+        benchmark::DoNotOptimize(err);
+    }
+
+    void TearDown(const ::benchmark::State& state) {
+        zng_free(inbuff);
+        zng_free(outbuff);
+    }
+};
+
+#define BENCHMARK_COMPRESS(name) \
+    BENCHMARK_DEFINE_F(compress, name)(benchmark::State& state) { \
+        Bench(state); \
+    } \
+    BENCHMARK_REGISTER_F(compress, name)->Arg(1)->Arg(8)->Arg(16)->Arg(32)->Arg(64)->Arg(512)->Arg(4<<10)->Arg(32<<10);
+
+BENCHMARK_COMPRESS(compress);