chiark / gitweb /
journald: fix some xsprrintf() buffer size fallout
[elogind.git] / src / journal / test-compress-benchmark.c
1 /***
2   This file is part of systemd
3
4   Copyright 2014 Zbigniew JÄ™drzejewski-Szmek
5
6   systemd is free software; you can redistribute it and/or modify it
7   under the terms of the GNU Lesser General Public License as published by
8   the Free Software Foundation; either version 2.1 of the License, or
9   (at your option) any later version.
10
11   systemd is distributed in the hope that it will be useful, but
12   WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14   Lesser General Public License for more details.
15
16   You should have received a copy of the GNU Lesser General Public License
17   along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #include "compress.h"
21 #include "util.h"
22 #include "macro.h"
23
24 typedef int (compress_t)(const void *src, uint64_t src_size, void *dst, size_t *dst_size);
25 typedef int (decompress_t)(const void *src, uint64_t src_size,
26                            void **dst, size_t *dst_alloc_size, size_t* dst_size, size_t dst_max);
27
28 #define MAX_SIZE (1024*1024LU)
29
30 static char* make_buf(size_t count) {
31         char *buf;
32         size_t i;
33
34         buf = malloc(count);
35         assert_se(buf);
36
37         for (i = 0; i < count; i++)
38                 buf[i] = 'a' + i % ('z' - 'a' + 1);
39
40         return buf;
41 }
42
43 static void test_compress_decompress(const char* label,
44                                      compress_t compress, decompress_t decompress) {
45         usec_t n, n2 = 0;
46         float dt;
47
48         _cleanup_free_ char *text, *buf;
49         _cleanup_free_ void *buf2 = NULL;
50         size_t buf2_allocated = 0;
51         size_t skipped = 0, compressed = 0, total = 0;
52
53         text = make_buf(MAX_SIZE);
54         buf = calloc(MAX_SIZE + 1, 1);
55         assert_se(text && buf);
56
57         n = now(CLOCK_MONOTONIC);
58
59         for (size_t i = 1; i <= MAX_SIZE; i += (i < 2048 ? 1 : 217)) {
60                 size_t j = 0, k = 0;
61                 int r;
62
63                 r = compress(text, i, buf, &j);
64                 /* assume compression must be successful except for small inputs */
65                 assert_se(r == 0 || (i < 2048 && r == -ENOBUFS));
66                 /* check for overwrites */
67                 assert_se(buf[i] == 0);
68                 if (r != 0) {
69                         skipped += i;
70                         continue;
71                 }
72
73                 assert_se(j > 0);
74                 if (j >= i)
75                         log_error("%s \"compressed\" %zu -> %zu", label, i, j);
76
77                 r = decompress(buf, j, &buf2, &buf2_allocated, &k, 0);
78                 assert_se(r == 0);
79                 assert_se(buf2_allocated >= k);
80                 assert_se(k == i);
81
82                 assert_se(memcmp(text, buf2, i) == 0);
83
84                 total += i;
85                 compressed += j;
86
87                 n2 = now(CLOCK_MONOTONIC);
88                 if (n2 - n > 60 * USEC_PER_SEC)
89                         break;
90         }
91
92         dt = (n2-n) / 1e6;
93
94         log_info("%s: compressed & decompressed %zu bytes in %.2fs (%.2fMiB/s), "
95                  "mean compresion %.2f%%, skipped %zu bytes",
96                  label, total, dt,
97                  total / 1024. / 1024 / dt,
98                  100 - compressed * 100. / total,
99                  skipped);
100 }
101
102 int main(int argc, char *argv[]) {
103
104         log_set_max_level(LOG_DEBUG);
105
106 #ifdef HAVE_XZ
107         test_compress_decompress("XZ", compress_blob_xz, decompress_blob_xz);
108 #endif
109 #ifdef HAVE_LZ4
110         test_compress_decompress("LZ4", compress_blob_lz4, decompress_blob_lz4);
111 #endif
112         return 0;
113 }