1 /* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 // vim: ft=cpp:expandtab:ts=8:sw=4:softtabstop=4:
3 #ident "$Id$"
4 /*======
5 This file is part of PerconaFT.
6 
7 
8 Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved.
9 
10     PerconaFT is free software: you can redistribute it and/or modify
11     it under the terms of the GNU General Public License, version 2,
12     as published by the Free Software Foundation.
13 
14     PerconaFT is distributed in the hope that it will be useful,
15     but WITHOUT ANY WARRANTY; without even the implied warranty of
16     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17     GNU General Public License for more details.
18 
19     You should have received a copy of the GNU General Public License
20     along with PerconaFT.  If not, see <http://www.gnu.org/licenses/>.
21 
22 ----------------------------------------
23 
24     PerconaFT is free software: you can redistribute it and/or modify
25     it under the terms of the GNU Affero General Public License, version 3,
26     as published by the Free Software Foundation.
27 
28     PerconaFT is distributed in the hope that it will be useful,
29     but WITHOUT ANY WARRANTY; without even the implied warranty of
30     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31     GNU Affero General Public License for more details.
32 
33     You should have received a copy of the GNU Affero General Public License
34     along with PerconaFT.  If not, see <http://www.gnu.org/licenses/>.
35 ======= */
36 
37 #ident "Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved."
38 
39 // test sub block compression and decompression
40 
41 #include <toku_portability.h>
42 #include "test.h"
43 #include <stdio.h>
44 #include <errno.h>
45 #include <string.h>
46 
47 #include "serialize/sub_block.h"
48 
49 static void
test_sub_block_compression(void * buf,int total_size,int my_max_sub_blocks,int n_cores,enum toku_compression_method method)50 test_sub_block_compression(void *buf, int total_size, int my_max_sub_blocks, int n_cores, enum toku_compression_method method) {
51     if (verbose)
52         printf("%s:%d %d %d\n", __FUNCTION__, __LINE__, total_size, my_max_sub_blocks);
53 
54     int r;
55 
56     int sub_block_size, n_sub_blocks;
57     r = choose_sub_block_size(total_size, my_max_sub_blocks, &sub_block_size, &n_sub_blocks);
58     assert(r == 0);
59     if (verbose)
60         printf("%s:%d %d %d\n", __FUNCTION__, __LINE__, sub_block_size, n_sub_blocks);
61 
62     struct sub_block sub_blocks[n_sub_blocks];
63     set_all_sub_block_sizes(total_size, sub_block_size, n_sub_blocks, sub_blocks);
64 
65     size_t cbuf_size_bound = get_sum_compressed_size_bound(n_sub_blocks, sub_blocks, method);
66     void *cbuf = toku_malloc(cbuf_size_bound);
67     assert(cbuf);
68 
69     size_t cbuf_size = compress_all_sub_blocks(n_sub_blocks, sub_blocks, (char*)buf, (char*)cbuf, n_cores, NULL, method);
70     assert(cbuf_size <= cbuf_size_bound);
71 
72     void *ubuf = toku_malloc(total_size);
73     assert(ubuf);
74 
75     r = decompress_all_sub_blocks(n_sub_blocks, sub_blocks, (unsigned char*)cbuf, (unsigned char*)ubuf, n_cores, NULL);
76     assert(r == 0);
77 
78     assert(memcmp(buf, ubuf, total_size) == 0);
79 
80     toku_free(ubuf);
81     toku_free(cbuf);
82 }
83 
84 static void
set_random(void * buf,int total_size)85 set_random(void *buf, int total_size) {
86     char *bp = (char *) buf;
87     for (int i = 0; i < total_size; i++)
88         bp[i] = random();
89 }
90 
91 static void
run_test(int total_size,int n_cores,enum toku_compression_method method)92 run_test(int total_size, int n_cores, enum toku_compression_method method) {
93     void *buf = toku_malloc(total_size);
94     assert(buf);
95 
96     for (int my_max_sub_blocks = 1; my_max_sub_blocks <= max_sub_blocks; my_max_sub_blocks++) {
97         memset(buf, 0, total_size);
98         test_sub_block_compression(buf, total_size, my_max_sub_blocks, n_cores, method);
99 
100         set_random(buf, total_size);
101         test_sub_block_compression(buf, total_size, my_max_sub_blocks, n_cores, method);
102     }
103 
104     toku_free(buf);
105 }
106 int
test_main(int argc,const char * argv[])107 test_main (int argc, const char *argv[]) {
108     int n_cores = 1;
109     int e = 1;
110 
111     for (int i = 1; i < argc; i++) {
112         const char *arg = argv[i];
113         if (strcmp(arg, "-v") == 0 || strcmp(arg, "--verbose") == 0) {
114             verbose++;
115             continue;
116         }
117         if (strcmp(arg, "-n") == 0) {
118             if (i+1 < argc) {
119                 n_cores = atoi(argv[++i]);
120                 continue;
121             }
122         }
123         if (strcmp(arg, "-e") == 0) {
124             if (i+1 < argc) {
125                 e = atoi(argv[++i]);
126                 continue;
127             }
128         }
129     }
130 
131     for (int total_size = 256*1024; total_size <= 4*1024*1024; total_size *= 2) {
132         for (int size = total_size - e; size <= total_size + e; size++) {
133             run_test(size, n_cores, TOKU_NO_COMPRESSION);
134             run_test(size, n_cores, TOKU_ZLIB_METHOD);
135             run_test(size, n_cores, TOKU_ZLIB_WITHOUT_CHECKSUM_METHOD);
136             run_test(size, n_cores, TOKU_QUICKLZ_METHOD);
137             run_test(size, n_cores, TOKU_LZMA_METHOD);
138         }
139     }
140 
141     return 0;
142 }
143