From ac8e56f050d11bc83db896ba937bc10a86106ff6 Mon Sep 17 00:00:00 2001
From: sdong <siying.d@fb.com>
Date: Mon, 7 Dec 2015 17:12:43 -0800
Subject: [PATCH] db_bench: in uncompress benchmark, get Snappy size from
 compressed stream

Summary: Now in benchmark "uncompress" in db_bench, we get size from compressed stream for all other compression types except Snappy, where we allocate memory based on parameter. Change it to match to behavior of other compression types.

Test Plan: Run ./db_bench --benchmarks=uncompress with snappy and other compression types.

Reviewers: yhchiang, kradhakrishnan, anthony, IslamAbdelRahman, igor

Reviewed By: igor

Subscribers: leveldb, dhruba

Differential Revision: https://reviews.facebook.net/D51681
---
 db/db_bench.cc | 19 +++++++++++++------
 1 file changed, 13 insertions(+), 6 deletions(-)

diff --git a/db/db_bench.cc b/db/db_bench.cc
index afbafaeb4..de0bf9271 100644
--- a/db/db_bench.cc
+++ b/db/db_bench.cc
@@ -2267,12 +2267,19 @@ class Benchmark {
     while (ok && bytes < 1024 * 1048576) {
       char *uncompressed = nullptr;
       switch (FLAGS_compression_type_e) {
-      case rocksdb::kSnappyCompression:
-        // allocate here to make comparison fair
-        uncompressed = new char[input.size()];
-        ok = Snappy_Uncompress(compressed.data(), compressed.size(),
-                               uncompressed);
-        break;
+        case rocksdb::kSnappyCompression: {
+          // get size and allocate here to make comparison fair
+          size_t ulength = 0;
+          if (!Snappy_GetUncompressedLength(compressed.data(),
+                                            compressed.size(), &ulength)) {
+            ok = false;
+            break;
+          }
+          uncompressed = new char[ulength];
+          ok = Snappy_Uncompress(compressed.data(), compressed.size(),
+                                 uncompressed);
+          break;
+        }
       case rocksdb::kZlibCompression:
         uncompressed = Zlib_Uncompress(compressed.data(), compressed.size(),
                                        &decompress_size, 2);