//go:build integration package postgres_test import ( "context" "fmt" "sort" "sync/atomic" "testing" "time" "github.com/google/uuid" "github.com/stretchr/testify/require" "mtusi-messenger/internal/domain" "mtusi-messenger/internal/platform/cache" storageredis "mtusi-messenger/internal/storage/redis" ) func TestCacheLatencyExperiment(t *testing.T) { suite := new(BaseSuite) suite.SetT(t) suite.SetupTest() defer suite.TearDownTest() ctx := context.Background() redisConfig := cache.RedisConfig{ Host: envOr("TEST_REDIS_HOST", "localhost"), Port: envInt("TEST_REDIS_PORT", 6379), Password: envOr("TEST_REDIS_PASSWORD", ""), DB: envInt("TEST_REDIS_DB", 1), DialTimeout: 5 * time.Second, ReadTimeout: 3 * time.Second, WriteTimeout: 3 * time.Second, PoolSize: 5, } redisCache, err := cache.NewRedis(redisConfig) require.NoError(t, err) defer func() { _ = redisCache.Close() }() alice := suite.createUser("latency_alice") for range 20 { suite.createChat(alice) } source := &countingChatSource{source: suite.store.Chats()} for range 50 { _, err := source.ListVisible(ctx, alice.ID, nil, 50) require.NoError(t, err) } source.loads.Store(0) baseline := measureLatency(ctx, t, source, alice.ID, 1000) cached := storageredis.NewChatRepository( source, cache.WithSingleflight(redisCache), time.Minute, nil, ) source.loads.Store(0) cachedStats := measureCachedLatency(ctx, t, cached, alice.ID, 1000) cachedStats.Loads = int(source.loads.Load()) require.Equal(t, 1000, baseline.Loads) require.Equal(t, 1, cachedStats.Loads) t.Logf("baseline: %s", formatLatency(baseline)) t.Logf("redis cache: %s", formatLatency(cachedStats)) } type countingChatSource struct { source interface { ListVisible(context.Context, uuid.UUID, *domain.Cursor, int) (domain.Page[domain.Chat], error) } loads atomic.Int32 } func (s *countingChatSource) Create(context.Context, domain.ChatDraft) (domain.Chat, error) { return domain.Chat{}, nil } func (s *countingChatSource) GetVisible(context.Context, uuid.UUID, uuid.UUID) (domain.Chat, error) { return domain.Chat{}, domain.ErrNotFound } func (s *countingChatSource) ListVisible( ctx context.Context, callerID uuid.UUID, after *domain.Cursor, limit int, ) (domain.Page[domain.Chat], error) { s.loads.Add(1) return s.source.ListVisible(ctx, callerID, after, limit) } func measureLatency( ctx context.Context, t *testing.T, source *countingChatSource, callerID uuid.UUID, requests int, ) latencyStats { t.Helper() values := make([]time.Duration, 0, requests) for range requests { started := time.Now() _, err := source.ListVisible(ctx, callerID, nil, 50) require.NoError(t, err) values = append(values, time.Since(started)) } return makeLatencyStats(values, int(source.loads.Load())) } func measureCachedLatency( ctx context.Context, t *testing.T, repository *storageredis.ChatRepository, callerID uuid.UUID, requests int, ) latencyStats { t.Helper() values := make([]time.Duration, 0, requests) for range requests { started := time.Now() _, err := repository.ListVisible(ctx, callerID, nil, 50) require.NoError(t, err) values = append(values, time.Since(started)) } return makeLatencyStats(values, 0) } type latencyStats struct { Requests int Loads int P50 time.Duration P95 time.Duration P99 time.Duration } func makeLatencyStats(values []time.Duration, loads int) latencyStats { sort.Slice(values, func(i, j int) bool { return values[i] < values[j] }) percentile := func(percent int) time.Duration { index := (len(values)*percent + 99) / 100 if index > 0 { index-- } return values[index] } return latencyStats{ Requests: len(values), Loads: loads, P50: percentile(50), P95: percentile(95), P99: percentile(99), } } func formatLatency(value latencyStats) string { return fmt.Sprintf( "requests=%d source_loads=%d p50=%s p95=%s p99=%s", value.Requests, value.Loads, value.P50, value.P95, value.P99, ) }