Fix compression tests when snappy not available (#11396)

Summary:
Tweak some bounds and things, and reduce risk of surprise results by running on all supported compressions (mostly).

Also improves the precise compressibility of CompressibleString by using RandomBinaryString.

Pull Request resolved: https://github.com/facebook/rocksdb/pull/11396

Test Plan: updated tests

Reviewed By: ltamasi

Differential Revision: D45211938

Pulled By: pdillinger

fbshipit-source-id: 9dc1dd8574a60a9364efe18558be66d31a35598b
This commit is contained in:
Peter Dillinger 2023-04-22 12:41:36 -07:00 committed by Facebook GitHub Bot
parent d79be3dca2
commit fb63d9b4ee
3 changed files with 146 additions and 176 deletions

View file

@ -20,30 +20,15 @@ class DBStatisticsTest : public DBTestBase {
}; };
TEST_F(DBStatisticsTest, CompressionStatsTest) { TEST_F(DBStatisticsTest, CompressionStatsTest) {
CompressionType type; for (CompressionType type : GetSupportedCompressions()) {
if (type == kNoCompression) {
if (Snappy_Supported()) { continue;
type = kSnappyCompression;
fprintf(stderr, "using snappy\n");
} else if (Zlib_Supported()) {
type = kZlibCompression;
fprintf(stderr, "using zlib\n");
} else if (BZip2_Supported()) {
type = kBZip2Compression;
fprintf(stderr, "using bzip2\n");
} else if (LZ4_Supported()) {
type = kLZ4Compression;
fprintf(stderr, "using lz4\n");
} else if (XPRESS_Supported()) {
type = kXpressCompression;
fprintf(stderr, "using xpress\n");
} else if (ZSTD_Supported()) {
type = kZSTD;
fprintf(stderr, "using ZSTD\n");
} else {
fprintf(stderr, "skipping test, compression disabled\n");
return;
} }
if (type == kBZip2Compression) {
// Weird behavior in this test
continue;
}
SCOPED_TRACE("Compression type: " + std::to_string(type));
Options options = CurrentOptions(); Options options = CurrentOptions();
options.compression = type; options.compression = type;
@ -67,7 +52,8 @@ TEST_F(DBStatisticsTest, CompressionStatsTest) {
Random rnd(301); Random rnd(301);
std::string buf; std::string buf;
// Check that compressions occur and are counted when compression is turned on // Check that compressions occur and are counted when compression is turned
// on
for (int i = 0; i < kNumKeysWritten; ++i) { for (int i = 0; i < kNumKeysWritten; ++i) {
ASSERT_OK( ASSERT_OK(
Put(Key(i), test::CompressibleString(&rnd, compress_to, len, &buf))); Put(Key(i), test::CompressibleString(&rnd, compress_to, len, &buf)));
@ -97,12 +83,10 @@ TEST_F(DBStatisticsTest, CompressionStatsTest) {
EXPECT_EQ(0, PopStat(NUMBER_BLOCK_COMPRESSION_REJECTED)); EXPECT_EQ(0, PopStat(NUMBER_BLOCK_COMPRESSION_REJECTED));
// Check when compression is rejected. // Check when compression is rejected.
compress_to = 0.95;
DestroyAndReopen(options); DestroyAndReopen(options);
for (int i = 0; i < kNumKeysWritten; ++i) { for (int i = 0; i < kNumKeysWritten; ++i) {
ASSERT_OK( ASSERT_OK(Put(Key(i), rnd.RandomBinaryString(len)));
Put(Key(i), test::CompressibleString(&rnd, compress_to, len, &buf)));
} }
ASSERT_OK(Flush()); ASSERT_OK(Flush());
for (int i = 0; i < kNumKeysWritten; ++i) { for (int i = 0; i < kNumKeysWritten; ++i) {
@ -126,8 +110,7 @@ TEST_F(DBStatisticsTest, CompressionStatsTest) {
DestroyAndReopen(options); DestroyAndReopen(options);
for (int i = 0; i < kNumKeysWritten; ++i) { for (int i = 0; i < kNumKeysWritten; ++i) {
ASSERT_OK( ASSERT_OK(Put(Key(i), rnd.RandomBinaryString(len)));
Put(Key(i), test::CompressibleString(&rnd, compress_to, len, &buf)));
} }
ASSERT_OK(Flush()); ASSERT_OK(Flush());
for (int i = 0; i < kNumKeysWritten; ++i) { for (int i = 0; i < kNumKeysWritten; ++i) {
@ -145,6 +128,7 @@ TEST_F(DBStatisticsTest, CompressionStatsTest) {
EXPECT_EQ(0, PopStat(BYTES_COMPRESSION_REJECTED)); EXPECT_EQ(0, PopStat(BYTES_COMPRESSION_REJECTED));
EXPECT_EQ(0, PopStat(BYTES_DECOMPRESSED_FROM)); EXPECT_EQ(0, PopStat(BYTES_DECOMPRESSED_FROM));
EXPECT_EQ(0, PopStat(BYTES_DECOMPRESSED_TO)); EXPECT_EQ(0, PopStat(BYTES_DECOMPRESSED_TO));
}
} }
TEST_F(DBStatisticsTest, MutexWaitStatsDisabledByDefault) { TEST_F(DBStatisticsTest, MutexWaitStatsDisabledByDefault) {

View file

@ -5081,29 +5081,14 @@ TEST_P(BlockBasedTableTest, PropertiesBlockRestartPointTest) {
} }
TEST_P(BlockBasedTableTest, CompressionRatioThreshold) { TEST_P(BlockBasedTableTest, CompressionRatioThreshold) {
Options options; for (CompressionType type : GetSupportedCompressions()) {
if (Snappy_Supported()) { if (type == kNoCompression) {
options.compression = kSnappyCompression; continue;
fprintf(stderr, "using snappy\n");
} else if (Zlib_Supported()) {
options.compression = kZlibCompression;
fprintf(stderr, "using zlib\n");
} else if (BZip2_Supported()) {
options.compression = kBZip2Compression;
fprintf(stderr, "using bzip2\n");
} else if (LZ4_Supported()) {
options.compression = kLZ4Compression;
fprintf(stderr, "using lz4\n");
} else if (XPRESS_Supported()) {
options.compression = kXpressCompression;
fprintf(stderr, "using xpress\n");
} else if (ZSTD_Supported()) {
options.compression = kZSTD;
fprintf(stderr, "using ZSTD\n");
} else {
fprintf(stderr, "skipping test, compression disabled\n");
return;
} }
SCOPED_TRACE("Compression type: " + std::to_string(type));
Options options;
options.compression = type;
BlockBasedTableOptions table_options = GetBlockBasedTableOptions(); BlockBasedTableOptions table_options = GetBlockBasedTableOptions();
int len = 10000; int len = 10000;
@ -5132,15 +5117,16 @@ TEST_P(BlockBasedTableTest, CompressionRatioThreshold) {
size_t table_file_size = c.TEST_GetSink()->contents().size(); size_t table_file_size = c.TEST_GetSink()->contents().size();
size_t approx_sst_overhead = 1000; size_t approx_sst_overhead = 1000;
if (compressible_to < threshold / 1024.0) { if (compressible_to < threshold / 1024.0) {
// Should be compressed // Should be compressed (substantial variance depending on algorithm)
EXPECT_NEAR2(len * compressible_to + approx_sst_overhead, EXPECT_NEAR2(len * compressible_to + approx_sst_overhead,
table_file_size, len / 10); table_file_size, len / 8);
} else { } else {
// Should not be compressed // Should not be compressed
EXPECT_NEAR2(len + approx_sst_overhead, table_file_size, len / 10); EXPECT_NEAR2(len + approx_sst_overhead, table_file_size, len / 10);
} }
} }
} }
}
} }
TEST_P(BlockBasedTableTest, PropertiesMetaBlockLast) { TEST_P(BlockBasedTableTest, PropertiesMetaBlockLast) {

View file

@ -76,7 +76,7 @@ extern Slice CompressibleString(Random* rnd, double compressed_fraction,
int len, std::string* dst) { int len, std::string* dst) {
int raw = static_cast<int>(len * compressed_fraction); int raw = static_cast<int>(len * compressed_fraction);
if (raw < 1) raw = 1; if (raw < 1) raw = 1;
std::string raw_data = rnd->RandomString(raw); std::string raw_data = rnd->RandomBinaryString(raw);
// Duplicate the random data until we have filled "len" bytes // Duplicate the random data until we have filled "len" bytes
dst->clear(); dst->clear();