#include "include/encoding.h"
#include "include/common_fwd.h"
#include "common/ceph_time.h"
+#include "common/ceph_context.h"
+#include "common/perf_counters.h"
namespace rgw {
#include "common/perf_counters.h"
#include "rgw_usage_cache.h"
-class CephContext;
namespace rgw {
EXPECT_FALSE(stats.has_value());
}
-// Test concurrent access
-TEST_F(TestRGWUsageCache, ConcurrentAccess) {
- const int num_threads = 4;
- const int ops_per_thread = 100;
- std::vector<std::thread> threads;
- std::atomic<int> successful_updates(0);
- std::atomic<int> failed_updates(0);
-
- for (int t = 0; t < num_threads; ++t) {
- threads.emplace_back([this, t, ops_per_thread, &successful_updates, &failed_updates]() {
- for (int i = 0; i < ops_per_thread; ++i) {
- std::string user_id = "t" + std::to_string(t) + "_u" + std::to_string(i);
- int result = cache->update_user_stats(user_id, i * 1024, i);
- if (result == 0) {
- successful_updates++;
- } else {
- failed_updates++;
- }
-
- auto stats = cache->get_user_stats(user_id);
- if (result == 0) {
- EXPECT_TRUE(stats.has_value());
- }
- }
- });
- }
-
- for (auto& th : threads) {
- th.join();
- }
-
- std::cout << "Concurrent test: " << successful_updates << " successful, "
- << failed_updates << " failed updates" << std::endl;
-
- EXPECT_GT(successful_updates, 0) << "At least some updates should succeed";
- EXPECT_GT(cache->get_cache_size(), 0u) << "Cache should contain entries";
-}
-
// Test updating existing user stats
TEST_F(TestRGWUsageCache, UpdateExistingUserStats) {
const std::string user_id = "update_test_user";
EXPECT_EQ(2u, cache->get_cache_misses()); // 1 user miss + 1 bucket miss
}
-TEST_F(TestRGWUsagePerfCounters, ConcurrentCounterUpdates) {
- const int num_threads = 4;
- const int ops_per_thread = 100;
- std::vector<std::thread> threads;
-
- for (int t = 0; t < num_threads; ++t) {
- threads.emplace_back([this, t, ops_per_thread]() {
- for (int i = 0; i < ops_per_thread; ++i) {
- std::string user_id = "t" + std::to_string(t) + "_u" + std::to_string(i);
-
- // First access - miss
- cache->get_user_stats(user_id);
-
- // Update
- cache->update_user_stats(user_id, i * 1024, i);
-
- // Second access - hit
- cache->get_user_stats(user_id);
- }
- });
- }
-
- for (auto& t : threads) {
- t.join();
- }
-
- // Each thread does ops_per_thread users
- // Each user: 1 miss (first get), 1 hit (second get)
- uint64_t expected_total = num_threads * ops_per_thread;
- EXPECT_EQ(expected_total, cache->get_cache_hits());
- EXPECT_EQ(expected_total, cache->get_cache_misses());
-
- std::cout << "Concurrent test results:" << std::endl;
- std::cout << " Total operations: " << expected_total * 2 << std::endl;
- std::cout << " Hits: " << cache->get_cache_hits() << std::endl;
- std::cout << " Misses: " << cache->get_cache_misses() << std::endl;
-}
-
TEST_F(TestRGWUsagePerfCounters, ExpiredEntryTracking) {
// Add a user
ASSERT_EQ(0, cache->update_user_stats("expiry_test", 1024, 1));