HSET Benchmark Tests This file contains benchmark tests for Redis HSET operations with different scales: 1, 10, 100, 1000, 10000, 100000 operations Prerequisites: - Redis server running on localhost:6379 - No authentication required Usage: go test -bench=BenchmarkHSET -v ./hset_benchmark_test.go
(b *testing.B)
| 34 | |
| 35 | // BenchmarkHSET benchmarks HSET operations with different scales |
| 36 | func BenchmarkHSET(b *testing.B) { |
| 37 | ctx := context.Background() |
| 38 | |
| 39 | // Setup Redis client |
| 40 | rdb := redis.NewClient(&redis.Options{ |
| 41 | Addr: "localhost:6379", |
| 42 | DB: 0, |
| 43 | }) |
| 44 | defer rdb.Close() |
| 45 | |
| 46 | // Test connection |
| 47 | if err := rdb.Ping(ctx).Err(); err != nil { |
| 48 | b.Skipf("Redis server not available: %v", err) |
| 49 | } |
| 50 | |
| 51 | // Clean up before and after tests |
| 52 | defer func() { |
| 53 | rdb.FlushDB(ctx) |
| 54 | }() |
| 55 | |
| 56 | scales := []int{1, 10, 100, 1000, 10000, 100000} |
| 57 | |
| 58 | for _, scale := range scales { |
| 59 | b.Run(fmt.Sprintf("HSET_%d_operations", scale), func(b *testing.B) { |
| 60 | benchmarkHSETOperations(b, rdb, ctx, scale) |
| 61 | }) |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | // benchmarkHSETOperations performs the actual HSET benchmark for a given scale |
| 66 | func benchmarkHSETOperations(b *testing.B, rdb *redis.Client, ctx context.Context, operations int) { |