2 * Copyright 2015 Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include <folly/io/Compression.h>
21 #include <unordered_map>
23 #include <boost/noncopyable.hpp>
24 #include <glog/logging.h>
25 #include <gtest/gtest.h>
27 #include <folly/Benchmark.h>
28 #include <folly/Hash.h>
29 #include <folly/Random.h>
30 #include <folly/Varint.h>
31 #include <folly/io/IOBufQueue.h>
33 namespace folly { namespace io { namespace test {
35 class DataHolder : private boost::noncopyable {
37 uint64_t hash(size_t size) const;
38 ByteRange data(size_t size) const;
41 explicit DataHolder(size_t sizeLog2);
43 std::unique_ptr<uint8_t[]> data_;
44 mutable std::unordered_map<uint64_t, uint64_t> hashCache_;
47 DataHolder::DataHolder(size_t sizeLog2)
48 : size_(size_t(1) << sizeLog2),
49 data_(new uint8_t[size_]) {
52 uint64_t DataHolder::hash(size_t size) const {
53 CHECK_LE(size, size_);
54 auto p = hashCache_.find(size);
55 if (p != hashCache_.end()) {
59 uint64_t h = folly::hash::fnv64_buf(data_.get(), size);
64 ByteRange DataHolder::data(size_t size) const {
65 CHECK_LE(size, size_);
66 return ByteRange(data_.get(), size);
69 uint64_t hashIOBuf(const IOBuf* buf) {
70 uint64_t h = folly::hash::FNV_64_HASH_START;
71 for (auto& range : *buf) {
72 h = folly::hash::fnv64_buf(range.data(), range.size(), h);
77 class RandomDataHolder : public DataHolder {
79 explicit RandomDataHolder(size_t sizeLog2);
82 RandomDataHolder::RandomDataHolder(size_t sizeLog2)
83 : DataHolder(sizeLog2) {
84 constexpr size_t numThreadsLog2 = 3;
85 constexpr size_t numThreads = size_t(1) << numThreadsLog2;
87 uint32_t seed = randomNumberSeed();
89 std::vector<std::thread> threads;
90 threads.reserve(numThreads);
91 for (size_t t = 0; t < numThreads; ++t) {
93 [this, seed, t, numThreadsLog2, sizeLog2] () {
94 std::mt19937 rng(seed + t);
95 size_t countLog2 = size_t(1) << (sizeLog2 - numThreadsLog2);
96 size_t start = size_t(t) << countLog2;
97 for (size_t i = 0; i < countLog2; ++i) {
98 this->data_[start + i] = rng();
103 for (auto& t : threads) {
108 class ConstantDataHolder : public DataHolder {
110 explicit ConstantDataHolder(size_t sizeLog2);
113 ConstantDataHolder::ConstantDataHolder(size_t sizeLog2)
114 : DataHolder(sizeLog2) {
115 memset(data_.get(), 'a', size_);
118 constexpr size_t dataSizeLog2 = 27; // 128MiB
119 RandomDataHolder randomDataHolder(dataSizeLog2);
120 ConstantDataHolder constantDataHolder(dataSizeLog2);
122 TEST(CompressionTestNeedsUncompressedLength, Simple) {
123 EXPECT_FALSE(getCodec(CodecType::NO_COMPRESSION)->needsUncompressedLength());
124 EXPECT_TRUE(getCodec(CodecType::LZ4)->needsUncompressedLength());
125 EXPECT_FALSE(getCodec(CodecType::SNAPPY)->needsUncompressedLength());
126 EXPECT_FALSE(getCodec(CodecType::ZLIB)->needsUncompressedLength());
127 EXPECT_FALSE(getCodec(CodecType::LZ4_VARINT_SIZE)->needsUncompressedLength());
128 EXPECT_TRUE(getCodec(CodecType::LZMA2)->needsUncompressedLength());
129 EXPECT_FALSE(getCodec(CodecType::LZMA2_VARINT_SIZE)
130 ->needsUncompressedLength());
133 class CompressionTest
134 : public testing::TestWithParam<std::tr1::tuple<int, CodecType>> {
137 auto tup = GetParam();
138 uncompressedLength_ = uint64_t(1) << std::tr1::get<0>(tup);
139 codec_ = getCodec(std::tr1::get<1>(tup));
142 void runSimpleTest(const DataHolder& dh);
144 uint64_t uncompressedLength_;
145 std::unique_ptr<Codec> codec_;
148 void CompressionTest::runSimpleTest(const DataHolder& dh) {
149 auto original = IOBuf::wrapBuffer(dh.data(uncompressedLength_));
150 auto compressed = codec_->compress(original.get());
151 if (!codec_->needsUncompressedLength()) {
152 auto uncompressed = codec_->uncompress(compressed.get());
154 EXPECT_EQ(uncompressedLength_, uncompressed->computeChainDataLength());
155 EXPECT_EQ(dh.hash(uncompressedLength_), hashIOBuf(uncompressed.get()));
158 auto uncompressed = codec_->uncompress(compressed.get(),
159 uncompressedLength_);
160 EXPECT_EQ(uncompressedLength_, uncompressed->computeChainDataLength());
161 EXPECT_EQ(dh.hash(uncompressedLength_), hashIOBuf(uncompressed.get()));
165 TEST_P(CompressionTest, RandomData) {
166 runSimpleTest(randomDataHolder);
169 TEST_P(CompressionTest, ConstantData) {
170 runSimpleTest(constantDataHolder);
173 INSTANTIATE_TEST_CASE_P(
176 testing::Combine(testing::Values(0, 1, 12, 22, 25, 27),
177 testing::Values(CodecType::NO_COMPRESSION,
181 CodecType::LZ4_VARINT_SIZE,
183 CodecType::LZMA2_VARINT_SIZE)));
185 class CompressionVarintTest
186 : public testing::TestWithParam<std::tr1::tuple<int, CodecType>> {
189 auto tup = GetParam();
190 uncompressedLength_ = uint64_t(1) << std::tr1::get<0>(tup);
191 codec_ = getCodec(std::tr1::get<1>(tup));
194 void runSimpleTest(const DataHolder& dh);
196 uint64_t uncompressedLength_;
197 std::unique_ptr<Codec> codec_;
200 inline uint64_t oneBasedMsbPos(uint64_t number) {
202 for (; number > 0; ++pos, number >>= 1) {
207 void CompressionVarintTest::runSimpleTest(const DataHolder& dh) {
208 auto original = IOBuf::wrapBuffer(dh.data(uncompressedLength_));
209 auto compressed = codec_->compress(original.get());
212 Random::rand64(std::max(9UL, oneBasedMsbPos(uncompressedLength_)) / 9UL);
213 auto tinyBuf = IOBuf::copyBuffer(compressed->data(),
214 std::min(compressed->length(), breakPoint));
215 compressed->trimStart(breakPoint);
216 tinyBuf->prependChain(std::move(compressed));
217 compressed = std::move(tinyBuf);
219 auto uncompressed = codec_->uncompress(compressed.get());
221 EXPECT_EQ(uncompressedLength_, uncompressed->computeChainDataLength());
222 EXPECT_EQ(dh.hash(uncompressedLength_), hashIOBuf(uncompressed.get()));
225 TEST_P(CompressionVarintTest, RandomData) { runSimpleTest(randomDataHolder); }
227 TEST_P(CompressionVarintTest, ConstantData) {
228 runSimpleTest(constantDataHolder);
231 INSTANTIATE_TEST_CASE_P(
232 CompressionVarintTest,
233 CompressionVarintTest,
234 testing::Combine(testing::Values(0, 1, 12, 22, 25, 27),
235 testing::Values(CodecType::LZ4_VARINT_SIZE,
236 CodecType::LZMA2_VARINT_SIZE)));
238 class CompressionCorruptionTest : public testing::TestWithParam<CodecType> {
241 codec_ = getCodec(GetParam());
244 void runSimpleTest(const DataHolder& dh);
246 std::unique_ptr<Codec> codec_;
249 void CompressionCorruptionTest::runSimpleTest(const DataHolder& dh) {
250 constexpr uint64_t uncompressedLength = 42;
251 auto original = IOBuf::wrapBuffer(dh.data(uncompressedLength));
252 auto compressed = codec_->compress(original.get());
254 if (!codec_->needsUncompressedLength()) {
255 auto uncompressed = codec_->uncompress(compressed.get());
256 EXPECT_EQ(uncompressedLength, uncompressed->computeChainDataLength());
257 EXPECT_EQ(dh.hash(uncompressedLength), hashIOBuf(uncompressed.get()));
260 auto uncompressed = codec_->uncompress(compressed.get(),
262 EXPECT_EQ(uncompressedLength, uncompressed->computeChainDataLength());
263 EXPECT_EQ(dh.hash(uncompressedLength), hashIOBuf(uncompressed.get()));
266 EXPECT_THROW(codec_->uncompress(compressed.get(), uncompressedLength + 1),
269 // Corrupt the first character
270 ++(compressed->writableData()[0]);
272 if (!codec_->needsUncompressedLength()) {
273 EXPECT_THROW(codec_->uncompress(compressed.get()),
277 EXPECT_THROW(codec_->uncompress(compressed.get(), uncompressedLength),
281 TEST_P(CompressionCorruptionTest, RandomData) {
282 runSimpleTest(randomDataHolder);
285 TEST_P(CompressionCorruptionTest, ConstantData) {
286 runSimpleTest(constantDataHolder);
289 INSTANTIATE_TEST_CASE_P(
290 CompressionCorruptionTest,
291 CompressionCorruptionTest,
293 // NO_COMPRESSION can't detect corruption
294 // LZ4 can't detect corruption reliably (sigh)
300 int main(int argc, char *argv[]) {
301 testing::InitGoogleTest(&argc, argv);
302 gflags::ParseCommandLineFlags(&argc, &argv, true);
304 auto ret = RUN_ALL_TESTS();
306 folly::runBenchmarksOnFlag();