]>
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" | |
9f35e8b4 | 23 | #include "random-util.h" |
fd53fee0 | 24 | |
fa1c4b51 | 25 | typedef int (compress_t)(const void *src, uint64_t src_size, void *dst, size_t *dst_size); |
fd53fee0 | 26 | typedef int (decompress_t)(const void *src, uint64_t src_size, |
fa1c4b51 | 27 | void **dst, size_t *dst_alloc_size, size_t* dst_size, size_t dst_max); |
fd53fee0 ZJS |
28 | |
29 | #define MAX_SIZE (1024*1024LU) | |
30 | ||
9f35e8b4 | 31 | static char* make_buf(size_t count, const char *type) { |
fd53fee0 ZJS |
32 | char *buf; |
33 | size_t i; | |
34 | ||
35 | buf = malloc(count); | |
0c0cdb06 | 36 | assert_se(buf); |
fd53fee0 | 37 | |
9f35e8b4 ZJS |
38 | if (streq(type, "zeros")) |
39 | memzero(buf, count); | |
40 | else if (streq(type, "simple")) | |
41 | for (i = 0; i < count; i++) | |
42 | buf[i] = 'a' + i % ('z' - 'a' + 1); | |
43 | else if (streq(type, "random")) { | |
44 | random_bytes(buf, count/10); | |
45 | random_bytes(buf + 2*count/10, count/10); | |
46 | random_bytes(buf + 4*count/10, count/20); | |
47 | random_bytes(buf + 6*count/10, count/20); | |
48 | random_bytes(buf + 8*count/10, count/20); | |
49 | } else | |
50 | assert_not_reached("here"); | |
fd53fee0 ZJS |
51 | |
52 | return buf; | |
53 | } | |
54 | ||
9f35e8b4 | 55 | static void test_compress_decompress(const char* label, const char* type, |
fd53fee0 | 56 | compress_t compress, decompress_t decompress) { |
da2e1c71 | 57 | usec_t n, n2 = 0; |
fd53fee0 ZJS |
58 | float dt; |
59 | ||
60 | _cleanup_free_ char *text, *buf; | |
61 | _cleanup_free_ void *buf2 = NULL; | |
fa1c4b51 | 62 | size_t buf2_allocated = 0; |
fd53fee0 ZJS |
63 | size_t skipped = 0, compressed = 0, total = 0; |
64 | ||
9f35e8b4 | 65 | text = make_buf(MAX_SIZE, type); |
fd53fee0 | 66 | buf = calloc(MAX_SIZE + 1, 1); |
0c0cdb06 | 67 | assert_se(text && buf); |
fd53fee0 ZJS |
68 | |
69 | n = now(CLOCK_MONOTONIC); | |
70 | ||
71 | for (size_t i = 1; i <= MAX_SIZE; i += (i < 2048 ? 1 : 217)) { | |
fa1c4b51 | 72 | size_t j = 0, k = 0; |
fd53fee0 ZJS |
73 | int r; |
74 | ||
75 | r = compress(text, i, buf, &j); | |
06b643e7 | 76 | /* assume compression must be successful except for small inputs */ |
9f35e8b4 ZJS |
77 | assert_se(r == 0 || (i < 2048 && r == -ENOBUFS) || streq(type, "random")); |
78 | ||
fd53fee0 | 79 | /* check for overwrites */ |
0c0cdb06 | 80 | assert_se(buf[i] == 0); |
fd53fee0 ZJS |
81 | if (r != 0) { |
82 | skipped += i; | |
83 | continue; | |
84 | } | |
85 | ||
0c0cdb06 | 86 | assert_se(j > 0); |
fd53fee0 | 87 | if (j >= i) |
fa1c4b51 | 88 | log_error("%s \"compressed\" %zu -> %zu", label, i, j); |
fd53fee0 ZJS |
89 | |
90 | r = decompress(buf, j, &buf2, &buf2_allocated, &k, 0); | |
0c0cdb06 RC |
91 | assert_se(r == 0); |
92 | assert_se(buf2_allocated >= k); | |
93 | assert_se(k == i); | |
fd53fee0 | 94 | |
0c0cdb06 | 95 | assert_se(memcmp(text, buf2, i) == 0); |
fd53fee0 ZJS |
96 | |
97 | total += i; | |
98 | compressed += j; | |
99 | ||
100 | n2 = now(CLOCK_MONOTONIC); | |
101 | if (n2 - n > 60 * USEC_PER_SEC) | |
102 | break; | |
103 | } | |
104 | ||
105 | dt = (n2-n) / 1e6; | |
106 | ||
9f35e8b4 | 107 | log_info("%s/%s: compressed & decompressed %zu bytes in %.2fs (%.2fMiB/s), " |
fd53fee0 | 108 | "mean compresion %.2f%%, skipped %zu bytes", |
9f35e8b4 | 109 | label, type, total, dt, |
fd53fee0 ZJS |
110 | total / 1024. / 1024 / dt, |
111 | 100 - compressed * 100. / total, | |
112 | skipped); | |
113 | } | |
114 | ||
115 | int main(int argc, char *argv[]) { | |
9f35e8b4 | 116 | const char *i; |
fd53fee0 ZJS |
117 | |
118 | log_set_max_level(LOG_DEBUG); | |
119 | ||
9f35e8b4 | 120 | NULSTR_FOREACH(i, "zeros\0simple\0random\0") { |
fd53fee0 | 121 | #ifdef HAVE_XZ |
9f35e8b4 | 122 | test_compress_decompress("XZ", i, compress_blob_xz, decompress_blob_xz); |
fd53fee0 ZJS |
123 | #endif |
124 | #ifdef HAVE_LZ4 | |
9f35e8b4 | 125 | test_compress_decompress("LZ4", i, compress_blob_lz4, decompress_blob_lz4); |
fd53fee0 | 126 | #endif |
9f35e8b4 | 127 | } |
fd53fee0 ZJS |
128 | return 0; |
129 | } |