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