1
0
mirror of https://github.com/facebook/zstd.git synced 2025-03-07 09:26:03 +02:00

Benchmark program for sequence compression API

This commit is contained in:
Elliot Gorokhovsky 2022-09-05 02:44:56 -07:00
parent 155d6a58a2
commit 61c79bf0d6
2 changed files with 113 additions and 0 deletions

58
contrib/seqBench/Makefile Normal file
View File

@ -0,0 +1,58 @@
# ################################################################
# Copyright (c) 2018-present, Yann Collet, Facebook, Inc.
# All rights reserved.
#
# This source code is licensed under both the BSD-style license (found in the
# LICENSE file in the root directory of this source tree) and the GPLv2 (found
# in the COPYING file in the root directory of this source tree).
# ################################################################
PROGDIR = ../../programs
LIBDIR = ../../lib
LIBZSTD = $(LIBDIR)/libzstd.a
CPPFLAGS+= -I$(LIBDIR) -I$(LIBDIR)/common -I$(LIBDIR)/dictBuilder -I$(PROGDIR)
CFLAGS ?= -O3 -g
CFLAGS += -std=gnu99
DEBUGFLAGS= -Wall -Wextra -Wcast-qual -Wcast-align -Wshadow \
-Wstrict-aliasing=1 -Wswitch-enum \
-Wstrict-prototypes -Wundef -Wpointer-arith \
-Wvla -Wformat=2 -Winit-self -Wfloat-equal -Wwrite-strings \
-Wredundant-decls
CFLAGS += $(DEBUGFLAGS) $(MOREFLAGS)
default: seqBench
all : seqBench
seqBench: util.o timefn.o benchfn.o datagen.o xxhash.o seqBench.c $(LIBZSTD)
$(CC) $(CPPFLAGS) $(CFLAGS) $^ $(LDFLAGS) -o $@
.PHONY: $(LIBZSTD)
$(LIBZSTD):
$(MAKE) -C $(LIBDIR) libzstd.a CFLAGS="$(CFLAGS)"
benchfn.o: $(PROGDIR)/benchfn.c
$(CC) $(CPPFLAGS) $(CFLAGS) $^ -c
timefn.o: $(PROGDIR)/timefn.c
$(CC) $(CPPFLAGS) $(CFLAGS) $^ -c
datagen.o: $(PROGDIR)/datagen.c
$(CC) $(CPPFLAGS) $(CFLAGS) $^ -c
util.o: $(PROGDIR)/util.c
$(CC) $(CPPFLAGS) $(CFLAGS) $^ -c
xxhash.o : $(LIBDIR)/common/xxhash.c
$(CC) $(CPPFLAGS) $(CFLAGS) $^ -c
clean:
$(RM) *.o
$(MAKE) -C $(LIBDIR) clean > /dev/null
$(RM) seqBench

View File

@ -0,0 +1,55 @@
#define ZSTD_STATIC_LINKING_ONLY
#include <zstd.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
int main(int argc, char *argv[]) {
ZSTD_CCtx* zc = ZSTD_createCCtx();
if (argc != 2) {
printf("Usage: seqBench <file>\n"); // TODO provide the block delim option here
return 1;
}
FILE *f = fopen(argv[1], "rb");
fseek(f, 0, SEEK_END);
long inBufSize = ftell(f);
fseek(f, 0, SEEK_SET);
char *inBuf = malloc(inBufSize + 1);
fread(inBuf, inBufSize, 1, f);
fclose(f);
// Should work fine for this benchmark, but we really need
// a function like ZSTD_compressBound() for sequences
size_t seqsSize = 2 * (inBufSize / sizeof(ZSTD_Sequence));
ZSTD_Sequence *seqs = (ZSTD_Sequence*)malloc(seqsSize * sizeof(ZSTD_Sequence));
char *outBuf = malloc(ZSTD_compressBound(inBufSize));
ZSTD_generateSequences(zc, seqs, seqsSize, inBuf, inBufSize);
ZSTD_CCtx_setParameter(zc, ZSTD_c_blockDelimiters, ZSTD_sf_explicitBlockDelimiters);
size_t outBufSize = ZSTD_compressSequences(zc, outBuf, inBufSize, seqs, seqsSize, inBuf, inBufSize);
if (ZSTD_isError(outBufSize)) {
printf("ERROR: %lu\n", outBufSize);
return 1;
}
char *validationBuf = malloc(inBufSize);
ZSTD_decompress(validationBuf, inBufSize, outBuf, outBufSize);
if (memcmp(inBuf, validationBuf, inBufSize) == 0) {
printf("Compression and decompression were successful!\n");
} else {
printf("ERROR: input and validation buffers don't match!\n");
for (int i = 0; i < inBufSize; i++) {
if (inBuf[i] != validationBuf[i]) {
printf("First bad index: %d\n", i);
break;
}
}
}
return 0;
}