]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/journal/test-compress-benchmark.c
build-sys: use #if Y instead of #ifdef Y everywhere
[thirdparty/systemd.git] / src / journal / test-compress-benchmark.c
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 "alloc-util.h"
21 #include "compress.h"
22 #include "env-util.h"
23 #include "macro.h"
24 #include "parse-util.h"
25 #include "random-util.h"
26 #include "string-util.h"
27 #include "util.h"
28
29 typedef int (compress_t)(const void *src, uint64_t src_size, void *dst,
30 size_t dst_alloc_size, size_t *dst_size);
31 typedef int (decompress_t)(const void *src, uint64_t src_size,
32 void **dst, size_t *dst_alloc_size, size_t* dst_size, size_t dst_max);
33
34 #if HAVE_XZ || HAVE_LZ4
35
36 static usec_t arg_duration;
37 static size_t arg_start;
38
39 #define MAX_SIZE (1024*1024LU)
40 #define PRIME 1048571 /* A prime close enough to one megabyte that mod 4 == 3 */
41
42 static size_t _permute(size_t x) {
43 size_t residue;
44
45 if (x >= PRIME)
46 return x;
47
48 residue = x*x % PRIME;
49 if (x <= PRIME / 2)
50 return residue;
51 else
52 return PRIME - residue;
53 }
54
55 static size_t permute(size_t x) {
56 return _permute((_permute(x) + arg_start) % MAX_SIZE ^ 0xFF345);
57 }
58
59 static char* make_buf(size_t count, const char *type) {
60 char *buf;
61 size_t i;
62
63 buf = malloc(count);
64 assert_se(buf);
65
66 if (streq(type, "zeros"))
67 memzero(buf, count);
68 else if (streq(type, "simple"))
69 for (i = 0; i < count; i++)
70 buf[i] = 'a' + i % ('z' - 'a' + 1);
71 else if (streq(type, "random")) {
72 size_t step = count / 10;
73
74 random_bytes(buf, step);
75 memzero(buf + 1*step, step);
76 random_bytes(buf + 2*step, step);
77 memzero(buf + 3*step, step);
78 random_bytes(buf + 4*step, step);
79 memzero(buf + 5*step, step);
80 random_bytes(buf + 6*step, step);
81 memzero(buf + 7*step, step);
82 random_bytes(buf + 8*step, step);
83 memzero(buf + 9*step, step);
84 } else
85 assert_not_reached("here");
86
87 return buf;
88 }
89
90 static void test_compress_decompress(const char* label, const char* type,
91 compress_t compress, decompress_t decompress) {
92 usec_t n, n2 = 0;
93 float dt;
94
95 _cleanup_free_ char *text, *buf;
96 _cleanup_free_ void *buf2 = NULL;
97 size_t buf2_allocated = 0;
98 size_t skipped = 0, compressed = 0, total = 0;
99
100 text = make_buf(MAX_SIZE, type);
101 buf = calloc(MAX_SIZE + 1, 1);
102 assert_se(text && buf);
103
104 n = now(CLOCK_MONOTONIC);
105
106 for (size_t i = 0; i <= MAX_SIZE; i++) {
107 size_t j = 0, k = 0, size;
108 int r;
109
110 size = permute(i);
111 if (size == 0)
112 continue;
113
114 log_debug("%s %zu %zu", type, i, size);
115
116 memzero(buf, MIN(size + 1000, MAX_SIZE));
117
118 r = compress(text, size, buf, size, &j);
119 /* assume compression must be successful except for small or random inputs */
120 assert_se(r == 0 || (size < 2048 && r == -ENOBUFS) || streq(type, "random"));
121
122 /* check for overwrites */
123 assert_se(buf[size] == 0);
124 if (r != 0) {
125 skipped += size;
126 continue;
127 }
128
129 assert_se(j > 0);
130 if (j >= size)
131 log_error("%s \"compressed\" %zu -> %zu", label, size, j);
132
133 r = decompress(buf, j, &buf2, &buf2_allocated, &k, 0);
134 assert_se(r == 0);
135 assert_se(buf2_allocated >= k);
136 assert_se(k == size);
137
138 assert_se(memcmp(text, buf2, size) == 0);
139
140 total += size;
141 compressed += j;
142
143 n2 = now(CLOCK_MONOTONIC);
144 if (n2 - n > arg_duration)
145 break;
146 }
147
148 dt = (n2-n) / 1e6;
149
150 log_info("%s/%s: compressed & decompressed %zu bytes in %.2fs (%.2fMiB/s), "
151 "mean compresion %.2f%%, skipped %zu bytes",
152 label, type, total, dt,
153 total / 1024. / 1024 / dt,
154 100 - compressed * 100. / total,
155 skipped);
156 }
157 #endif
158
159 int main(int argc, char *argv[]) {
160 #if HAVE_XZ || HAVE_LZ4
161 const char *i;
162 int r;
163
164 log_set_max_level(LOG_INFO);
165
166 if (argc >= 2) {
167 unsigned x;
168
169 assert_se(safe_atou(argv[1], &x) >= 0);
170 arg_duration = x * USEC_PER_SEC;
171 } else {
172 bool slow;
173
174 r = getenv_bool("SYSTEMD_SLOW_TESTS");
175 slow = r >= 0 ? r : SYSTEMD_SLOW_TESTS_DEFAULT;
176
177 arg_duration = slow ? 2 * USEC_PER_SEC : USEC_PER_SEC / 50;
178 }
179
180 if (argc == 3)
181 (void) safe_atozu(argv[2], &arg_start);
182 else
183 arg_start = getpid_cached();
184
185 NULSTR_FOREACH(i, "zeros\0simple\0random\0") {
186 #if HAVE_XZ
187 test_compress_decompress("XZ", i, compress_blob_xz, decompress_blob_xz);
188 #endif
189 #if HAVE_LZ4
190 test_compress_decompress("LZ4", i, compress_blob_lz4, decompress_blob_lz4);
191 #endif
192 }
193 return 0;
194 #else
195 return EXIT_TEST_SKIP;
196 #endif
197 }