Run queries against the cache until '*done' becomes true. Returns a pair of the number of cache hits and lookups.
| 101 | // Run queries against the cache until '*done' becomes true. |
| 102 | // Returns a pair of the number of cache hits and lookups. |
| 103 | pair<int64_t, int64_t> DoQueries(const atomic<bool>* done) { |
| 104 | const BenchSetup& setup = GetParam(); |
| 105 | Random r(GetRandomSeed32()); |
| 106 | int64_t lookups = 0; |
| 107 | int64_t hits = 0; |
| 108 | while (!*done) { |
| 109 | uint32_t int_key; |
| 110 | if (setup.pattern == BenchSetup::Pattern::ZIPFIAN) { |
| 111 | int_key = r.Skewed(Bits::Log2Floor(setup.max_key())); |
| 112 | } else { |
| 113 | int_key = r.Uniform(setup.max_key()); |
| 114 | } |
| 115 | char key_buf[sizeof(int_key)]; |
| 116 | memcpy(key_buf, &int_key, sizeof(int_key)); |
| 117 | Slice key_slice(key_buf, arraysize(key_buf)); |
| 118 | auto h(cache_->Lookup(key_slice, Cache::EXPECT_IN_CACHE)); |
| 119 | if (h) { |
| 120 | ++hits; |
| 121 | } else { |
| 122 | auto ph(cache_->Allocate( |
| 123 | key_slice, /* val_len=*/kEntrySize, /* charge=*/kEntrySize)); |
| 124 | cache_->Insert(std::move(ph), nullptr); |
| 125 | } |
| 126 | ++lookups; |
| 127 | } |
| 128 | return {hits, lookups}; |
| 129 | } |
| 130 | |
| 131 | // Starts the given number of threads to concurrently call DoQueries. |
| 132 | // Returns the aggregated number of cache hits and lookups. |