2 * Copyright 2014 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/io/IOBufQueue.h>
32 namespace folly { namespace io { namespace test {
34 class DataHolder : private boost::noncopyable {
36 uint64_t hash(size_t size) const;
37 ByteRange data(size_t size) const;
40 explicit DataHolder(size_t sizeLog2);
42 std::unique_ptr<uint8_t[]> data_;
43 mutable std::unordered_map<uint64_t, uint64_t> hashCache_;
46 DataHolder::DataHolder(size_t sizeLog2)
47 : size_(size_t(1) << sizeLog2),
48 data_(new uint8_t[size_]) {
51 uint64_t DataHolder::hash(size_t size) const {
52 CHECK_LE(size, size_);
53 auto p = hashCache_.find(size);
54 if (p != hashCache_.end()) {
58 uint64_t h = folly::hash::fnv64_buf(data_.get(), size);
63 ByteRange DataHolder::data(size_t size) const {
64 CHECK_LE(size, size_);
65 return ByteRange(data_.get(), size);
68 uint64_t hashIOBuf(const IOBuf* buf) {
69 uint64_t h = folly::hash::FNV_64_HASH_START;
70 for (auto& range : *buf) {
71 h = folly::hash::fnv64_buf(range.data(), range.size(), h);
76 class RandomDataHolder : public DataHolder {
78 explicit RandomDataHolder(size_t sizeLog2);
81 RandomDataHolder::RandomDataHolder(size_t sizeLog2)
82 : DataHolder(sizeLog2) {
83 constexpr size_t numThreadsLog2 = 3;
84 constexpr size_t numThreads = size_t(1) << numThreadsLog2;
86 uint32_t seed = randomNumberSeed();
88 std::vector<std::thread> threads;
89 threads.reserve(numThreads);
90 for (size_t t = 0; t < numThreads; ++t) {
92 [this, seed, t, numThreadsLog2, sizeLog2] () {
93 std::mt19937 rng(seed + t);
94 size_t countLog2 = size_t(1) << (sizeLog2 - numThreadsLog2);
95 size_t start = size_t(t) << countLog2;
96 for (size_t i = 0; i < countLog2; ++i) {
97 this->data_[start + i] = rng();
102 for (auto& t : threads) {
107 class ConstantDataHolder : public DataHolder {
109 explicit ConstantDataHolder(size_t sizeLog2);
112 ConstantDataHolder::ConstantDataHolder(size_t sizeLog2)
113 : DataHolder(sizeLog2) {
114 memset(data_.get(), 'a', size_);
117 constexpr size_t dataSizeLog2 = 27; // 128MiB
118 RandomDataHolder randomDataHolder(dataSizeLog2);
119 ConstantDataHolder constantDataHolder(dataSizeLog2);
121 TEST(CompressionTestNeedsUncompressedLength, Simple) {
122 EXPECT_FALSE(getCodec(CodecType::NO_COMPRESSION)->needsUncompressedLength());
123 EXPECT_TRUE(getCodec(CodecType::LZ4)->needsUncompressedLength());
124 EXPECT_FALSE(getCodec(CodecType::SNAPPY)->needsUncompressedLength());
125 EXPECT_FALSE(getCodec(CodecType::ZLIB)->needsUncompressedLength());
126 EXPECT_FALSE(getCodec(CodecType::LZ4_VARINT_SIZE)->needsUncompressedLength());
127 EXPECT_TRUE(getCodec(CodecType::LZMA2)->needsUncompressedLength());
128 EXPECT_FALSE(getCodec(CodecType::LZMA2_VARINT_SIZE)
129 ->needsUncompressedLength());
132 class CompressionTest : public testing::TestWithParam<
133 std::tr1::tuple<int, CodecType>> {
136 auto tup = GetParam();
137 uncompressedLength_ = uint64_t(1) << std::tr1::get<0>(tup);
138 codec_ = getCodec(std::tr1::get<1>(tup));
141 void runSimpleTest(const DataHolder& dh);
143 uint64_t uncompressedLength_;
144 std::unique_ptr<Codec> codec_;
147 void CompressionTest::runSimpleTest(const DataHolder& dh) {
148 auto original = IOBuf::wrapBuffer(dh.data(uncompressedLength_));
149 auto compressed = codec_->compress(original.get());
150 if (!codec_->needsUncompressedLength()) {
151 auto uncompressed = codec_->uncompress(compressed.get());
152 EXPECT_EQ(uncompressedLength_, uncompressed->computeChainDataLength());
153 EXPECT_EQ(dh.hash(uncompressedLength_), hashIOBuf(uncompressed.get()));
156 auto uncompressed = codec_->uncompress(compressed.get(),
157 uncompressedLength_);
158 EXPECT_EQ(uncompressedLength_, uncompressed->computeChainDataLength());
159 EXPECT_EQ(dh.hash(uncompressedLength_), hashIOBuf(uncompressed.get()));
163 TEST_P(CompressionTest, RandomData) {
164 runSimpleTest(randomDataHolder);
167 TEST_P(CompressionTest, ConstantData) {
168 runSimpleTest(constantDataHolder);
171 INSTANTIATE_TEST_CASE_P(
175 testing::Values(0, 1, 12, 22, 25, 27),
176 testing::Values(CodecType::NO_COMPRESSION,
180 CodecType::LZ4_VARINT_SIZE,
182 CodecType::LZMA2_VARINT_SIZE)));
184 class CompressionCorruptionTest : public testing::TestWithParam<CodecType> {
187 codec_ = getCodec(GetParam());
190 void runSimpleTest(const DataHolder& dh);
192 std::unique_ptr<Codec> codec_;
195 void CompressionCorruptionTest::runSimpleTest(const DataHolder& dh) {
196 constexpr uint64_t uncompressedLength = 42;
197 auto original = IOBuf::wrapBuffer(dh.data(uncompressedLength));
198 auto compressed = codec_->compress(original.get());
200 if (!codec_->needsUncompressedLength()) {
201 auto uncompressed = codec_->uncompress(compressed.get());
202 EXPECT_EQ(uncompressedLength, uncompressed->computeChainDataLength());
203 EXPECT_EQ(dh.hash(uncompressedLength), hashIOBuf(uncompressed.get()));
206 auto uncompressed = codec_->uncompress(compressed.get(),
208 EXPECT_EQ(uncompressedLength, uncompressed->computeChainDataLength());
209 EXPECT_EQ(dh.hash(uncompressedLength), hashIOBuf(uncompressed.get()));
212 EXPECT_THROW(codec_->uncompress(compressed.get(), uncompressedLength + 1),
215 // Corrupt the first character
216 ++(compressed->writableData()[0]);
218 if (!codec_->needsUncompressedLength()) {
219 EXPECT_THROW(codec_->uncompress(compressed.get()),
223 EXPECT_THROW(codec_->uncompress(compressed.get(), uncompressedLength),
227 TEST_P(CompressionCorruptionTest, RandomData) {
228 runSimpleTest(randomDataHolder);
231 TEST_P(CompressionCorruptionTest, ConstantData) {
232 runSimpleTest(constantDataHolder);
235 INSTANTIATE_TEST_CASE_P(
236 CompressionCorruptionTest,
237 CompressionCorruptionTest,
239 // NO_COMPRESSION can't detect corruption
240 // LZ4 can't detect corruption reliably (sigh)
246 int main(int argc, char *argv[]) {
247 testing::InitGoogleTest(&argc, argv);
248 gflags::ParseCommandLineFlags(&argc, &argv, true);
250 auto ret = RUN_ALL_TESTS();
252 folly::runBenchmarksOnFlag();