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