]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/journal/test-compress-benchmark.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd
6 Copyright 2014 Zbigniew Jędrzejewski-Szmek
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.
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.
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/>.
24 #include "parse-util.h"
25 #include "random-util.h"
26 #include "string-util.h"
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
);
33 static usec_t arg_duration
= 2 * USEC_PER_SEC
;
34 static size_t arg_start
;
36 #define MAX_SIZE (1024*1024LU)
37 #define PRIME 1048571 /* A prime close enough to one megabyte that mod 4 == 3 */
39 static size_t _permute(size_t x
) {
45 residue
= x
*x
% PRIME
;
49 return PRIME
- residue
;
52 static size_t permute(size_t x
) {
53 return _permute((_permute(x
) + arg_start
) % MAX_SIZE
^ 0xFF345);
56 static char* make_buf(size_t count
, const char *type
) {
63 if (streq(type
, "zeros"))
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;
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
);
82 assert_not_reached("here");
87 static void test_compress_decompress(const char* label
, const char* type
,
88 compress_t compress
, decompress_t decompress
) {
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;
97 text
= make_buf(MAX_SIZE
, type
);
98 buf
= calloc(MAX_SIZE
+ 1, 1);
99 assert_se(text
&& buf
);
101 n
= now(CLOCK_MONOTONIC
);
103 for (size_t i
= 0; i
<= MAX_SIZE
; i
++) {
104 size_t j
= 0, k
= 0, size
;
109 log_debug("%s %zu %zu", type
, i
, size
);
111 memzero(buf
, MIN(size
+ 1000, MAX_SIZE
));
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"));
117 /* check for overwrites */
118 assert_se(buf
[size
] == 0);
126 log_error("%s \"compressed\" %zu -> %zu", label
, size
, j
);
128 r
= decompress(buf
, j
, &buf2
, &buf2_allocated
, &k
, 0);
130 assert_se(buf2_allocated
>= k
);
131 assert_se(k
== size
);
133 assert_se(memcmp(text
, buf2
, size
) == 0);
138 n2
= now(CLOCK_MONOTONIC
);
139 if (n2
- n
> arg_duration
)
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
,
153 int main(int argc
, char *argv
[]) {
156 log_set_max_level(LOG_INFO
);
161 assert_se(safe_atou(argv
[1], &x
) >= 0);
162 arg_duration
= x
* USEC_PER_SEC
;
165 (void) safe_atolu(argv
[2], &arg_start
);
167 arg_start
= getpid();
169 NULSTR_FOREACH(i
, "zeros\0simple\0random\0") {
171 test_compress_decompress("XZ", i
, compress_blob_xz
, decompress_blob_xz
);
174 test_compress_decompress("LZ4", i
, compress_blob_lz4
, decompress_blob_lz4
);