Skip to content

Commit 96513a8

Browse files
theraysmithcopybara-github
authored andcommitted
Added a blob_compare tool that compares two sbs files that may have the blobs in a different order
PiperOrigin-RevId: 696458888
1 parent 5674c33 commit 96513a8

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

compression/BUILD.bazel

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,3 +259,14 @@ cc_binary(
259259
"@highway//:thread_pool",
260260
],
261261
)
262+
263+
cc_binary(
264+
name = "blob_compare",
265+
srcs = ["blob_compare.cc"],
266+
deps = [
267+
":blob_store",
268+
":io",
269+
"@highway//:hwy",
270+
"@highway//:hwy_test_util",
271+
],
272+
)

compression/blob_compare.cc

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#include <cstdint>
2+
#include <cstdio>
3+
#include <string>
4+
#include <vector>
5+
6+
#include "compression/blob_store.h"
7+
#include "compression/io.h"
8+
#include "hwy/aligned_allocator.h"
9+
#include "hwy/base.h"
10+
11+
namespace gcpp {
12+
13+
// Compares two sbs files, ignoring the order of the blobs.
14+
// Gives up on the first mismatch.
15+
void CompareBlobs(const char* path1, const char* path2) {
16+
BlobReader reader1;
17+
HWY_ASSERT(reader1.Open(Path(path1)) == 0);
18+
BlobReader reader2;
19+
HWY_ASSERT(reader2.Open(Path(path2)) == 0);
20+
hwy::Span<const hwy::uint128_t> keys1 = reader1.Keys();
21+
size_t total_matches = 0;
22+
size_t total_fails = 0;
23+
for (size_t i = 0; i < keys1.size(); ++i) {
24+
fprintf(stderr, "key %s, blob1 size=%zu, blob2 size=%zu\n",
25+
StringFromKey(keys1[i]).c_str(), reader1.BlobSize(keys1[i]),
26+
reader2.BlobSize(keys1[i]));
27+
std::vector<uint8_t> data1(reader1.BlobSize(keys1[i]));
28+
HWY_ASSERT(reader1.ReadOne(keys1[i], data1.data(), data1.size()) == 0);
29+
HWY_ASSERT(reader2.BlobSize(keys1[i]) == data1.size());
30+
std::vector<uint8_t> data2(reader2.BlobSize(keys1[i]));
31+
HWY_ASSERT(reader2.ReadOne(keys1[i], data2.data(), data2.size()) == 0);
32+
size_t fails = 0;
33+
for (size_t j = 0; j < data1.size(); ++j) {
34+
if (data1[j] != data2[j]) {
35+
if (fails == 0) {
36+
fprintf(stderr, "key %s Mismatch at %zu\n",
37+
StringFromKey(keys1[i]).c_str(), j);
38+
}
39+
++fails;
40+
}
41+
}
42+
if (fails > 0) {
43+
fprintf(stderr, "key %s has %.2f%% Mismatch!\n",
44+
StringFromKey(keys1[i]).c_str(), 100.0 * fails / data1.size());
45+
++total_fails;
46+
} else {
47+
fprintf(stderr, "key %s Matched!\n", StringFromKey(keys1[i]).c_str());
48+
++total_matches;
49+
}
50+
}
51+
fprintf(stderr, "Total matches=%zu, mismatches=%zu\n", total_matches,
52+
total_fails);
53+
}
54+
55+
} // namespace gcpp
56+
57+
int main(int argc, char** argv) {
58+
if (argc != 3) {
59+
fprintf(stderr, "Usage: %s <sbs_path> <sbs_path>\n", argv[0]);
60+
return 1;
61+
}
62+
gcpp::CompareBlobs(argv[1], argv[2]);
63+
return 0;
64+
}

0 commit comments

Comments
 (0)