brpc_snappy_compress_unittest.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. // Licensed to the Apache Software Foundation (ASF) under one
  2. // or more contributor license agreements. See the NOTICE file
  3. // distributed with this work for additional information
  4. // regarding copyright ownership. The ASF licenses this file
  5. // to you under the Apache License, Version 2.0 (the
  6. // "License"); you may not use this file except in compliance
  7. // with the License. You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing,
  12. // software distributed under the License is distributed on an
  13. // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. // KIND, either express or implied. See the License for the
  15. // specific language governing permissions and limitations
  16. // under the License.
  17. // brpc - A framework to host and access services throughout Baidu.
  18. // Date: 2015/01/20 19:01:06
  19. #include <gtest/gtest.h>
  20. #include "butil/gperftools_profiler.h"
  21. #include "butil/third_party/snappy/snappy.h"
  22. #include "butil/macros.h"
  23. #include "butil/iobuf.h"
  24. #include "butil/time.h"
  25. #include "snappy_message.pb.h"
  26. #include "brpc/policy/snappy_compress.h"
  27. #include "brpc/policy/gzip_compress.h"
  28. typedef bool (*Compress)(const google::protobuf::Message&, butil::IOBuf*);
  29. typedef bool (*Decompress)(const butil::IOBuf&, google::protobuf::Message*);
  30. inline void CompressMessage(const char* method_name,
  31. int num, snappy_message::SnappyMessageProto& msg,
  32. int len, Compress compress, Decompress decompress) {
  33. butil::Timer timer;
  34. size_t compression_length = 0;
  35. int64_t total_compress_time = 0;
  36. int64_t total_decompress_time = 0;
  37. snappy_message::SnappyMessageProto new_msg;
  38. for (int index = 0; index < num; index++) {
  39. butil::IOBuf buf;
  40. timer.start();
  41. ASSERT_TRUE(compress(msg, &buf));
  42. timer.stop();
  43. total_compress_time += timer.n_elapsed();
  44. compression_length += buf.length();
  45. timer.start();
  46. ASSERT_TRUE(decompress(buf, &new_msg));
  47. timer.stop();
  48. total_decompress_time += timer.n_elapsed();
  49. }
  50. float compression_ratio = compression_length / (((double)num) * len);
  51. printf("%20s%20d%20f%20f%30f%30f%29f%%\n", method_name, len,
  52. total_compress_time/1000.0/num, total_decompress_time/1000.0/num,
  53. 1000000000.0/1024/1024*num*len/total_compress_time,
  54. 1000000000.0/1024/1024*num*len/total_decompress_time,
  55. compression_ratio*100.0);
  56. }
  57. static bool SnappyDecompressIOBuf(char* input, size_t len, butil::IOBuf* buf) {
  58. size_t decompress_length;
  59. if (!butil::snappy::GetUncompressedLength(input, len, &decompress_length)) {
  60. return false;
  61. }
  62. char* output = new char[decompress_length];
  63. if (!butil::snappy::RawUncompress(input, len, output)) {
  64. delete [] output;
  65. return false;
  66. }
  67. buf->append(output, decompress_length);
  68. delete [] output;
  69. return true;
  70. }
  71. class test_compress_method : public testing::Test {};
  72. TEST_F(test_compress_method, snappy) {
  73. snappy_message::SnappyMessageProto old_msg;
  74. old_msg.set_text("Hello World!");
  75. old_msg.add_numbers(2);
  76. old_msg.add_numbers(7);
  77. old_msg.add_numbers(45);
  78. butil::IOBuf buf;
  79. ASSERT_TRUE(brpc::policy::SnappyCompress(old_msg, &buf));
  80. snappy_message::SnappyMessageProto new_msg;
  81. ASSERT_TRUE(brpc::policy::SnappyDecompress(buf, &new_msg));
  82. ASSERT_TRUE(strcmp(new_msg.text().c_str(), "Hello World!") == 0);
  83. ASSERT_TRUE(new_msg.numbers_size() == 3);
  84. ASSERT_EQ(new_msg.numbers(0), 2);
  85. ASSERT_EQ(new_msg.numbers(1), 7);
  86. ASSERT_EQ(new_msg.numbers(2), 45);
  87. }
  88. TEST_F(test_compress_method, snappy_iobuf) {
  89. butil::IOBuf buf, output_buf, check_buf;
  90. const char* test = "this is a test";
  91. buf.append(test, strlen(test));
  92. ASSERT_TRUE(brpc::policy::SnappyCompress(buf, &output_buf));
  93. ASSERT_TRUE(brpc::policy::SnappyDecompress(output_buf, &check_buf));
  94. ASSERT_STREQ(check_buf.to_string().c_str(), test);
  95. }
  96. TEST_F(test_compress_method, mass_snappy) {
  97. snappy_message::SnappyMessageProto old_msg;
  98. int len = 12435;
  99. char* text = new char[len + 1];
  100. for (int j = 0; j < len;) {
  101. for (int i = 0; i < 26 && j < len; i++) {
  102. text[j++] = 'a' + i;
  103. }
  104. for (int i = 0; i < 10 && j < len; i++) {
  105. text[j++] = '0' + i;
  106. }
  107. }
  108. text[len] = '\0';
  109. old_msg.set_text(text);
  110. old_msg.add_numbers(2);
  111. old_msg.add_numbers(7);
  112. old_msg.add_numbers(45);
  113. butil::IOBuf buf;
  114. ProfilerStart("./snappy_compress.prof");
  115. ASSERT_TRUE(brpc::policy::SnappyCompress(old_msg, &buf));
  116. snappy_message::SnappyMessageProto new_msg;
  117. ASSERT_TRUE(brpc::policy::SnappyDecompress(buf, &new_msg));
  118. ProfilerStop();
  119. ASSERT_TRUE(strcmp(new_msg.text().c_str(), text) == 0);
  120. ASSERT_TRUE(new_msg.numbers_size() == 3);
  121. ASSERT_EQ(new_msg.numbers(0), 2);
  122. ASSERT_EQ(new_msg.numbers(1), 7);
  123. ASSERT_EQ(new_msg.numbers(2), 45);
  124. delete [] text;
  125. }
  126. TEST_F(test_compress_method, snappy_test) {
  127. int len = 200;
  128. char* text = new char[len + 1];
  129. for (int j = 0; j < len;) {
  130. for (int i = 0; i < 26 && j < len; i++) {
  131. text[j++] = 'a' + i;
  132. }
  133. for (int i = 0; i < 10 && j < len; i++) {
  134. text[j++] = '0' + i;
  135. }
  136. }
  137. text[len] = '\0';
  138. butil::IOBuf buf;
  139. std::string output;
  140. std::string append_string;
  141. ASSERT_TRUE(butil::snappy::Compress(text, len, &output));
  142. size_t com_len1 = output.size();
  143. const char* s_text = "123456";
  144. ASSERT_TRUE(butil::snappy::Compress(s_text, strlen(s_text), &append_string));
  145. output.append(append_string);
  146. std::string uncompress_str;
  147. std::string uncompress_str_t;
  148. char* ptr = const_cast<char*>(output.c_str());
  149. ASSERT_TRUE(butil::snappy::Uncompress(ptr, com_len1, &uncompress_str));
  150. ptr = const_cast<char*>(append_string.c_str());
  151. ASSERT_TRUE(butil::snappy::Uncompress(ptr, strlen(ptr), &uncompress_str_t));
  152. delete [] text;
  153. }
  154. TEST_F(test_compress_method, throughput_compare) {
  155. int len = 0;
  156. int len_subs[] = {128, 1024, 16*1024, 32*1024, 512*1024};
  157. butil::Timer timer;
  158. printf("%20s%20s%20s%20s%30s%30s%30s\n", "Compress method", "Compress size(B)",
  159. "Compress time(us)", "Decompress time(us)", "Compress throughput(MB/s)",
  160. "Decompress throughput(MB/s)", "Compress ratio");
  161. for (size_t num = 0; num < ARRAY_SIZE(len_subs); ++num) {
  162. len = len_subs[num];
  163. snappy_message::SnappyMessageProto old_msg;
  164. char* text = new char[len + 1];
  165. for (int j = 0; j < len;) {
  166. for (int i = 0; i < 26 && j < len; i++) {
  167. text[j++] = 'a' + i;
  168. }
  169. for (int i = 0; i < 10 && j < len; i++) {
  170. text[j++] = '0' + i;
  171. }
  172. }
  173. text[len] = '\0';
  174. old_msg.set_text(text);
  175. int k = std::min(32*1024*1024/len, 5000);
  176. CompressMessage("Snappy", k, old_msg, len,
  177. brpc::policy::SnappyCompress,
  178. brpc::policy::SnappyDecompress);
  179. CompressMessage("Gzip", k, old_msg, len,
  180. brpc::policy::GzipCompress,
  181. brpc::policy::GzipDecompress);
  182. CompressMessage("Zlib", k, old_msg, len,
  183. brpc::policy::ZlibCompress,
  184. brpc::policy::ZlibDecompress);
  185. printf("\n");
  186. delete [] text;
  187. }
  188. }
  189. TEST_F(test_compress_method, throughput_compare_complete_random) {
  190. char str_table[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  191. int rand_num = 0;
  192. int len = 0;
  193. int len_subs[] = {128, 1024, 16*1024, 32*1024, 512 * 1024};
  194. butil::Timer timer;
  195. printf("%20s%20s%20s%20s%30s%30s%30s\n", "Compress method", "Compress size(B)",
  196. "Compress time(us)", "Decompress time(us)", "Compress throughput(MB/s)",
  197. "Decompress throughput(MB/s)", "Compress ratio");
  198. for (size_t num = 0; num < ARRAY_SIZE(len_subs); ++num) {
  199. len = len_subs[num];
  200. snappy_message::SnappyMessageProto old_msg;
  201. char* text = new char[len + 1];
  202. for (int j = 0; j < len;) {
  203. rand_num = rand()%62;
  204. text[j++] = str_table[rand_num];
  205. }
  206. text[len] = '\0';
  207. old_msg.set_text(text);
  208. int k = std::min(32*1024*1024/len, 5000);
  209. CompressMessage("Snappy", k, old_msg, len,
  210. brpc::policy::SnappyCompress,
  211. brpc::policy::SnappyDecompress);
  212. CompressMessage("Gzip", k, old_msg, len,
  213. brpc::policy::GzipCompress,
  214. brpc::policy::GzipDecompress);
  215. CompressMessage("Zlib", k, old_msg, len,
  216. brpc::policy::ZlibCompress,
  217. brpc::policy::ZlibDecompress);
  218. printf("\n");
  219. delete [] text;
  220. }
  221. }
  222. TEST_F(test_compress_method, mass_snappy_iobuf) {
  223. butil::IOBuf buf;
  224. int len = 782;
  225. char* text = new char[len + 1];
  226. for (int j = 0; j < len;) {
  227. for (int i = 0; i < 26 && j < len; i++) {
  228. text[j++] = 'a' + i;
  229. }
  230. }
  231. text[len] = '\0';
  232. buf.append(text, strlen(text));
  233. butil::IOBuf output_buf, check_buf;
  234. ASSERT_TRUE(brpc::policy::SnappyCompress(buf, &output_buf));
  235. const std::string output_str = output_buf.to_string();
  236. len = output_str.size();
  237. ASSERT_TRUE(SnappyDecompressIOBuf(const_cast<char*>(output_str.data()), len, &check_buf));
  238. std::string check_str = check_buf.to_string();
  239. ASSERT_TRUE(strcmp(check_str.c_str(), text) == 0);
  240. delete [] text;
  241. }