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