brpc_snappy_compress_unittest.cpp 8.81 KB
Newer Older
1
// brpc - A framework to host and access services throughout Baidu.
gejun's avatar
gejun committed
2
// Copyright (c) 2014 Baidu, Inc.
gejun's avatar
gejun committed
3 4 5 6

// Date: 2015/01/20 19:01:06

#include <gtest/gtest.h>
7
#include "butil/gperftools_profiler.h"
8 9 10 11
#include "butil/third_party/snappy/snappy.h"
#include "butil/macros.h"
#include "butil/iobuf.h"
#include "butil/time.h"
12
#include "snappy_message.pb.h"
gejun's avatar
gejun committed
13 14 15
#include "brpc/policy/snappy_compress.h"
#include "brpc/policy/gzip_compress.h"

16 17
typedef bool (*Compress)(const google::protobuf::Message&, butil::IOBuf*);
typedef bool (*Decompress)(const butil::IOBuf&, google::protobuf::Message*);
gejun's avatar
gejun committed
18 19 20 21

inline void CompressMessage(const char* method_name,
                            int num, snappy_message::SnappyMessageProto& msg, 
                            int len, Compress compress, Decompress decompress) {
22
    butil::Timer timer;
gejun's avatar
gejun committed
23 24 25 26 27
    size_t compression_length = 0;
    int64_t total_compress_time = 0;
    int64_t total_decompress_time = 0;
    snappy_message::SnappyMessageProto new_msg;
    for (int index = 0; index < num; index++) {
28
        butil::IOBuf buf;
gejun's avatar
gejun committed
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
        timer.start();
        ASSERT_TRUE(compress(msg, &buf));
        timer.stop();
        total_compress_time += timer.n_elapsed();
        compression_length += buf.length();
        timer.start();
        ASSERT_TRUE(decompress(buf, &new_msg));
        timer.stop();
        total_decompress_time += timer.n_elapsed();
    }
    float compression_ratio = compression_length / (((double)num) * len);
    printf("%20s%20d%20f%20f%30f%30f%29f%%\n", method_name, len, 
            total_compress_time/1000.0/num, total_decompress_time/1000.0/num, 
            1000000000.0/1024/1024*num*len/total_compress_time,
            1000000000.0/1024/1024*num*len/total_decompress_time,
            compression_ratio*100.0);
}

47
static bool SnappyDecompressIOBuf(char* input, size_t len, butil::IOBuf* buf) {
gejun's avatar
gejun committed
48
    size_t decompress_length;
49
    if (!butil::snappy::GetUncompressedLength(input, len, &decompress_length)) {
gejun's avatar
gejun committed
50 51 52
        return false;
    }
    char* output = new char[decompress_length];
53
    if (!butil::snappy::RawUncompress(input, len, output)) {
gejun's avatar
gejun committed
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
        delete [] output;
        return false;
    }
    buf->append(output, decompress_length);
    delete [] output;
    return true;
}

class test_compress_method : public testing::Test {};

TEST_F(test_compress_method, snappy) {
    snappy_message::SnappyMessageProto old_msg;
    old_msg.set_text("Hello World!");
    old_msg.add_numbers(2);
    old_msg.add_numbers(7);
    old_msg.add_numbers(45);
70
    butil::IOBuf buf;
gejun's avatar
gejun committed
71 72 73 74 75 76 77 78 79 80 81
    ASSERT_TRUE(brpc::policy::SnappyCompress(old_msg, &buf));
    snappy_message::SnappyMessageProto new_msg;
    ASSERT_TRUE(brpc::policy::SnappyDecompress(buf, &new_msg));
    ASSERT_TRUE(strcmp(new_msg.text().c_str(), "Hello World!") == 0);
    ASSERT_TRUE(new_msg.numbers_size() == 3);
    ASSERT_EQ(new_msg.numbers(0), 2);
    ASSERT_EQ(new_msg.numbers(1), 7);
    ASSERT_EQ(new_msg.numbers(2), 45);
}

TEST_F(test_compress_method, snappy_iobuf) {
82
    butil::IOBuf buf, output_buf, check_buf; 
gejun's avatar
gejun committed
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
    const char* test = "this is a test";
    buf.append(test, strlen(test));
    ASSERT_TRUE(brpc::policy::SnappyCompress(buf, &output_buf)); 
    ASSERT_TRUE(brpc::policy::SnappyDecompress(output_buf, &check_buf));
    ASSERT_STREQ(check_buf.to_string().c_str(), test);
}

TEST_F(test_compress_method, mass_snappy) {
    snappy_message::SnappyMessageProto old_msg;
    int len = 12435; 
    char* text = new char[len + 1];
    for (int j = 0; j < len;) {
        for (int i = 0; i < 26 && j < len; i++) {
            text[j++] = 'a' + i;
        }
        for (int i = 0; i < 10 && j < len; i++) {
            text[j++] = '0' + i;
        }
    }
    text[len] = '\0';
    old_msg.set_text(text);
    old_msg.add_numbers(2);
    old_msg.add_numbers(7);
    old_msg.add_numbers(45);
107
    butil::IOBuf buf;
gejun's avatar
gejun committed
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
    ProfilerStart("./snappy_compress.prof");
    ASSERT_TRUE(brpc::policy::SnappyCompress(old_msg, &buf));
    snappy_message::SnappyMessageProto new_msg;
    ASSERT_TRUE(brpc::policy::SnappyDecompress(buf, &new_msg));
    ProfilerStop();
    ASSERT_TRUE(strcmp(new_msg.text().c_str(), text) == 0);
    ASSERT_TRUE(new_msg.numbers_size() == 3);
    ASSERT_EQ(new_msg.numbers(0), 2);
    ASSERT_EQ(new_msg.numbers(1), 7);
    ASSERT_EQ(new_msg.numbers(2), 45);
    delete [] text;
}

TEST_F(test_compress_method, snappy_test) {
    int len = 200;
    char* text = new char[len + 1];
    for (int j = 0; j < len;) {
        for (int i = 0; i < 26 && j < len; i++) {
            text[j++] = 'a' + i;
        }
        for (int i = 0; i < 10 && j < len; i++) {
            text[j++] = '0' + i;
        }
    }
    text[len] = '\0';
133
    butil::IOBuf buf;
gejun's avatar
gejun committed
134 135
    std::string output;
    std::string append_string;
136
    ASSERT_TRUE(butil::snappy::Compress(text, len, &output));
gejun's avatar
gejun committed
137 138
    size_t com_len1 = output.size();
    const char* s_text = "123456";
139
    ASSERT_TRUE(butil::snappy::Compress(s_text, strlen(s_text), &append_string));
gejun's avatar
gejun committed
140 141 142 143
    output.append(append_string);
    std::string uncompress_str;
    std::string uncompress_str_t;
    char* ptr = const_cast<char*>(output.c_str());
144
    ASSERT_TRUE(butil::snappy::Uncompress(ptr, com_len1, &uncompress_str));
gejun's avatar
gejun committed
145
    ptr = const_cast<char*>(append_string.c_str());
146
    ASSERT_TRUE(butil::snappy::Uncompress(ptr, strlen(ptr), &uncompress_str_t));
gejun's avatar
gejun committed
147 148 149 150 151 152
    delete [] text;
}

TEST_F(test_compress_method, throughput_compare) {
    int len = 0;
    int len_subs[] = {128, 1024, 16*1024, 32*1024, 512*1024}; 
153
    butil::Timer timer;
gejun's avatar
gejun committed
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
    printf("%20s%20s%20s%20s%30s%30s%30s\n", "Compress method", "Compress size(B)", 
           "Compress time(us)", "Decompress time(us)", "Compress throughput(MB/s)", 
           "Decompress throughput(MB/s)", "Compress ratio");    
    for (size_t num = 0; num < ARRAY_SIZE(len_subs); ++num) {
        len = len_subs[num];
        snappy_message::SnappyMessageProto old_msg;
        char* text = new char[len + 1];
        for (int j = 0; j < len;) {
            for (int i = 0; i < 26 && j < len; i++) {
                text[j++] = 'a' + i;
            }
            for (int i = 0; i < 10 && j < len; i++) {
                text[j++] = '0' + i;
            }
        }
        text[len] = '\0';
        old_msg.set_text(text);
        int k = std::min(32*1024*1024/len, 5000);
        CompressMessage("Snappy", k, old_msg, len, 
                         brpc::policy::SnappyCompress, 
                         brpc::policy::SnappyDecompress);
        CompressMessage("Gzip", k, old_msg, len, 
                         brpc::policy::GzipCompress, 
                         brpc::policy::GzipDecompress);
        CompressMessage("Zlib", k, old_msg, len, 
                         brpc::policy::ZlibCompress, 
                         brpc::policy::ZlibDecompress);
        printf("\n");
        delete [] text;
    }
}

TEST_F(test_compress_method, throughput_compare_complete_random) {
    char str_table[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    int rand_num = 0;
    int len = 0;
    int len_subs[] = {128, 1024, 16*1024, 32*1024, 512 * 1024}; 
191
    butil::Timer timer;
gejun's avatar
gejun committed
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
    printf("%20s%20s%20s%20s%30s%30s%30s\n", "Compress method", "Compress size(B)", 
           "Compress time(us)", "Decompress time(us)", "Compress throughput(MB/s)", 
           "Decompress throughput(MB/s)", "Compress ratio");
    for (size_t num = 0; num < ARRAY_SIZE(len_subs); ++num) {
        len = len_subs[num];
        snappy_message::SnappyMessageProto old_msg;
        char* text = new char[len + 1];
        for (int j = 0; j < len;) {
            rand_num = rand()%62;
            text[j++] = str_table[rand_num];
        }
        text[len] = '\0';
        old_msg.set_text(text);
        int k = std::min(32*1024*1024/len, 5000);
        CompressMessage("Snappy", k, old_msg, len, 
                         brpc::policy::SnappyCompress, 
                         brpc::policy::SnappyDecompress);
        CompressMessage("Gzip", k, old_msg, len, 
                         brpc::policy::GzipCompress, 
                         brpc::policy::GzipDecompress);
        CompressMessage("Zlib", k, old_msg, len, 
                         brpc::policy::ZlibCompress, 
                         brpc::policy::ZlibDecompress);
        printf("\n");
        delete [] text;
    }
}

TEST_F(test_compress_method, mass_snappy_iobuf) {
221
    butil::IOBuf buf; 
gejun's avatar
gejun committed
222 223 224 225 226 227 228 229 230
    int len = 782;
    char* text = new char[len + 1];
    for (int j = 0; j < len;) {
        for (int i = 0; i < 26 && j < len; i++) {
            text[j++] = 'a' + i;
        }
    }
    text[len] = '\0';
    buf.append(text, strlen(text));
231
    butil::IOBuf output_buf, check_buf;
gejun's avatar
gejun committed
232 233 234 235 236 237 238 239
    ASSERT_TRUE(brpc::policy::SnappyCompress(buf, &output_buf)); 
    const std::string output_str = output_buf.to_string();
    len = output_str.size();
    ASSERT_TRUE(SnappyDecompressIOBuf(const_cast<char*>(output_str.data()), len, &check_buf));
    std::string check_str = check_buf.to_string();
    ASSERT_TRUE(strcmp(check_str.c_str(), text) == 0);
    delete [] text;
}