2 * Copyright 2016 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>
22 #include <unordered_map>
24 #include <boost/noncopyable.hpp>
25 #include <glog/logging.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>
32 #include <folly/portability/GTest.h>
34 namespace folly { namespace io { namespace test {
36 class DataHolder : private boost::noncopyable {
38 uint64_t hash(size_t size) const;
39 ByteRange data(size_t size) const;
42 explicit DataHolder(size_t sizeLog2);
44 std::unique_ptr<uint8_t[]> data_;
45 mutable std::unordered_map<uint64_t, uint64_t> hashCache_;
48 DataHolder::DataHolder(size_t sizeLog2)
49 : size_(size_t(1) << sizeLog2),
50 data_(new uint8_t[size_]) {
53 uint64_t DataHolder::hash(size_t size) const {
54 CHECK_LE(size, size_);
55 auto p = hashCache_.find(size);
56 if (p != hashCache_.end()) {
60 uint64_t h = folly::hash::fnv64_buf(data_.get(), size);
65 ByteRange DataHolder::data(size_t size) const {
66 CHECK_LE(size, size_);
67 return ByteRange(data_.get(), size);
70 uint64_t hashIOBuf(const IOBuf* buf) {
71 uint64_t h = folly::hash::FNV_64_HASH_START;
72 for (auto& range : *buf) {
73 h = folly::hash::fnv64_buf(range.data(), range.size(), h);
78 class RandomDataHolder : public DataHolder {
80 explicit RandomDataHolder(size_t sizeLog2);
83 RandomDataHolder::RandomDataHolder(size_t sizeLog2)
84 : DataHolder(sizeLog2) {
85 constexpr size_t numThreadsLog2 = 3;
86 constexpr size_t numThreads = size_t(1) << numThreadsLog2;
88 uint32_t seed = randomNumberSeed();
90 std::vector<std::thread> threads;
91 threads.reserve(numThreads);
92 for (size_t t = 0; t < numThreads; ++t) {
94 [this, seed, t, numThreadsLog2, sizeLog2] () {
95 std::mt19937 rng(seed + t);
96 size_t countLog2 = sizeLog2 - numThreadsLog2;
97 size_t start = size_t(t) << countLog2;
98 for (size_t i = 0; i < countLog2; ++i) {
99 this->data_[start + i] = rng();
104 for (auto& t : threads) {
109 class ConstantDataHolder : public DataHolder {
111 explicit ConstantDataHolder(size_t sizeLog2);
114 ConstantDataHolder::ConstantDataHolder(size_t sizeLog2)
115 : DataHolder(sizeLog2) {
116 memset(data_.get(), 'a', size_);
119 constexpr size_t dataSizeLog2 = 27; // 128MiB
120 RandomDataHolder randomDataHolder(dataSizeLog2);
121 ConstantDataHolder constantDataHolder(dataSizeLog2);
123 TEST(CompressionTestNeedsUncompressedLength, Simple) {
124 EXPECT_FALSE(getCodec(CodecType::NO_COMPRESSION)->needsUncompressedLength());
125 EXPECT_TRUE(getCodec(CodecType::LZ4)->needsUncompressedLength());
126 EXPECT_FALSE(getCodec(CodecType::SNAPPY)->needsUncompressedLength());
127 EXPECT_FALSE(getCodec(CodecType::ZLIB)->needsUncompressedLength());
128 EXPECT_FALSE(getCodec(CodecType::LZ4_VARINT_SIZE)->needsUncompressedLength());
129 EXPECT_TRUE(getCodec(CodecType::LZMA2)->needsUncompressedLength());
130 EXPECT_FALSE(getCodec(CodecType::LZMA2_VARINT_SIZE)
131 ->needsUncompressedLength());
132 EXPECT_FALSE(getCodec(CodecType::ZSTD)->needsUncompressedLength());
133 EXPECT_FALSE(getCodec(CodecType::GZIP)->needsUncompressedLength());
136 class CompressionTest
137 : public testing::TestWithParam<std::tr1::tuple<int, int, CodecType>> {
139 void SetUp() override {
140 auto tup = GetParam();
141 uncompressedLength_ = uint64_t(1) << std::tr1::get<0>(tup);
142 chunks_ = std::tr1::get<1>(tup);
143 codec_ = getCodec(std::tr1::get<2>(tup));
146 void runSimpleTest(const DataHolder& dh);
149 std::unique_ptr<IOBuf> split(std::unique_ptr<IOBuf> data) const;
151 uint64_t uncompressedLength_;
153 std::unique_ptr<Codec> codec_;
156 void CompressionTest::runSimpleTest(const DataHolder& dh) {
157 const auto original = split(IOBuf::wrapBuffer(dh.data(uncompressedLength_)));
158 const auto compressed = split(codec_->compress(original.get()));
159 if (!codec_->needsUncompressedLength()) {
160 auto uncompressed = codec_->uncompress(compressed.get());
161 EXPECT_EQ(uncompressedLength_, uncompressed->computeChainDataLength());
162 EXPECT_EQ(dh.hash(uncompressedLength_), hashIOBuf(uncompressed.get()));
165 auto uncompressed = codec_->uncompress(compressed.get(),
166 uncompressedLength_);
167 EXPECT_EQ(uncompressedLength_, uncompressed->computeChainDataLength());
168 EXPECT_EQ(dh.hash(uncompressedLength_), hashIOBuf(uncompressed.get()));
172 // Uniformly split data into (potentially empty) chunks.
173 std::unique_ptr<IOBuf> CompressionTest::split(
174 std::unique_ptr<IOBuf> data) const {
175 if (data->isChained()) {
179 const size_t size = data->computeChainDataLength();
181 std::multiset<size_t> splits;
182 for (size_t i = 1; i < chunks_; ++i) {
183 splits.insert(Random::rand64(size));
186 folly::IOBufQueue result;
189 for (size_t split : splits) {
190 result.append(IOBuf::copyBuffer(data->data() + offset, split - offset));
193 result.append(IOBuf::copyBuffer(data->data() + offset, size - offset));
195 return result.move();
198 TEST_P(CompressionTest, RandomData) {
199 runSimpleTest(randomDataHolder);
202 TEST_P(CompressionTest, ConstantData) {
203 runSimpleTest(constantDataHolder);
206 INSTANTIATE_TEST_CASE_P(
210 testing::Values(0, 1, 12, 22, 25, 27),
211 testing::Values(1, 2, 3, 8, 65),
213 CodecType::NO_COMPRESSION,
217 CodecType::LZ4_VARINT_SIZE,
219 CodecType::LZMA2_VARINT_SIZE,
223 class CompressionVarintTest
224 : public testing::TestWithParam<std::tr1::tuple<int, CodecType>> {
226 void SetUp() override {
227 auto tup = GetParam();
228 uncompressedLength_ = uint64_t(1) << std::tr1::get<0>(tup);
229 codec_ = getCodec(std::tr1::get<1>(tup));
232 void runSimpleTest(const DataHolder& dh);
234 uint64_t uncompressedLength_;
235 std::unique_ptr<Codec> codec_;
238 inline uint64_t oneBasedMsbPos(uint64_t number) {
240 for (; number > 0; ++pos, number >>= 1) {
245 void CompressionVarintTest::runSimpleTest(const DataHolder& dh) {
246 auto original = IOBuf::wrapBuffer(dh.data(uncompressedLength_));
247 auto compressed = codec_->compress(original.get());
251 std::max(uint64_t(9), oneBasedMsbPos(uncompressedLength_)) / 9UL);
252 auto tinyBuf = IOBuf::copyBuffer(compressed->data(),
253 std::min(compressed->length(), breakPoint));
254 compressed->trimStart(breakPoint);
255 tinyBuf->prependChain(std::move(compressed));
256 compressed = std::move(tinyBuf);
258 auto uncompressed = codec_->uncompress(compressed.get());
260 EXPECT_EQ(uncompressedLength_, uncompressed->computeChainDataLength());
261 EXPECT_EQ(dh.hash(uncompressedLength_), hashIOBuf(uncompressed.get()));
264 TEST_P(CompressionVarintTest, RandomData) {
265 runSimpleTest(randomDataHolder);
268 TEST_P(CompressionVarintTest, ConstantData) {
269 runSimpleTest(constantDataHolder);
272 INSTANTIATE_TEST_CASE_P(
273 CompressionVarintTest,
274 CompressionVarintTest,
276 testing::Values(0, 1, 12, 22, 25, 27),
278 CodecType::LZ4_VARINT_SIZE,
279 CodecType::LZMA2_VARINT_SIZE)));
281 class CompressionCorruptionTest : public testing::TestWithParam<CodecType> {
283 void SetUp() override { codec_ = getCodec(GetParam()); }
285 void runSimpleTest(const DataHolder& dh);
287 std::unique_ptr<Codec> codec_;
290 void CompressionCorruptionTest::runSimpleTest(const DataHolder& dh) {
291 constexpr uint64_t uncompressedLength = 42;
292 auto original = IOBuf::wrapBuffer(dh.data(uncompressedLength));
293 auto compressed = codec_->compress(original.get());
295 if (!codec_->needsUncompressedLength()) {
296 auto uncompressed = codec_->uncompress(compressed.get());
297 EXPECT_EQ(uncompressedLength, uncompressed->computeChainDataLength());
298 EXPECT_EQ(dh.hash(uncompressedLength), hashIOBuf(uncompressed.get()));
301 auto uncompressed = codec_->uncompress(compressed.get(),
303 EXPECT_EQ(uncompressedLength, uncompressed->computeChainDataLength());
304 EXPECT_EQ(dh.hash(uncompressedLength), hashIOBuf(uncompressed.get()));
307 EXPECT_THROW(codec_->uncompress(compressed.get(), uncompressedLength + 1),
310 // Corrupt the first character
311 ++(compressed->writableData()[0]);
313 if (!codec_->needsUncompressedLength()) {
314 EXPECT_THROW(codec_->uncompress(compressed.get()),
318 EXPECT_THROW(codec_->uncompress(compressed.get(), uncompressedLength),
322 TEST_P(CompressionCorruptionTest, RandomData) {
323 runSimpleTest(randomDataHolder);
326 TEST_P(CompressionCorruptionTest, ConstantData) {
327 runSimpleTest(constantDataHolder);
330 INSTANTIATE_TEST_CASE_P(
331 CompressionCorruptionTest,
332 CompressionCorruptionTest,
334 // NO_COMPRESSION can't detect corruption
335 // LZ4 can't detect corruption reliably (sigh)
341 int main(int argc, char *argv[]) {
342 testing::InitGoogleTest(&argc, argv);
343 gflags::ParseCommandLineFlags(&argc, &argv, true);
345 auto ret = RUN_ALL_TESTS();
347 folly::runBenchmarksOnFlag();