mirror of
https://github.com/deepseek-ai/3FS
synced 2025-06-26 18:16:45 +00:00
Initial commit
This commit is contained in:
41
tests/common/utils/TestShards.cc
Normal file
41
tests/common/utils/TestShards.cc
Normal file
@@ -0,0 +1,41 @@
|
||||
#include <folly/Random.h>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
#include "common/utils/RobinHood.h"
|
||||
#include "common/utils/Shards.h"
|
||||
#include "tests/GtestHelpers.h"
|
||||
|
||||
namespace hf3fs::test {
|
||||
namespace {
|
||||
|
||||
TEST(TestShards, Normal) {
|
||||
Shards<std::unordered_map<uint32_t, uint32_t>, 64> shards;
|
||||
|
||||
constexpr auto N = 8;
|
||||
constexpr auto M = 100000;
|
||||
std::vector<std::thread> threads(N);
|
||||
for (auto &thread : threads) {
|
||||
thread = std::thread([&shards] {
|
||||
for (auto i = 0; i < M; ++i) {
|
||||
auto r = folly::Random::rand32();
|
||||
shards.withLock([&](std::unordered_map<uint32_t, uint32_t> &map) { ++map[r]; }, r);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
for (auto &thread : threads) {
|
||||
thread.join();
|
||||
}
|
||||
|
||||
uint64_t sum = 0;
|
||||
shards.iterate([&sum](std::unordered_map<uint32_t, uint32_t> &map) {
|
||||
for (auto &[k, v] : map) {
|
||||
sum += v;
|
||||
}
|
||||
});
|
||||
ASSERT_EQ(sum, M * N);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
} // namespace hf3fs::test
|
||||
Reference in New Issue
Block a user