summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorHans Kristian Rosbach <hk-git@circlestorm.org>2025-01-29 16:54:36 +0100
committerHans Kristian Rosbach <hk-github@circlestorm.org>2025-02-01 12:41:22 +0100
commit057104f11a07ab0a7ae4565cebe112780aa618c5 (patch)
treea509630a4771a1984cf51ec26cb1ec7b21edb0f0 /test
parenta0fa24710c8faa1a746a20cfd5c7c24571e15ca4 (diff)
downloadProject-Tick-057104f11a07ab0a7ae4565cebe112780aa618c5.tar.gz
Project-Tick-057104f11a07ab0a7ae4565cebe112780aa618c5.zip
Add uncompress benchmark
Diffstat (limited to 'test')
-rw-r--r--test/benchmarks/CMakeLists.txt1
-rw-r--r--test/benchmarks/benchmark_uncompress.cc94
2 files changed, 95 insertions, 0 deletions
diff --git a/test/benchmarks/CMakeLists.txt b/test/benchmarks/CMakeLists.txt
index 1e6671ffe4..0efed96878 100644
--- a/test/benchmarks/CMakeLists.txt
+++ b/test/benchmarks/CMakeLists.txt
@@ -48,6 +48,7 @@ add_executable(benchmark_zlib
benchmark_crc32.cc
benchmark_main.cc
benchmark_slidehash.cc
+ benchmark_uncompress.cc
)
target_compile_definitions(benchmark_zlib PRIVATE -DBENCHMARK_STATIC_DEFINE)
diff --git a/test/benchmarks/benchmark_uncompress.cc b/test/benchmarks/benchmark_uncompress.cc
new file mode 100644
index 0000000000..58830c146a
--- /dev/null
+++ b/test/benchmarks/benchmark_uncompress.cc
@@ -0,0 +1,94 @@
+/* benchmark_uncompress.cc -- benchmark uncompress()
+ * 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 (1024 * 1024)
+#define NUM_TESTS 6
+
+class uncompress_bench: public benchmark::Fixture {
+private:
+ size_t maxlen;
+ uint8_t *inbuff;
+ uint8_t *outbuff;
+ uint8_t *compressed_buff[NUM_TESTS];
+ uLong compressed_sizes[NUM_TESTS];
+ int64_t sizes[NUM_TESTS] = {1, 64, 1024, 16384, 128*1024, 1024*1024};
+
+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);
+
+ // Initialize input buffer
+ int pos = 0;
+ for (int32_t i = 0; i < MAX_SIZE - 42 ; i+=42){
+ pos += sprintf((char *)inbuff+pos, "%s", teststr);
+ }
+
+ // Compress data into different buffers
+ for (size_t i = 0; i < NUM_TESTS; ++i) {
+ compressed_buff[i] = (uint8_t *)zng_alloc(MAX_SIZE + 1);
+ assert(compressed_buff[i] != NULL);
+
+ uLong compressed_size = maxlen;
+ int err = PREFIX(compress)(compressed_buff[i], &compressed_size, inbuff, sizes[i]);
+ if (err != Z_OK) {
+ fprintf(stderr, "Compression failed with error %d\n", err);
+ abort();
+ }
+ compressed_sizes[i] = compressed_size;
+ }
+ }
+
+ void Bench(benchmark::State& state) {
+ int err = 0;
+
+ for (auto _ : state) {
+ int index = 0;
+ while (sizes[index] != state.range(0)) ++index;
+
+ uLong out_size = maxlen;
+ err = PREFIX(uncompress)(outbuff, &out_size, compressed_buff[index], compressed_sizes[index]);
+ }
+
+ benchmark::DoNotOptimize(err);
+ }
+
+ void TearDown(const ::benchmark::State& state) {
+ zng_free(inbuff);
+ zng_free(outbuff);
+
+ for (size_t 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);