47 lines
1.1 KiB
Go
47 lines
1.1 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/gomodule/redigo/redis"
|
|
"math"
|
|
"math/rand"
|
|
"time"
|
|
)
|
|
|
|
/**
|
|
getRateTest calculates closest value for requests per milliseconds from inputted requests per second
|
|
Then starts goroutines for getRedis at that interval
|
|
@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 getRateTest(rdbPtr *redis.Conn, rate int, keyMaxValue int, duration time.Duration) {
|
|
durationChannel := make(chan bool, 1)
|
|
x := int(math.Abs(60 / float64(rate) * 10000000))
|
|
rateLimiter := time.Tick(time.Duration(x))
|
|
|
|
fmt.Println("Starting Test at", rateLimiter, "milliseconds per request...")
|
|
go func() {
|
|
for i := 0; i < int(duration.Seconds()); i++ {
|
|
<-time.Tick(time.Second)
|
|
}
|
|
durationChannel <- true
|
|
|
|
}()
|
|
end := false
|
|
for {
|
|
if end {
|
|
break
|
|
}
|
|
select {
|
|
case <-durationChannel:
|
|
fmt.Println("Closing Test...")
|
|
end = true
|
|
case <-rateLimiter:
|
|
key := rand.Intn(keyMaxValue)
|
|
go getRedis(rdbPtr, key)
|
|
}
|
|
}
|
|
fmt.Println("Rate test concluded...")
|
|
}
|