test-compress-benchmark.c revision fd53fee04b1f1c1ca1e30e8d470d7416900a35dc
d14abf155341d55053c76eeec58b787a456b753bRobert Mustacchi/***
d14abf155341d55053c76eeec58b787a456b753bRobert Mustacchi This file is part of systemd
d14abf155341d55053c76eeec58b787a456b753bRobert Mustacchi
d14abf155341d55053c76eeec58b787a456b753bRobert Mustacchi Copyright 2014 Zbigniew Jędrzejewski-Szmek
d14abf155341d55053c76eeec58b787a456b753bRobert Mustacchi
d14abf155341d55053c76eeec58b787a456b753bRobert Mustacchi systemd is free software; you can redistribute it and/or modify it
d14abf155341d55053c76eeec58b787a456b753bRobert Mustacchi under the terms of the GNU Lesser General Public License as published by
d14abf155341d55053c76eeec58b787a456b753bRobert Mustacchi the Free Software Foundation; either version 2.1 of the License, or
d14abf155341d55053c76eeec58b787a456b753bRobert Mustacchi (at your option) any later version.
d14abf155341d55053c76eeec58b787a456b753bRobert Mustacchi
d14abf155341d55053c76eeec58b787a456b753bRobert Mustacchi systemd is distributed in the hope that it will be useful, but
d14abf155341d55053c76eeec58b787a456b753bRobert Mustacchi WITHOUT ANY WARRANTY; without even the implied warranty of
d14abf155341d55053c76eeec58b787a456b753bRobert Mustacchi MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
d14abf155341d55053c76eeec58b787a456b753bRobert Mustacchi Lesser General Public License for more details.
d14abf155341d55053c76eeec58b787a456b753bRobert Mustacchi
d14abf155341d55053c76eeec58b787a456b753bRobert Mustacchi You should have received a copy of the GNU Lesser General Public License
d14abf155341d55053c76eeec58b787a456b753bRobert Mustacchi along with systemd; If not, see <http://www.gnu.org/licenses/>.
d14abf155341d55053c76eeec58b787a456b753bRobert Mustacchi***/
d14abf155341d55053c76eeec58b787a456b753bRobert Mustacchi
d14abf155341d55053c76eeec58b787a456b753bRobert Mustacchi#include "compress.h"
d14abf155341d55053c76eeec58b787a456b753bRobert Mustacchi#include "util.h"
d14abf155341d55053c76eeec58b787a456b753bRobert Mustacchi#include "macro.h"
d14abf155341d55053c76eeec58b787a456b753bRobert Mustacchi
d14abf155341d55053c76eeec58b787a456b753bRobert Mustacchitypedef int (compress_t)(const void *src, uint64_t src_size, void *dst, uint64_t *dst_size);
d14abf155341d55053c76eeec58b787a456b753bRobert Mustacchitypedef int (decompress_t)(const void *src, uint64_t src_size,
d14abf155341d55053c76eeec58b787a456b753bRobert Mustacchi void **dst, uint64_t *dst_alloc_size, uint64_t* dst_size, uint64_t dst_max);
d14abf155341d55053c76eeec58b787a456b753bRobert Mustacchi
d14abf155341d55053c76eeec58b787a456b753bRobert Mustacchi#define MAX_SIZE (1024*1024LU)
d14abf155341d55053c76eeec58b787a456b753bRobert Mustacchi
d14abf155341d55053c76eeec58b787a456b753bRobert Mustacchistatic char* make_buf(size_t count) {
d14abf155341d55053c76eeec58b787a456b753bRobert Mustacchi char *buf;
d14abf155341d55053c76eeec58b787a456b753bRobert Mustacchi size_t i;
d14abf155341d55053c76eeec58b787a456b753bRobert Mustacchi
d14abf155341d55053c76eeec58b787a456b753bRobert Mustacchi buf = malloc(count);
d14abf155341d55053c76eeec58b787a456b753bRobert Mustacchi assert(buf);
d14abf155341d55053c76eeec58b787a456b753bRobert Mustacchi
d14abf155341d55053c76eeec58b787a456b753bRobert Mustacchi for (i = 0; i < count; i++)
d14abf155341d55053c76eeec58b787a456b753bRobert Mustacchi buf[i] = 'a' + i % ('z' - 'a' + 1);
d14abf155341d55053c76eeec58b787a456b753bRobert Mustacchi
d14abf155341d55053c76eeec58b787a456b753bRobert Mustacchi return buf;
d14abf155341d55053c76eeec58b787a456b753bRobert Mustacchi}
d14abf155341d55053c76eeec58b787a456b753bRobert Mustacchi
d14abf155341d55053c76eeec58b787a456b753bRobert Mustacchistatic void test_compress_decompress(const char* label,
d14abf155341d55053c76eeec58b787a456b753bRobert Mustacchi compress_t compress, decompress_t decompress) {
d14abf155341d55053c76eeec58b787a456b753bRobert Mustacchi usec_t n, n2;
d14abf155341d55053c76eeec58b787a456b753bRobert Mustacchi float dt;
d14abf155341d55053c76eeec58b787a456b753bRobert Mustacchi
d14abf155341d55053c76eeec58b787a456b753bRobert Mustacchi _cleanup_free_ char *text, *buf;
d14abf155341d55053c76eeec58b787a456b753bRobert Mustacchi _cleanup_free_ void *buf2 = NULL;
d14abf155341d55053c76eeec58b787a456b753bRobert Mustacchi size_t buf2_allocated = 0;
d14abf155341d55053c76eeec58b787a456b753bRobert Mustacchi size_t skipped = 0, compressed = 0, total = 0;
d14abf155341d55053c76eeec58b787a456b753bRobert Mustacchi
d14abf155341d55053c76eeec58b787a456b753bRobert Mustacchi text = make_buf(MAX_SIZE);
d14abf155341d55053c76eeec58b787a456b753bRobert Mustacchi buf = calloc(MAX_SIZE + 1, 1);
d14abf155341d55053c76eeec58b787a456b753bRobert Mustacchi assert(text && buf);
d14abf155341d55053c76eeec58b787a456b753bRobert Mustacchi
d14abf155341d55053c76eeec58b787a456b753bRobert Mustacchi n = now(CLOCK_MONOTONIC);
d14abf155341d55053c76eeec58b787a456b753bRobert Mustacchi
d14abf155341d55053c76eeec58b787a456b753bRobert Mustacchi for (size_t i = 1; i <= MAX_SIZE; i += (i < 2048 ? 1 : 217)) {
d14abf155341d55053c76eeec58b787a456b753bRobert Mustacchi size_t j = 0, k = 0;
d14abf155341d55053c76eeec58b787a456b753bRobert Mustacchi int r;
d14abf155341d55053c76eeec58b787a456b753bRobert Mustacchi
d14abf155341d55053c76eeec58b787a456b753bRobert Mustacchi r = compress(text, i, buf, &j);
d14abf155341d55053c76eeec58b787a456b753bRobert Mustacchi /* assume compresion must be succesful except for small inputs */
d14abf155341d55053c76eeec58b787a456b753bRobert Mustacchi assert(r == 0 || (i < 2048 && r == -ENOBUFS));
d14abf155341d55053c76eeec58b787a456b753bRobert Mustacchi /* check for overwrites */
d14abf155341d55053c76eeec58b787a456b753bRobert Mustacchi assert(buf[i] == 0);
d14abf155341d55053c76eeec58b787a456b753bRobert Mustacchi if (r != 0) {
d14abf155341d55053c76eeec58b787a456b753bRobert Mustacchi skipped += i;
d14abf155341d55053c76eeec58b787a456b753bRobert Mustacchi continue;
d14abf155341d55053c76eeec58b787a456b753bRobert Mustacchi }
d14abf155341d55053c76eeec58b787a456b753bRobert Mustacchi
d14abf155341d55053c76eeec58b787a456b753bRobert Mustacchi assert(j > 0);
d14abf155341d55053c76eeec58b787a456b753bRobert Mustacchi if (j >= i)
d14abf155341d55053c76eeec58b787a456b753bRobert Mustacchi log_error("%s \"compressed\" %zu -> %zu", label, i, j);
d14abf155341d55053c76eeec58b787a456b753bRobert Mustacchi
d14abf155341d55053c76eeec58b787a456b753bRobert Mustacchi r = decompress(buf, j, &buf2, &buf2_allocated, &k, 0);
d14abf155341d55053c76eeec58b787a456b753bRobert Mustacchi assert(r == 0);
d14abf155341d55053c76eeec58b787a456b753bRobert Mustacchi assert(buf2_allocated >= k);
d14abf155341d55053c76eeec58b787a456b753bRobert Mustacchi assert(k == i);
d14abf155341d55053c76eeec58b787a456b753bRobert Mustacchi
d14abf155341d55053c76eeec58b787a456b753bRobert Mustacchi assert(memcmp(text, buf2, i) == 0);
d14abf155341d55053c76eeec58b787a456b753bRobert Mustacchi
d14abf155341d55053c76eeec58b787a456b753bRobert Mustacchi total += i;
d14abf155341d55053c76eeec58b787a456b753bRobert Mustacchi compressed += j;
d14abf155341d55053c76eeec58b787a456b753bRobert Mustacchi
d14abf155341d55053c76eeec58b787a456b753bRobert Mustacchi n2 = now(CLOCK_MONOTONIC);
d14abf155341d55053c76eeec58b787a456b753bRobert Mustacchi if (n2 - n > 60 * USEC_PER_SEC)
d14abf155341d55053c76eeec58b787a456b753bRobert Mustacchi break;
d14abf155341d55053c76eeec58b787a456b753bRobert Mustacchi }
d14abf155341d55053c76eeec58b787a456b753bRobert Mustacchi
d14abf155341d55053c76eeec58b787a456b753bRobert Mustacchi dt = (n2-n) / 1e6;
d14abf155341d55053c76eeec58b787a456b753bRobert Mustacchi
d14abf155341d55053c76eeec58b787a456b753bRobert Mustacchi log_info("%s: compressed & decompressed %zu bytes in %.2fs (%.2fMiB/s), "
d14abf155341d55053c76eeec58b787a456b753bRobert Mustacchi "mean compresion %.2f%%, skipped %zu bytes",
d14abf155341d55053c76eeec58b787a456b753bRobert Mustacchi label, total, dt,
d14abf155341d55053c76eeec58b787a456b753bRobert Mustacchi total / 1024. / 1024 / dt,
d14abf155341d55053c76eeec58b787a456b753bRobert Mustacchi 100 - compressed * 100. / total,
d14abf155341d55053c76eeec58b787a456b753bRobert Mustacchi skipped);
d14abf155341d55053c76eeec58b787a456b753bRobert Mustacchi}
d14abf155341d55053c76eeec58b787a456b753bRobert Mustacchi
d14abf155341d55053c76eeec58b787a456b753bRobert Mustacchiint main(int argc, char *argv[]) {
d14abf155341d55053c76eeec58b787a456b753bRobert Mustacchi
d14abf155341d55053c76eeec58b787a456b753bRobert Mustacchi log_set_max_level(LOG_DEBUG);
d14abf155341d55053c76eeec58b787a456b753bRobert Mustacchi
d14abf155341d55053c76eeec58b787a456b753bRobert Mustacchi#ifdef HAVE_XZ
d14abf155341d55053c76eeec58b787a456b753bRobert Mustacchi test_compress_decompress("XZ", compress_blob_xz, decompress_blob_xz);
d14abf155341d55053c76eeec58b787a456b753bRobert Mustacchi#endif
d14abf155341d55053c76eeec58b787a456b753bRobert Mustacchi#ifdef HAVE_LZ4
d14abf155341d55053c76eeec58b787a456b753bRobert Mustacchi test_compress_decompress("LZ4", compress_blob_lz4, decompress_blob_lz4);
d14abf155341d55053c76eeec58b787a456b753bRobert Mustacchi#endif
d14abf155341d55053c76eeec58b787a456b753bRobert Mustacchi return 0;
d14abf155341d55053c76eeec58b787a456b753bRobert Mustacchi}
d14abf155341d55053c76eeec58b787a456b753bRobert Mustacchi