benchmarkHSETOperationsConcurrent performs the actual HSET benchmark for a given scale
(b *testing.B, rdb *redis.Client, ctx context.Context, operations int)
| 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) { |
| 114 | hashKey := fmt.Sprintf("benchmark_hash_%d", operations) |
| 115 | |
| 116 | b.ResetTimer() |
| 117 | b.StartTimer() |
| 118 | totalTimes := []time.Duration{} |
| 119 | |
| 120 | for i := 0; i < b.N; i++ { |
| 121 | b.StopTimer() |
| 122 | // Clean up the hash before each iteration |
| 123 | rdb.Del(ctx, hashKey) |
| 124 | b.StartTimer() |
| 125 | |
| 126 | startTime := time.Now() |
| 127 | // Perform the specified number of HSET operations |
| 128 | |
| 129 | wg := sync.WaitGroup{} |
| 130 | timesCh := make(chan time.Duration, operations) |
| 131 | errCh := make(chan error, operations) |
| 132 | |
| 133 | for j := 0; j < operations; j++ { |
| 134 | wg.Add(1) |
| 135 | go func(j int) { |
| 136 | defer wg.Done() |
| 137 | field := fmt.Sprintf("field_%d", j) |
| 138 | value := fmt.Sprintf("value_%d", j) |
| 139 | |
| 140 | err := rdb.HSet(ctx, hashKey, field, value).Err() |
| 141 | if err != nil { |
| 142 | errCh <- err |
| 143 | return |
| 144 | } |
| 145 | timesCh <- time.Since(startTime) |
| 146 | }(j) |
| 147 | } |
| 148 | |
| 149 | wg.Wait() |
| 150 | close(timesCh) |
| 151 | close(errCh) |
| 152 | |
| 153 | // Check for errors |
| 154 | for err := range errCh { |
| 155 | b.Errorf("HSET operation failed: %v", err) |
| 156 | } |
| 157 | |
| 158 | for d := range timesCh { |
| 159 | totalTimes = append(totalTimes, d) |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | // Stop the timer to calculate metrics |
| 164 | b.StopTimer() |
| 165 | |
| 166 | // Report operations per second |
| 167 | opsPerSec := float64(operations*b.N) / b.Elapsed().Seconds() |
| 168 | b.ReportMetric(opsPerSec, "ops/sec") |
| 169 | |
| 170 | // Report average time per operation |