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