|
| 1 | +/* |
| 2 | + * Copyright (c) 2020 NetEase Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +/* |
| 18 | + * Project: curve |
| 19 | + * Created Date: 20231213 |
| 20 | + * Author: xuyifeng |
| 21 | + */ |
| 22 | + |
| 23 | +#include <gtest/gtest.h> |
| 24 | +#include <glog/logging.h> |
| 25 | +#include <cstdint> |
| 26 | + |
| 27 | +#include "src/common/lru_cache.h" |
| 28 | + |
| 29 | +namespace curve { |
| 30 | +namespace common { |
| 31 | + |
| 32 | +static void |
| 33 | +assert_cache_metrics(std::shared_ptr<ARCCache<int, int>> cache) { |
| 34 | + auto metrics = cache->GetCacheMetrics(); |
| 35 | + auto arcSize = cache->ArcSize(); |
| 36 | + /* sizeof(key) + sizeof(value), yet sizeof(int) + sizeof(value) */ |
| 37 | + auto sizeofKey = sizeof(int); |
| 38 | + auto sizeofValue = sizeof(int); |
| 39 | + ASSERT_EQ(arcSize.BSize() * sizeofKey + |
| 40 | + arcSize.TSize() * (sizeofKey + sizeofValue), |
| 41 | + metrics->cacheBytes.get_value()); |
| 42 | +} |
| 43 | + |
| 44 | +TEST(ArcTest, test_cache_create) { |
| 45 | + int maxCount = 5; |
| 46 | + auto cache = std::make_shared<ARCCache<int, int>>(maxCount, |
| 47 | + std::make_shared<CacheMetrics>("Cache")); |
| 48 | + |
| 49 | + ASSERT_EQ(cache->Capacity(), 5); |
| 50 | + ASSERT_EQ(cache->Size(), 0); |
| 51 | +} |
| 52 | + |
| 53 | +TEST(ArcTest, test_cache_put) { |
| 54 | + int maxCount = 5; |
| 55 | + auto cache = std::make_shared<ARCCache<int, int>>(maxCount, |
| 56 | + std::make_shared<CacheMetrics>("Cache")); |
| 57 | + auto metrics = cache->GetCacheMetrics(); |
| 58 | + |
| 59 | + for (int i = 0; i < maxCount; ++i) { |
| 60 | + cache->Put(i, i); |
| 61 | + } |
| 62 | + |
| 63 | + ASSERT_TRUE(cache->Size() == maxCount); |
| 64 | + |
| 65 | + int v; |
| 66 | + for (int i = 0; i < maxCount; ++i) { |
| 67 | + ASSERT_TRUE(cache->Get(i, &v)); |
| 68 | + ASSERT_EQ(v, i); |
| 69 | + } |
| 70 | + |
| 71 | + // run again trigger Touch() internally |
| 72 | + for (int i = 0; i < maxCount; ++i) { |
| 73 | + ASSERT_TRUE(cache->Get(i, &v)); |
| 74 | + ASSERT_EQ(v, i); |
| 75 | + } |
| 76 | + |
| 77 | + assert_cache_metrics(cache); |
| 78 | +} |
| 79 | + |
| 80 | +TEST(ArcTest, test_cache_getlast) { |
| 81 | + int maxCount = 5; |
| 82 | + auto cache = std::make_shared<ARCCache<int, int>>(maxCount, |
| 83 | + std::make_shared<CacheMetrics>("Cache")); |
| 84 | + auto metrics = cache->GetCacheMetrics(); |
| 85 | + |
| 86 | + for (int i = 0; i < maxCount; ++i) { |
| 87 | + cache->Put(i, i); |
| 88 | + } |
| 89 | + |
| 90 | + for (int i = 0; i < maxCount; ++i) { |
| 91 | + int k; |
| 92 | + ASSERT_TRUE(cache->GetLast(i, &k)); |
| 93 | + ASSERT_EQ(k, i); |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +TEST(ArcTest, test_cache_getlast2) { |
| 98 | + int maxCount = 5; |
| 99 | + auto cache = std::make_shared<ARCCache<int, int>>(maxCount, |
| 100 | + std::make_shared<CacheMetrics>("Cache")); |
| 101 | + auto metrics = cache->GetCacheMetrics(); |
| 102 | + |
| 103 | + for (int i = 0; i < maxCount; ++i) { |
| 104 | + cache->Put(i, i); |
| 105 | + } |
| 106 | + |
| 107 | + int k, v; |
| 108 | + ASSERT_TRUE(cache->GetLast(&k, &v)); |
| 109 | + ASSERT_EQ(k, 0); |
| 110 | + ASSERT_EQ(v, 0); |
| 111 | +} |
| 112 | + |
| 113 | +static bool filter_check_3(const int &v) { |
| 114 | + return v == 3; |
| 115 | +} |
| 116 | + |
| 117 | +TEST(ArcTest, test_cache_getlast3) { |
| 118 | + int maxCount = 5; |
| 119 | + auto cache = std::make_shared<ARCCache<int, int>>(maxCount, |
| 120 | + std::make_shared<CacheMetrics>("Cache")); |
| 121 | + auto metrics = cache->GetCacheMetrics(); |
| 122 | + |
| 123 | + for (int i = 0; i < maxCount; ++i) { |
| 124 | + cache->Put(i, i); |
| 125 | + } |
| 126 | + |
| 127 | + int k, v; |
| 128 | + ASSERT_TRUE(cache->GetLast(&k, &v, filter_check_3)); |
| 129 | + ASSERT_EQ(k, 3); |
| 130 | + ASSERT_EQ(v, 3); |
| 131 | +} |
| 132 | + |
| 133 | +TEST(ArcTest, test_cache_retire) { |
| 134 | + int maxCount = 5; |
| 135 | + auto cache = std::make_shared<ARCCache<int, int>>(maxCount, |
| 136 | + std::make_shared<CacheMetrics>("Cache")); |
| 137 | + auto metrics = cache->GetCacheMetrics(); |
| 138 | + |
| 139 | + for (int i = 0; i < maxCount+1; ++i) { |
| 140 | + cache->Put(i, i); |
| 141 | + } |
| 142 | + |
| 143 | + ASSERT_TRUE(cache->Size() == maxCount); |
| 144 | + |
| 145 | + int v; |
| 146 | + ASSERT_TRUE(cache->Get(0, &v) == false); |
| 147 | + for (int i = 1; i < maxCount+1; ++i) { |
| 148 | + ASSERT_TRUE(cache->Get(i, &v)); |
| 149 | + ASSERT_EQ(v, i); |
| 150 | + } |
| 151 | + |
| 152 | + int removed_count = 0; |
| 153 | + for (int i = 100; i < 200; ++i) { |
| 154 | + int removed; |
| 155 | + int ret = cache->Put(i, i, &removed); |
| 156 | + if (ret) { |
| 157 | + removed_count++; |
| 158 | + // This checks Arc cache's scan-resistent behavior, |
| 159 | + // first an item in t2 is eliminated, after that, |
| 160 | + // items in t1 is replaced |
| 161 | + ASSERT_TRUE(removed == 1 || removed >= 100); |
| 162 | + } |
| 163 | + (void)removed; |
| 164 | + } |
| 165 | + ASSERT_EQ(removed_count, (200 - 100)); |
| 166 | + |
| 167 | + auto s = cache->ArcSize(); |
| 168 | + ASSERT_TRUE(s.BSize() + s.TSize() <= 2 * maxCount); |
| 169 | + |
| 170 | + assert_cache_metrics(cache); |
| 171 | +} |
| 172 | + |
| 173 | +TEST(ArcTest, test_cache_remove) { |
| 174 | + int maxCount = 5; |
| 175 | + auto cache = std::make_shared<ARCCache<int, int>>(maxCount, |
| 176 | + std::make_shared<CacheMetrics>("Cache")); |
| 177 | + auto metrics = cache->GetCacheMetrics(); |
| 178 | + |
| 179 | + for (int i = 0; i < maxCount; ++i) { |
| 180 | + cache->Put(i, i); |
| 181 | + } |
| 182 | + |
| 183 | + cache->Remove(0); |
| 184 | + int v; |
| 185 | + ASSERT_FALSE(cache->Get(0, &v)); |
| 186 | + ASSERT_TRUE(cache->Size() == maxCount-1); |
| 187 | + |
| 188 | + for (int i = 1; i < maxCount; ++i) { |
| 189 | + ASSERT_TRUE(cache->Get(i, &v)); |
| 190 | + ASSERT_EQ(v, i); |
| 191 | + } |
| 192 | + |
| 193 | + for (int i = 100; i < 200; ++i) { |
| 194 | + cache->Put(i, i); |
| 195 | + } |
| 196 | + |
| 197 | + auto s = cache->ArcSize(); |
| 198 | + ASSERT_TRUE(s.BSize() + s.TSize() <= 2 * maxCount); |
| 199 | + assert_cache_metrics(cache); |
| 200 | +} |
| 201 | + |
| 202 | +} // namespace common |
| 203 | +} // namespace curve |
| 204 | + |
0 commit comments