1// Copyright 2020 Google Inc. All Rights Reserved.
2//
3// Redistribution and use in source and binary forms, with or without
4// modification, are permitted provided that the following conditions are
5// met:
6//
7// * Redistributions of source code must retain the above copyright
8// notice, this list of conditions and the following disclaimer.
9// * Redistributions in binary form must reproduce the above
10// copyright notice, this list of conditions and the following disclaimer
11// in the documentation and/or other materials provided with the
12// distribution.
13// * Neither the name of Google Inc. nor the names of its
14// contributors may be used to endorse or promote products derived from
15// this software without specific prior written permission.
16//
17// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29#include <cstddef>
30#include <cstdint>
31#include <string>
32#include <vector>
33
34#include "snappy-test.h"
35
36#include "benchmark/benchmark.h"
37
38#include "snappy-internal.h"
39#include "snappy-sinksource.h"
40#include "snappy.h"
41#include "snappy_test_data.h"
42
43namespace snappy {
44
45namespace {
46
47void BM_UFlat(benchmark::State& state) {
48 // Pick file to process based on state.range(0).
49 int file_index = state.range(0);
50
51 CHECK_GE(file_index, 0);
52 CHECK_LT(file_index, ARRAYSIZE(kTestDataFiles));
53 std::string contents =
54 ReadTestDataFile(kTestDataFiles[file_index].filename,
55 kTestDataFiles[file_index].size_limit);
56
57 std::string zcontents;
58 snappy::Compress(contents.data(), contents.size(), &zcontents);
59 char* dst = new char[contents.size()];
60
61 for (auto s : state) {
62 CHECK(snappy::RawUncompress(zcontents.data(), zcontents.size(), dst));
63 benchmark::DoNotOptimize(dst);
64 }
65 state.SetBytesProcessed(static_cast<int64_t>(state.iterations()) *
66 static_cast<int64_t>(contents.size()));
67 state.SetLabel(kTestDataFiles[file_index].label);
68
69 delete[] dst;
70}
71BENCHMARK(BM_UFlat)->DenseRange(0, ARRAYSIZE(kTestDataFiles) - 1);
72
73struct SourceFiles {
74 SourceFiles() {
75 for (int i = 0; i < kFiles; i++) {
76 std::string contents = ReadTestDataFile(kTestDataFiles[i].filename,
77 kTestDataFiles[i].size_limit);
78 max_size = std::max(max_size, contents.size());
79 sizes[i] = contents.size();
80 snappy::Compress(contents.data(), contents.size(), &zcontents[i]);
81 }
82 }
83 static constexpr int kFiles = ARRAYSIZE(kTestDataFiles);
84 std::string zcontents[kFiles];
85 size_t sizes[kFiles];
86 size_t max_size = 0;
87};
88
89void BM_UFlatMedley(benchmark::State& state) {
90 static const SourceFiles* const source = new SourceFiles();
91
92 std::vector<char> dst(source->max_size);
93
94 for (auto s : state) {
95 for (int i = 0; i < SourceFiles::kFiles; i++) {
96 CHECK(snappy::RawUncompress(source->zcontents[i].data(),
97 source->zcontents[i].size(), dst.data()));
98 benchmark::DoNotOptimize(dst);
99 }
100 }
101
102 int64_t source_sizes = 0;
103 for (int i = 0; i < SourceFiles::kFiles; i++) {
104 source_sizes += static_cast<int64_t>(source->sizes[i]);
105 }
106 state.SetBytesProcessed(static_cast<int64_t>(state.iterations()) *
107 source_sizes);
108}
109BENCHMARK(BM_UFlatMedley);
110
111void BM_UValidate(benchmark::State& state) {
112 // Pick file to process based on state.range(0).
113 int file_index = state.range(0);
114
115 CHECK_GE(file_index, 0);
116 CHECK_LT(file_index, ARRAYSIZE(kTestDataFiles));
117 std::string contents =
118 ReadTestDataFile(kTestDataFiles[file_index].filename,
119 kTestDataFiles[file_index].size_limit);
120
121 std::string zcontents;
122 snappy::Compress(contents.data(), contents.size(), &zcontents);
123
124 for (auto s : state) {
125 CHECK(snappy::IsValidCompressedBuffer(zcontents.data(), zcontents.size()));
126 }
127 state.SetBytesProcessed(static_cast<int64_t>(state.iterations()) *
128 static_cast<int64_t>(contents.size()));
129 state.SetLabel(kTestDataFiles[file_index].label);
130}
131BENCHMARK(BM_UValidate)->DenseRange(0, ARRAYSIZE(kTestDataFiles) - 1);
132
133void BM_UValidateMedley(benchmark::State& state) {
134 static const SourceFiles* const source = new SourceFiles();
135
136 for (auto s : state) {
137 for (int i = 0; i < SourceFiles::kFiles; i++) {
138 CHECK(snappy::IsValidCompressedBuffer(source->zcontents[i].data(),
139 source->zcontents[i].size()));
140 }
141 }
142
143 int64_t source_sizes = 0;
144 for (int i = 0; i < SourceFiles::kFiles; i++) {
145 source_sizes += static_cast<int64_t>(source->sizes[i]);
146 }
147 state.SetBytesProcessed(static_cast<int64_t>(state.iterations()) *
148 source_sizes);
149}
150BENCHMARK(BM_UValidateMedley);
151
152void BM_UIOVec(benchmark::State& state) {
153 // Pick file to process based on state.range(0).
154 int file_index = state.range(0);
155
156 CHECK_GE(file_index, 0);
157 CHECK_LT(file_index, ARRAYSIZE(kTestDataFiles));
158 std::string contents =
159 ReadTestDataFile(kTestDataFiles[file_index].filename,
160 kTestDataFiles[file_index].size_limit);
161
162 std::string zcontents;
163 snappy::Compress(contents.data(), contents.size(), &zcontents);
164
165 // Uncompress into an iovec containing ten entries.
166 const int kNumEntries = 10;
167 struct iovec iov[kNumEntries];
168 char *dst = new char[contents.size()];
169 size_t used_so_far = 0;
170 for (int i = 0; i < kNumEntries; ++i) {
171 iov[i].iov_base = dst + used_so_far;
172 if (used_so_far == contents.size()) {
173 iov[i].iov_len = 0;
174 continue;
175 }
176
177 if (i == kNumEntries - 1) {
178 iov[i].iov_len = contents.size() - used_so_far;
179 } else {
180 iov[i].iov_len = contents.size() / kNumEntries;
181 }
182 used_so_far += iov[i].iov_len;
183 }
184
185 for (auto s : state) {
186 CHECK(snappy::RawUncompressToIOVec(zcontents.data(), zcontents.size(), iov,
187 kNumEntries));
188 benchmark::DoNotOptimize(iov);
189 }
190 state.SetBytesProcessed(static_cast<int64_t>(state.iterations()) *
191 static_cast<int64_t>(contents.size()));
192 state.SetLabel(kTestDataFiles[file_index].label);
193
194 delete[] dst;
195}
196BENCHMARK(BM_UIOVec)->DenseRange(0, 4);
197
198void BM_UFlatSink(benchmark::State& state) {
199 // Pick file to process based on state.range(0).
200 int file_index = state.range(0);
201
202 CHECK_GE(file_index, 0);
203 CHECK_LT(file_index, ARRAYSIZE(kTestDataFiles));
204 std::string contents =
205 ReadTestDataFile(kTestDataFiles[file_index].filename,
206 kTestDataFiles[file_index].size_limit);
207
208 std::string zcontents;
209 snappy::Compress(contents.data(), contents.size(), &zcontents);
210 char* dst = new char[contents.size()];
211
212 for (auto s : state) {
213 snappy::ByteArraySource source(zcontents.data(), zcontents.size());
214 snappy::UncheckedByteArraySink sink(dst);
215 CHECK(snappy::Uncompress(&source, &sink));
216 benchmark::DoNotOptimize(sink);
217 }
218 state.SetBytesProcessed(static_cast<int64_t>(state.iterations()) *
219 static_cast<int64_t>(contents.size()));
220 state.SetLabel(kTestDataFiles[file_index].label);
221
222 std::string s(dst, contents.size());
223 CHECK_EQ(contents, s);
224
225 delete[] dst;
226}
227
228BENCHMARK(BM_UFlatSink)->DenseRange(0, ARRAYSIZE(kTestDataFiles) - 1);
229
230void BM_ZFlat(benchmark::State& state) {
231 // Pick file to process based on state.range(0).
232 int file_index = state.range(0);
233
234 CHECK_GE(file_index, 0);
235 CHECK_LT(file_index, ARRAYSIZE(kTestDataFiles));
236 std::string contents =
237 ReadTestDataFile(kTestDataFiles[file_index].filename,
238 kTestDataFiles[file_index].size_limit);
239 char* dst = new char[snappy::MaxCompressedLength(contents.size())];
240
241 size_t zsize = 0;
242 for (auto s : state) {
243 snappy::RawCompress(contents.data(), contents.size(), dst, &zsize);
244 benchmark::DoNotOptimize(dst);
245 }
246 state.SetBytesProcessed(static_cast<int64_t>(state.iterations()) *
247 static_cast<int64_t>(contents.size()));
248 const double compression_ratio =
249 static_cast<double>(zsize) / std::max<size_t>(1, contents.size());
250 state.SetLabel(StrFormat("%s (%.2f %%)", kTestDataFiles[file_index].label,
251 100.0 * compression_ratio));
252 VLOG(0) << StrFormat("compression for %s: %d -> %d bytes",
253 kTestDataFiles[file_index].label, contents.size(),
254 zsize);
255 delete[] dst;
256}
257BENCHMARK(BM_ZFlat)->DenseRange(0, ARRAYSIZE(kTestDataFiles) - 1);
258
259void BM_ZFlatAll(benchmark::State& state) {
260 const int num_files = ARRAYSIZE(kTestDataFiles);
261
262 std::vector<std::string> contents(num_files);
263 std::vector<char*> dst(num_files);
264
265 int64_t total_contents_size = 0;
266 for (int i = 0; i < num_files; ++i) {
267 contents[i] = ReadTestDataFile(kTestDataFiles[i].filename,
268 kTestDataFiles[i].size_limit);
269 dst[i] = new char[snappy::MaxCompressedLength(contents[i].size())];
270 total_contents_size += contents[i].size();
271 }
272
273 size_t zsize = 0;
274 for (auto s : state) {
275 for (int i = 0; i < num_files; ++i) {
276 snappy::RawCompress(contents[i].data(), contents[i].size(), dst[i],
277 &zsize);
278 benchmark::DoNotOptimize(dst);
279 }
280 }
281
282 state.SetBytesProcessed(static_cast<int64_t>(state.iterations()) *
283 total_contents_size);
284
285 for (char* dst_item : dst) {
286 delete[] dst_item;
287 }
288 state.SetLabel(StrFormat("%d kTestDataFiles", num_files));
289}
290BENCHMARK(BM_ZFlatAll);
291
292void BM_ZFlatIncreasingTableSize(benchmark::State& state) {
293 CHECK_GT(ARRAYSIZE(kTestDataFiles), 0);
294 const std::string base_content = ReadTestDataFile(
295 kTestDataFiles[0].filename, kTestDataFiles[0].size_limit);
296
297 std::vector<std::string> contents;
298 std::vector<char*> dst;
299 int64_t total_contents_size = 0;
300 for (int table_bits = kMinHashTableBits; table_bits <= kMaxHashTableBits;
301 ++table_bits) {
302 std::string content = base_content;
303 content.resize(1 << table_bits);
304 dst.push_back(new char[snappy::MaxCompressedLength(content.size())]);
305 total_contents_size += content.size();
306 contents.push_back(std::move(content));
307 }
308
309 size_t zsize = 0;
310 for (auto s : state) {
311 for (size_t i = 0; i < contents.size(); ++i) {
312 snappy::RawCompress(contents[i].data(), contents[i].size(), dst[i],
313 &zsize);
314 benchmark::DoNotOptimize(dst);
315 }
316 }
317
318 state.SetBytesProcessed(static_cast<int64_t>(state.iterations()) *
319 total_contents_size);
320
321 for (char* dst_item : dst) {
322 delete[] dst_item;
323 }
324 state.SetLabel(StrFormat("%d tables", contents.size()));
325}
326BENCHMARK(BM_ZFlatIncreasingTableSize);
327
328} // namespace
329
330} // namespace snappy
331