benchmarkHSETOperations performs the actual HSET benchmark for a given scale
(b *testing.B, rdb *redis.Client, ctx context.Context, operations int)
| 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) { |
| 67 | hashKey := fmt.Sprintf("benchmark_hash_%d", operations) |
| 68 | |
| 69 | b.ResetTimer() |
| 70 | b.StartTimer() |
| 71 | totalTimes := []time.Duration{} |
| 72 | |
| 73 | for i := 0; i < b.N; i++ { |
| 74 | b.StopTimer() |
| 75 | // Clean up the hash before each iteration |
| 76 | rdb.Del(ctx, hashKey) |
| 77 | b.StartTimer() |
| 78 | |
| 79 | startTime := time.Now() |
| 80 | // Perform the specified number of HSET operations |
| 81 | for j := 0; j < operations; j++ { |
| 82 | field := fmt.Sprintf("field_%d", j) |
| 83 | value := fmt.Sprintf("value_%d", j) |
| 84 | |
| 85 | err := rdb.HSet(ctx, hashKey, field, value).Err() |
| 86 | if err != nil { |
| 87 | b.Fatalf("HSET operation failed: %v", err) |
| 88 | } |
| 89 | } |
| 90 | totalTimes = append(totalTimes, time.Since(startTime)) |
| 91 | } |
| 92 | |
| 93 | // Stop the timer to calculate metrics |
| 94 | b.StopTimer() |
| 95 | |
| 96 | // Report operations per second |
| 97 | opsPerSec := float64(operations*b.N) / b.Elapsed().Seconds() |
| 98 | b.ReportMetric(opsPerSec, "ops/sec") |
| 99 | |
| 100 | // Report average time per operation |
| 101 | avgTimePerOp := b.Elapsed().Nanoseconds() / int64(operations*b.N) |
| 102 | b.ReportMetric(float64(avgTimePerOp), "ns/op") |
| 103 | // report average time in milliseconds from totalTimes |
| 104 | sumTime := time.Duration(0) |
| 105 | for _, t := range totalTimes { |
| 106 | sumTime += t |
| 107 | } |
| 108 | avgTimePerOpMs := sumTime.Milliseconds() / int64(len(totalTimes)) |
| 109 | b.ReportMetric(float64(avgTimePerOpMs), "ms") |
| 110 | } |
| 111 | |
| 112 | // benchmarkHSETOperationsConcurrent performs the actual HSET benchmark for a given scale |
| 113 | func benchmarkHSETOperationsConcurrent(b *testing.B, rdb *redis.Client, ctx context.Context, operations int) { |
no test coverage detected