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 zlib, lzma, quicklz, and snappy.
40 // Compare to compress-test which tests the toku compression (which is a composite of quicklz and zlib).
41 
42 #include <sys/time.h>
43 #include "test.h"
44 #include "serialize/compress.h"
45 
tdiff(struct timeval * start,struct timeval * end)46 static float tdiff (struct timeval *start, struct timeval *end) {
47     return (end->tv_sec-start->tv_sec) + 1e-6*(end->tv_usec - start->tv_usec);
48 }
49 
test_compress_buf_method(unsigned char * buf,int i,enum toku_compression_method m)50 static uLongf test_compress_buf_method (unsigned char *buf, int i, enum toku_compression_method m) {
51     int bound = toku_compress_bound(m, i);
52     unsigned char *MALLOC_N(bound, cb);
53     uLongf actual_clen = bound;
54     toku_compress(m, cb, &actual_clen, buf, i);
55     unsigned char *MALLOC_N(i, ubuf);
56     toku_decompress(ubuf, i, cb, actual_clen);
57     assert(0==memcmp(ubuf, buf, i));
58     toku_free(ubuf);
59     toku_free(cb);
60     return actual_clen;
61 }
62 
test_compress_i(int i,enum toku_compression_method m,uLongf * compress_size,uLongf * uncompress_size)63 static void test_compress_i (int i, enum toku_compression_method m, uLongf *compress_size, uLongf *uncompress_size) {
64     unsigned char *MALLOC_N(i, b);
65     for (int j=0; j<i; j++) b[j] = random()%256;
66     *compress_size += test_compress_buf_method (b, i, m);
67     *uncompress_size += i;
68 
69     for (int j=0; j<i; j++) b[j] = 0;
70     *compress_size += test_compress_buf_method (b, i, m);
71     *uncompress_size += i;
72 
73     for (int j=0; j<i; j++) b[j] = 0xFF;
74     *compress_size += test_compress_buf_method(b, i, m);
75     *uncompress_size += i;
76 
77     toku_free(b);
78 }
79 
test_compress(enum toku_compression_method m,uLongf * compress_size,uLongf * uncompress_size)80 static void test_compress (enum toku_compression_method m, uLongf *compress_size, uLongf *uncompress_size) {
81     // unlike quicklz, we can handle length 0.
82     for (int i=0; i<100; i++) {
83         test_compress_i(i, m, compress_size, uncompress_size);
84     }
85     test_compress_i(1024, m, compress_size, uncompress_size);
86     test_compress_i(1024*1024*4, m, compress_size, uncompress_size);
87     test_compress_i(1024*1024*4 - 123, m, compress_size, uncompress_size); // just some random lengths
88 }
89 
test_compress_methods()90 static void test_compress_methods () {
91     struct timeval start, end;
92     uLongf compress_size = 0;
93     uLongf uncompress_size = 0;
94 
95     gettimeofday(&start, NULL);
96     test_compress(TOKU_ZLIB_METHOD, &compress_size, &uncompress_size);
97     gettimeofday(&end, NULL);
98     printf("TOKU_ZLIB_METHOD Time=%.6fs , Ratio=%.2f[%d/%d]\n",
99             tdiff(&start, &end),
100             (float)compress_size / (float)uncompress_size, (int)compress_size, (int)uncompress_size);
101 
102     compress_size = 0;
103     uncompress_size = 0;
104     gettimeofday(&start, NULL);
105     test_compress(TOKU_ZLIB_WITHOUT_CHECKSUM_METHOD, &compress_size, &uncompress_size);
106     gettimeofday(&end, NULL);
107     printf("TOKU_ZLIB_WITHOUT_CHECKSUM_METHOD Time=%.6fs, Ratio=%.2f[%d/%d]\n",
108             tdiff(&start, &end),
109             (float)compress_size / (float)uncompress_size, (int)compress_size, (int)uncompress_size);
110 
111     compress_size = 0;
112     uncompress_size = 0;
113     gettimeofday(&start, NULL);
114     test_compress(TOKU_QUICKLZ_METHOD, &compress_size, &uncompress_size);
115     gettimeofday(&end, NULL);
116     printf("TOKU_QUICKLZ_METHOD Time=%.6fs, Ratio=%.2f[%d/%d]\n",
117             tdiff(&start, &end),
118             (float)compress_size / (float)uncompress_size, (int)compress_size, (int)uncompress_size);
119 
120     compress_size = 0;
121     uncompress_size = 0;
122     gettimeofday(&start, NULL);
123     test_compress(TOKU_LZMA_METHOD, &compress_size, &uncompress_size);
124     gettimeofday(&end, NULL);
125     printf("TOKU_LZMA_METHOD Time=%.6fs, Ratio=%.2f[%d/%d]\n",
126             tdiff(&start, &end),
127             (float)compress_size / (float)uncompress_size, (int)compress_size, (int)uncompress_size);
128 
129     compress_size = 0;
130     uncompress_size = 0;
131     gettimeofday(&start, NULL);
132     test_compress(TOKU_SNAPPY_METHOD, &compress_size, &uncompress_size);
133     gettimeofday(&end, NULL);
134     printf("TOKU_SNAPPY_METHOD Time=%.6fs, Ratio=%.2f[%d/%d]\n",
135             tdiff(&start, &end),
136             (float)compress_size / (float)uncompress_size, (int)compress_size, (int)uncompress_size);
137 }
138 
test_main(int argc,const char * argv[])139 int test_main (int argc, const char *argv[]) {
140     default_parse_args(argc, argv);
141 
142     test_compress_methods();
143 
144     return 0;
145 }
146