2 This file is part of systemd
4 Copyright 2014 Zbigniew Jędrzejewski-Szmek
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
24 typedef int (compress_t)(const void *src, uint64_t src_size, void *dst, size_t *dst_size);
25 typedef int (decompress_t)(const void *src, uint64_t src_size,
26 void **dst, size_t *dst_alloc_size, size_t* dst_size, size_t dst_max);
28 #define MAX_SIZE (1024*1024LU)
30 static char* make_buf(size_t count) {
37 for (i = 0; i < count; i++)
38 buf[i] = 'a' + i % ('z' - 'a' + 1);
43 static void test_compress_decompress(const char* label,
44 compress_t compress, decompress_t decompress) {
48 _cleanup_free_ char *text, *buf;
49 _cleanup_free_ void *buf2 = NULL;
50 size_t buf2_allocated = 0;
51 size_t skipped = 0, compressed = 0, total = 0;
53 text = make_buf(MAX_SIZE);
54 buf = calloc(MAX_SIZE + 1, 1);
57 n = now(CLOCK_MONOTONIC);
59 for (size_t i = 1; i <= MAX_SIZE; i += (i < 2048 ? 1 : 217)) {
63 r = compress(text, i, buf, &j);
64 /* assume compresion must be succesful except for small inputs */
65 assert(r == 0 || (i < 2048 && r == -ENOBUFS));
66 /* check for overwrites */
75 log_error("%s \"compressed\" %zu -> %zu", label, i, j);
77 r = decompress(buf, j, &buf2, &buf2_allocated, &k, 0);
79 assert(buf2_allocated >= k);
82 assert(memcmp(text, buf2, i) == 0);
87 n2 = now(CLOCK_MONOTONIC);
88 if (n2 - n > 60 * USEC_PER_SEC)
94 log_info("%s: compressed & decompressed %zu bytes in %.2fs (%.2fMiB/s), "
95 "mean compresion %.2f%%, skipped %zu bytes",
97 total / 1024. / 1024 / dt,
98 100 - compressed * 100. / total,
102 int main(int argc, char *argv[]) {
104 log_set_max_level(LOG_DEBUG);
107 test_compress_decompress("XZ", compress_blob_xz, decompress_blob_xz);
110 test_compress_decompress("LZ4", compress_blob_lz4, decompress_blob_lz4);