]>
Commit | Line | Data |
---|---|---|
fd53fee0 ZJS |
1 | /*** |
2 | This file is part of systemd | |
3 | ||
4 | Copyright 2014 Zbigniew Jędrzejewski-Szmek | |
5 | ||
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. | |
10 | ||
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. | |
15 | ||
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/>. | |
18 | ***/ | |
19 | ||
20 | #include "compress.h" | |
21 | #include "util.h" | |
22 | #include "macro.h" | |
23 | ||
fa1c4b51 | 24 | typedef int (compress_t)(const void *src, uint64_t src_size, void *dst, size_t *dst_size); |
fd53fee0 | 25 | typedef int (decompress_t)(const void *src, uint64_t src_size, |
fa1c4b51 | 26 | void **dst, size_t *dst_alloc_size, size_t* dst_size, size_t dst_max); |
fd53fee0 ZJS |
27 | |
28 | #define MAX_SIZE (1024*1024LU) | |
29 | ||
30 | static char* make_buf(size_t count) { | |
31 | char *buf; | |
32 | size_t i; | |
33 | ||
34 | buf = malloc(count); | |
35 | assert(buf); | |
36 | ||
37 | for (i = 0; i < count; i++) | |
38 | buf[i] = 'a' + i % ('z' - 'a' + 1); | |
39 | ||
40 | return buf; | |
41 | } | |
42 | ||
43 | static void test_compress_decompress(const char* label, | |
44 | compress_t compress, decompress_t decompress) { | |
da2e1c71 | 45 | usec_t n, n2 = 0; |
fd53fee0 ZJS |
46 | float dt; |
47 | ||
48 | _cleanup_free_ char *text, *buf; | |
49 | _cleanup_free_ void *buf2 = NULL; | |
fa1c4b51 | 50 | size_t buf2_allocated = 0; |
fd53fee0 ZJS |
51 | size_t skipped = 0, compressed = 0, total = 0; |
52 | ||
53 | text = make_buf(MAX_SIZE); | |
54 | buf = calloc(MAX_SIZE + 1, 1); | |
55 | assert(text && buf); | |
56 | ||
57 | n = now(CLOCK_MONOTONIC); | |
58 | ||
59 | for (size_t i = 1; i <= MAX_SIZE; i += (i < 2048 ? 1 : 217)) { | |
fa1c4b51 | 60 | size_t j = 0, k = 0; |
fd53fee0 ZJS |
61 | int r; |
62 | ||
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 */ | |
67 | assert(buf[i] == 0); | |
68 | if (r != 0) { | |
69 | skipped += i; | |
70 | continue; | |
71 | } | |
72 | ||
73 | assert(j > 0); | |
74 | if (j >= i) | |
fa1c4b51 | 75 | log_error("%s \"compressed\" %zu -> %zu", label, i, j); |
fd53fee0 ZJS |
76 | |
77 | r = decompress(buf, j, &buf2, &buf2_allocated, &k, 0); | |
78 | assert(r == 0); | |
79 | assert(buf2_allocated >= k); | |
80 | assert(k == i); | |
81 | ||
82 | assert(memcmp(text, buf2, i) == 0); | |
83 | ||
84 | total += i; | |
85 | compressed += j; | |
86 | ||
87 | n2 = now(CLOCK_MONOTONIC); | |
88 | if (n2 - n > 60 * USEC_PER_SEC) | |
89 | break; | |
90 | } | |
91 | ||
92 | dt = (n2-n) / 1e6; | |
93 | ||
94 | log_info("%s: compressed & decompressed %zu bytes in %.2fs (%.2fMiB/s), " | |
95 | "mean compresion %.2f%%, skipped %zu bytes", | |
96 | label, total, dt, | |
97 | total / 1024. / 1024 / dt, | |
98 | 100 - compressed * 100. / total, | |
99 | skipped); | |
100 | } | |
101 | ||
102 | int main(int argc, char *argv[]) { | |
103 | ||
104 | log_set_max_level(LOG_DEBUG); | |
105 | ||
106 | #ifdef HAVE_XZ | |
107 | test_compress_decompress("XZ", compress_blob_xz, decompress_blob_xz); | |
108 | #endif | |
109 | #ifdef HAVE_LZ4 | |
110 | test_compress_decompress("LZ4", compress_blob_lz4, decompress_blob_lz4); | |
111 | #endif | |
112 | return 0; | |
113 | } |