Files
mtusi2026lab4/internal/storage/postgres/cache_performance_experiment_test.go
T
2026-10-15 20:45:29 +03:00

193 lines
4.9 KiB
Go

//go:build integration
package postgres_test
import (
"context"
"fmt"
"sort"
"sync"
"sync/atomic"
"testing"
"time"
"github.com/google/uuid"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"mtusi-messenger/internal/domain"
"mtusi-messenger/internal/platform/cache"
storageredis "mtusi-messenger/internal/storage/redis"
"mtusi-messenger/internal/storage/redis/mocks"
)
func TestCachePerformanceExperiment(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", 2),
DialTimeout: 5 * time.Second,
ReadTimeout: 3 * time.Second,
WriteTimeout: 3 * time.Second,
PoolSize: 10,
}
redisCache, err := cache.NewRedis(redisConfig)
require.NoError(t, err)
defer func() { _ = redisCache.Close() }()
alice := suite.createUser("perf_alice")
bob := suite.createUser("perf_bob")
carol := suite.createUser("perf_carol")
for i := range 150 {
title := fmt.Sprintf("Chat %d with Alice", i+1)
members := []uuid.UUID{alice.ID}
if i%3 == 0 {
members = append(members, bob.ID)
}
if i%5 == 0 {
members = append(members, carol.ID)
}
_, err := suite.store.Chats().Create(ctx, domain.ChatDraft{
Title: title,
MemberIDs: members,
})
require.NoError(t, err)
}
realChats := suite.store.Chats()
var loads atomic.Int32
source := mocks.NewMockChatSource(t)
source.EXPECT().ListVersion(mock.Anything, mock.Anything).RunAndReturn(
func(ctx context.Context, userID uuid.UUID) (int64, error) {
return realChats.ListVersion(ctx, userID)
}).Maybe()
source.EXPECT().ListVisible(mock.Anything, mock.Anything, mock.Anything, mock.Anything).
RunAndReturn(func(ctx context.Context, callerID uuid.UUID, after *domain.Cursor, limit int) (domain.Page[domain.Chat], error) {
loads.Add(1)
return realChats.ListVisible(ctx, callerID, after, limit)
}).Maybe()
distributed, err := cache.NewDistributed(redisCache, cache.DistributedConfig{}, nil)
require.NoError(t, err)
cached := storageredis.NewChatRepository(
source,
distributed,
time.Minute,
nil,
)
baselineStats := benchmarkConcurrent(ctx, t, func(ctx context.Context) error {
_, err := source.ListVisible(ctx, alice.ID, nil, 50)
return err
}, 50, 200)
baselineStats.SQLQueries = int(loads.Load())
loads.Store(0)
cachedStats := benchmarkConcurrent(ctx, t, func(ctx context.Context) error {
_, err := cached.ListVisible(ctx, alice.ID, nil, 50)
return err
}, 50, 200)
cachedStats.SQLQueries = int(loads.Load())
loads.Store(0)
_, err = cached.ListVisible(ctx, alice.ID, nil, 50)
require.NoError(t, err)
cachedHotStats := benchmarkConcurrent(ctx, t, func(ctx context.Context) error {
_, err := cached.ListVisible(ctx, alice.ID, nil, 50)
return err
}, 50, 200)
cachedHotStats.SQLQueries = int(loads.Load())
t.Logf("baseline (no cache): %s", formatPerfStats(baselineStats))
t.Logf("redis cache (cold): %s", formatPerfStats(cachedStats))
t.Logf("redis cache (hot): %s", formatPerfStats(cachedHotStats))
}
type perfStats struct {
TotalRequests int
Concurrency int
SQLQueries int
Duration time.Duration
RPS float64
P50 time.Duration
P95 time.Duration
P99 time.Duration
}
func benchmarkConcurrent(
ctx context.Context,
t *testing.T,
operation func(context.Context) error,
concurrency int,
requestsPerWorker int,
) perfStats {
t.Helper()
totalRequests := concurrency * requestsPerWorker
latencies := make([]time.Duration, totalRequests)
var wg sync.WaitGroup
var failures atomic.Int32
started := time.Now()
for worker := range concurrency {
wg.Add(1)
go func(workerID int) {
defer wg.Done()
offset := workerID * requestsPerWorker
for i := range requestsPerWorker {
reqStart := time.Now()
err := operation(ctx)
if err != nil {
failures.Add(1)
}
latencies[offset+i] = time.Since(reqStart)
}
}(worker)
}
wg.Wait()
require.Zero(t, failures.Load(), "some requests failed")
duration := time.Since(started)
sort.Slice(latencies, func(i, j int) bool { return latencies[i] < latencies[j] })
percentile := func(p int) time.Duration {
index := (len(latencies)*p + 99) / 100
if index > 0 {
index--
}
return latencies[index]
}
return perfStats{
TotalRequests: totalRequests,
Concurrency: concurrency,
Duration: duration,
RPS: float64(totalRequests) / duration.Seconds(),
P50: percentile(50),
P95: percentile(95),
P99: percentile(99),
}
}
func formatPerfStats(s perfStats) string {
return fmt.Sprintf(
"requests=%d concurrency=%d sql=%d duration=%s rps=%.0f p50=%s p95=%s p99=%s",
s.TotalRequests, s.Concurrency, s.SQLQueries, s.Duration.Round(time.Millisecond),
s.RPS, s.P50, s.P95, s.P99,
)
}