]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/journal/test-compress-benchmark.c
Add SPDX license identifiers to source files under the LGPL
[thirdparty/systemd.git] / src / journal / test-compress-benchmark.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 This file is part of systemd
4
5 Copyright 2014 Zbigniew Jędrzejewski-Szmek
6
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
11
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
19 ***/
20
21 #include "alloc-util.h"
22 #include "compress.h"
23 #include "env-util.h"
24 #include "macro.h"
25 #include "parse-util.h"
26 #include "random-util.h"
27 #include "string-util.h"
28 #include "util.h"
29
30 typedef int (compress_t)(const void *src, uint64_t src_size, void *dst,
31 size_t dst_alloc_size, size_t *dst_size);
32 typedef int (decompress_t)(const void *src, uint64_t src_size,
33 void **dst, size_t *dst_alloc_size, size_t* dst_size, size_t dst_max);
34
35 #if HAVE_XZ || HAVE_LZ4
36
37 static usec_t arg_duration;
38 static size_t arg_start;
39
40 #define MAX_SIZE (1024*1024LU)
41 #define PRIME 1048571 /* A prime close enough to one megabyte that mod 4 == 3 */
42
43 static size_t _permute(size_t x) {
44 size_t residue;
45
46 if (x >= PRIME)
47 return x;
48
49 residue = x*x % PRIME;
50 if (x <= PRIME / 2)
51 return residue;
52 else
53 return PRIME - residue;
54 }
55
56 static size_t permute(size_t x) {
57 return _permute((_permute(x) + arg_start) % MAX_SIZE ^ 0xFF345);
58 }
59
60 static char* make_buf(size_t count, const char *type) {
61 char *buf;
62 size_t i;
63
64 buf = malloc(count);
65 assert_se(buf);
66
67 if (streq(type, "zeros"))
68 memzero(buf, count);
69 else if (streq(type, "simple"))
70 for (i = 0; i < count; i++)
71 buf[i] = 'a' + i % ('z' - 'a' + 1);
72 else if (streq(type, "random")) {
73 size_t step = count / 10;
74
75 random_bytes(buf, step);
76 memzero(buf + 1*step, step);
77 random_bytes(buf + 2*step, step);
78 memzero(buf + 3*step, step);
79 random_bytes(buf + 4*step, step);
80 memzero(buf + 5*step, step);
81 random_bytes(buf + 6*step, step);
82 memzero(buf + 7*step, step);
83 random_bytes(buf + 8*step, step);
84 memzero(buf + 9*step, step);
85 } else
86 assert_not_reached("here");
87
88 return buf;
89 }
90
91 static void test_compress_decompress(const char* label, const char* type,
92 compress_t compress, decompress_t decompress) {
93 usec_t n, n2 = 0;
94 float dt;
95
96 _cleanup_free_ char *text, *buf;
97 _cleanup_free_ void *buf2 = NULL;
98 size_t buf2_allocated = 0;
99 size_t skipped = 0, compressed = 0, total = 0;
100
101 text = make_buf(MAX_SIZE, type);
102 buf = calloc(MAX_SIZE + 1, 1);
103 assert_se(text && buf);
104
105 n = now(CLOCK_MONOTONIC);
106
107 for (size_t i = 0; i <= MAX_SIZE; i++) {
108 size_t j = 0, k = 0, size;
109 int r;
110
111 size = permute(i);
112 if (size == 0)
113 continue;
114
115 log_debug("%s %zu %zu", type, i, size);
116
117 memzero(buf, MIN(size + 1000, MAX_SIZE));
118
119 r = compress(text, size, buf, size, &j);
120 /* assume compression must be successful except for small or random inputs */
121 assert_se(r == 0 || (size < 2048 && r == -ENOBUFS) || streq(type, "random"));
122
123 /* check for overwrites */
124 assert_se(buf[size] == 0);
125 if (r != 0) {
126 skipped += size;
127 continue;
128 }
129
130 assert_se(j > 0);
131 if (j >= size)
132 log_error("%s \"compressed\" %zu -> %zu", label, size, j);
133
134 r = decompress(buf, j, &buf2, &buf2_allocated, &k, 0);
135 assert_se(r == 0);
136 assert_se(buf2_allocated >= k);
137 assert_se(k == size);
138
139 assert_se(memcmp(text, buf2, size) == 0);
140
141 total += size;
142 compressed += j;
143
144 n2 = now(CLOCK_MONOTONIC);
145 if (n2 - n > arg_duration)
146 break;
147 }
148
149 dt = (n2-n) / 1e6;
150
151 log_info("%s/%s: compressed & decompressed %zu bytes in %.2fs (%.2fMiB/s), "
152 "mean compresion %.2f%%, skipped %zu bytes",
153 label, type, total, dt,
154 total / 1024. / 1024 / dt,
155 100 - compressed * 100. / total,
156 skipped);
157 }
158 #endif
159
160 int main(int argc, char *argv[]) {
161 #if HAVE_XZ || HAVE_LZ4
162 const char *i;
163 int r;
164
165 log_set_max_level(LOG_INFO);
166
167 if (argc >= 2) {
168 unsigned x;
169
170 assert_se(safe_atou(argv[1], &x) >= 0);
171 arg_duration = x * USEC_PER_SEC;
172 } else {
173 bool slow;
174
175 r = getenv_bool("SYSTEMD_SLOW_TESTS");
176 slow = r >= 0 ? r : SYSTEMD_SLOW_TESTS_DEFAULT;
177
178 arg_duration = slow ? 2 * USEC_PER_SEC : USEC_PER_SEC / 50;
179 }
180
181 if (argc == 3)
182 (void) safe_atozu(argv[2], &arg_start);
183 else
184 arg_start = getpid_cached();
185
186 NULSTR_FOREACH(i, "zeros\0simple\0random\0") {
187 #if HAVE_XZ
188 test_compress_decompress("XZ", i, compress_blob_xz, decompress_blob_xz);
189 #endif
190 #if HAVE_LZ4
191 test_compress_decompress("LZ4", i, compress_blob_lz4, decompress_blob_lz4);
192 #endif
193 }
194 return 0;
195 #else
196 return EXIT_TEST_SKIP;
197 #endif
198 }