redisLoadTest/burstTest.go

43 lines
1001 B
Go
Raw Normal View History

2022-01-05 00:33:34 +00:00
package main
import (
"fmt"
"github.com/gomodule/redigo/redis"
"math/rand"
"time"
)
/**
getBurstTest starts goroutines for getRedis at the burstRateLimit per second
@param rdbPtr connection is a pointer to the redis connection
@param rate int requests per second for the test
@param keyMaxValue int max value to query the database and expect a return
*/
func getBurstTest(rdbPtr *redis.Conn, rate int, keyMaxValue int, duration time.Duration) {
durationChannel := make(chan bool, 1)
burstRateLimiter := time.Tick(time.Second)
go func() {
for i := 0; i < int(duration.Seconds()); i++ {
<-time.Tick(time.Second)
}
durationChannel <- true
}()
fmt.Println("Starting burst test at", rate, "requests per second...")
end := false
for {
if end {
break
}
select {
case <-durationChannel:
end = true
case <-burstRateLimiter:
for i := 0; i < rate; i++ {
key := rand.Intn(keyMaxValue)
go getRedis(rdbPtr, key)
}
}
}
fmt.Println("Burst test concluded...")
}