|
22 | 22 | #include <sstream> |
23 | 23 | #include <vector> |
24 | 24 | #include <stdexcept> |
| 25 | +#include <algorithm> |
25 | 26 |
|
26 | 27 | #include <catch2/catch.hpp> |
27 | 28 | #include <theta_sketch.hpp> |
@@ -151,6 +152,130 @@ TEST_CASE("theta sketch: single item", "[theta_sketch]") { |
151 | 152 | REQUIRE(update_sketch.compact(false).is_ordered()); |
152 | 153 | } |
153 | 154 |
|
| 155 | +TEST_CASE("theta sketch: compact with trim, all four cases", "[theta_sketch]") { |
| 156 | + update_theta_sketch update_sketch = update_theta_sketch::builder().build(); |
| 157 | + for (int i = 0; i < 8000; i++) update_sketch.update(i); |
| 158 | + const uint32_t k = 1 << theta_constants::DEFAULT_LG_K; |
| 159 | + // an update sketch retains more than k between rebuilds, so trimming has work to do |
| 160 | + REQUIRE(update_sketch.get_num_retained() > k); |
| 161 | + const uint32_t retained_before = update_sketch.get_num_retained(); |
| 162 | + const uint64_t theta_before = update_sketch.get_theta64(); |
| 163 | + |
| 164 | + // the trimmed result is what trim() + compact() would produce |
| 165 | + update_theta_sketch trimmed = update_sketch; |
| 166 | + trimmed.trim(); |
| 167 | + compact_theta_sketch expected = trimmed.compact(true); |
| 168 | + const std::vector<uint64_t> expected_entries(expected.begin(), expected.end()); |
| 169 | + |
| 170 | + // case 1: ordered, not trimmed (the default) keeps every retained entry |
| 171 | + compact_theta_sketch c1 = update_sketch.compact(true, false); |
| 172 | + REQUIRE(c1.is_ordered()); |
| 173 | + REQUIRE(c1.get_num_retained() == retained_before); |
| 174 | + REQUIRE(c1.get_theta64() == theta_before); |
| 175 | + REQUIRE(std::is_sorted(c1.begin(), c1.end())); |
| 176 | + |
| 177 | + // case 2: unordered, not trimmed |
| 178 | + compact_theta_sketch c2 = update_sketch.compact(false, false); |
| 179 | + REQUIRE_FALSE(c2.is_ordered()); |
| 180 | + REQUIRE(c2.get_num_retained() == retained_before); |
| 181 | + REQUIRE(c2.get_theta64() == theta_before); |
| 182 | + |
| 183 | + // case 3: ordered and trimmed |
| 184 | + compact_theta_sketch c3 = update_sketch.compact(true, true); |
| 185 | + REQUIRE(c3.is_ordered()); |
| 186 | + REQUIRE(c3.get_num_retained() == k); |
| 187 | + REQUIRE(c3.get_theta64() < theta_before); // new theta is the k-th smallest hash |
| 188 | + REQUIRE(c3.get_theta64() == expected.get_theta64()); |
| 189 | + REQUIRE(std::is_sorted(c3.begin(), c3.end())); |
| 190 | + REQUIRE(std::vector<uint64_t>(c3.begin(), c3.end()) == expected_entries); |
| 191 | + // every retained hash is strictly below the new theta |
| 192 | + for (auto h: c3) REQUIRE(h < c3.get_theta64()); |
| 193 | + |
| 194 | + // case 4: unordered and trimmed: same set and theta, no sort |
| 195 | + compact_theta_sketch c4 = update_sketch.compact(false, true); |
| 196 | + REQUIRE_FALSE(c4.is_ordered()); |
| 197 | + REQUIRE(c4.get_num_retained() == k); |
| 198 | + REQUIRE(c4.get_theta64() == expected.get_theta64()); |
| 199 | + std::vector<uint64_t> c4_entries(c4.begin(), c4.end()); |
| 200 | + std::sort(c4_entries.begin(), c4_entries.end()); |
| 201 | + REQUIRE(c4_entries == expected_entries); |
| 202 | + |
| 203 | + // the source sketch must be untouched by any of the four |
| 204 | + REQUIRE(update_sketch.get_num_retained() == retained_before); |
| 205 | + REQUIRE(update_sketch.get_theta64() == theta_before); |
| 206 | +} |
| 207 | + |
| 208 | +TEST_CASE("theta sketch: compact with trim widens the bounds in estimation mode", "[theta_sketch]") { |
| 209 | + // trimming is lossy even when the source is already estimating: it discards |
| 210 | + // retained entries, and the relative error scales with 1 / sqrt(retained) |
| 211 | + const uint32_t k = 1 << theta_constants::DEFAULT_LG_K; |
| 212 | + update_theta_sketch sketch = update_theta_sketch::builder().build(); |
| 213 | + for (int i = 0; i < 40000; i++) sketch.update(i); |
| 214 | + REQUIRE(sketch.is_estimation_mode()); |
| 215 | + REQUIRE(sketch.get_num_retained() > k); |
| 216 | + |
| 217 | + compact_theta_sketch plain = sketch.compact(true, false); |
| 218 | + compact_theta_sketch trimmed = sketch.compact(true, true); |
| 219 | + REQUIRE(trimmed.get_num_retained() == k); |
| 220 | + REQUIRE(plain.get_num_retained() > trimmed.get_num_retained()); |
| 221 | + |
| 222 | + // fewer retained entries -> strictly wider confidence interval |
| 223 | + const double plain_width = plain.get_upper_bound(2) - plain.get_lower_bound(2); |
| 224 | + const double trimmed_width = trimmed.get_upper_bound(2) - trimmed.get_lower_bound(2); |
| 225 | + REQUIRE(trimmed_width > plain_width); |
| 226 | +} |
| 227 | + |
| 228 | +TEST_CASE("theta sketch: compact with trim converts exact mode to estimation", "[theta_sketch]") { |
| 229 | + // an update sketch can retain far more than k entries while still in exact mode: |
| 230 | + // nothing has been evicted yet, so theta is still 1.0 and the count is exact |
| 231 | + const uint32_t k = 1 << theta_constants::DEFAULT_LG_K; |
| 232 | + update_theta_sketch sketch = update_theta_sketch::builder().build(); |
| 233 | + const int n = 5000; |
| 234 | + for (int i = 0; i < n; i++) sketch.update(i); |
| 235 | + REQUIRE(sketch.get_num_retained() > k); |
| 236 | + REQUIRE_FALSE(sketch.is_estimation_mode()); |
| 237 | + REQUIRE(sketch.get_theta() == 1.0); |
| 238 | + |
| 239 | + // not trimming keeps every entry and the exact count |
| 240 | + compact_theta_sketch exact = sketch.compact(true, false); |
| 241 | + REQUIRE_FALSE(exact.is_estimation_mode()); |
| 242 | + REQUIRE(exact.get_num_retained() == static_cast<uint32_t>(n)); |
| 243 | + REQUIRE(exact.get_estimate() == Approx(n)); |
| 244 | + |
| 245 | + // trimming is lossy: it discards real data, lowers theta below 1.0 and the |
| 246 | + // result is an estimate carrying error where the source held an exact count |
| 247 | + compact_theta_sketch trimmed = sketch.compact(true, true); |
| 248 | + REQUIRE(trimmed.is_estimation_mode()); |
| 249 | + REQUIRE(trimmed.get_num_retained() == k); |
| 250 | + REQUIRE(trimmed.get_theta() < 1.0); |
| 251 | + REQUIRE(trimmed.get_estimate() != Approx(n)); |
| 252 | + // the estimate is still sound: n must lie inside the 3-sigma bounds |
| 253 | + REQUIRE(trimmed.get_lower_bound(3) <= n); |
| 254 | + REQUIRE(trimmed.get_upper_bound(3) >= n); |
| 255 | +} |
| 256 | + |
| 257 | +TEST_CASE("theta sketch: compact with trim, empty and below k", "[theta_sketch]") { |
| 258 | + // empty: trimming changes nothing |
| 259 | + update_theta_sketch empty_sketch = update_theta_sketch::builder().build(); |
| 260 | + compact_theta_sketch empty_result = empty_sketch.compact(true, true); |
| 261 | + REQUIRE(empty_result.is_empty()); |
| 262 | + REQUIRE(empty_result.get_num_retained() == 0); |
| 263 | + REQUIRE(empty_result.is_ordered()); |
| 264 | + |
| 265 | + // below k: nothing to trim, theta and entries are preserved |
| 266 | + update_theta_sketch small = update_theta_sketch::builder().build(); |
| 267 | + for (int i = 0; i < 100; i++) small.update(i); |
| 268 | + REQUIRE_FALSE(small.is_estimation_mode()); |
| 269 | + |
| 270 | + compact_theta_sketch small_result = small.compact(true, true); |
| 271 | + REQUIRE_FALSE(small_result.is_estimation_mode()); |
| 272 | + REQUIRE(small_result.get_num_retained() == 100); |
| 273 | + REQUIRE(small_result.get_theta64() == small.get_theta64()); |
| 274 | + REQUIRE(small_result.get_estimate() == Approx(100.0)); |
| 275 | + REQUIRE(std::is_sorted(small_result.begin(), small_result.end())); |
| 276 | + REQUIRE_FALSE(small.compact(false, true).is_ordered()); |
| 277 | +} |
| 278 | + |
154 | 279 | TEST_CASE("theta sketch: resize exact", "[theta_sketch]") { |
155 | 280 | update_theta_sketch update_sketch = update_theta_sketch::builder().build(); |
156 | 281 | for (int i = 0; i < 2000; i++) update_sketch.update(i); |
|
0 commit comments