in cachelib/benchmarks/CacheAllocatorOpsMicroBench.cpp [122:196]
void runFindMultiThreads(int numThreads,
bool isPeek,
uint64_t objSize,
uint64_t writePct) {
constexpr uint64_t kObjects = 100'000;
constexpr uint64_t kLoops = 10'000'000;
auto cache = getCache();
std::vector<std::string> keys;
for (uint64_t i = 0; i < kObjects; i++) {
// Length of key should be 10 bytes
auto key = folly::sformat("k_{: <8}", i);
auto hdl = cache->allocate(0, key, objSize);
XCHECK(hdl);
cache->insertOrReplace(hdl);
keys.push_back(key);
}
navy::SeqPoints sp;
auto readOps = [&] {
sp.wait(0);
std::mt19937 gen;
std::uniform_int_distribution<uint64_t> dist(0, kObjects - 1);
for (uint64_t loop = 0; loop < kLoops; loop++) {
const auto& key = keys[dist(gen)];
auto hdl = isPeek ? cache->peek(key) : cache->find(key);
folly::doNotOptimizeAway(hdl);
}
};
auto writeOps = [&] {
sp.wait(0);
if (writePct == 0) {
return;
}
std::mt19937 gen;
std::uniform_int_distribution<uint64_t> dist(0,
kObjects * writePct / 100 - 1);
for (uint64_t loop = 0; loop < kLoops / 10; loop++) {
const auto& key = keys[dist(gen)];
auto hdl = cache->allocate(0, key, objSize);
XCHECK(hdl);
cache->insertOrReplace(hdl);
folly::doNotOptimizeAway(hdl);
}
};
// Create readers
std::vector<std::thread> rs;
for (int i = 0; i < numThreads; i++) {
rs.push_back(std::thread{readOps});
}
// Create writers
std::vector<std::thread> ws;
for (int i = 0; i < 4; i++) {
ws.push_back(std::thread{writeOps});
}
{
Timer t{folly::sformat("{} - {: <2} Threads, {: <4} Bytes, {: <2}% Write",
isPeek ? "Peek" : "Find", numThreads, objSize,
writePct),
kLoops};
sp.reached(0); // Start the operations
for (auto& r : rs) {
r.join();
}
}
for (auto& w : ws) {
w.join();
}
}