2022-01-05 00:33:34 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/gomodule/redigo/redis"
|
2022-01-05 06:09:52 +00:00
|
|
|
"math"
|
2022-01-05 00:33:34 +00:00
|
|
|
"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)
|
2022-01-05 06:09:52 +00:00
|
|
|
burstRateLimiter := time.Tick(time.Millisecond * 250)
|
|
|
|
timesChan := make(chan time.Duration)
|
|
|
|
errorChan := make(chan bool)
|
|
|
|
endChan := make(chan bool)
|
|
|
|
totalErrorChan := make(chan int)
|
|
|
|
totalTimeChan := make(chan []int64)
|
|
|
|
quaterRate := math.Abs(float64(rate) / 4)
|
2022-01-05 00:33:34 +00:00
|
|
|
go func() {
|
|
|
|
for i := 0; i < int(duration.Seconds()); i++ {
|
|
|
|
<-time.Tick(time.Second)
|
|
|
|
}
|
|
|
|
durationChannel <- true
|
|
|
|
}()
|
2022-01-05 06:09:52 +00:00
|
|
|
|
|
|
|
go keepTotal(timesChan, errorChan, endChan, totalTimeChan, totalErrorChan)
|
|
|
|
fmt.Println("Starting burst test at", quaterRate, "requests per 250 milliseconds...")
|
2022-01-05 00:33:34 +00:00
|
|
|
end := false
|
|
|
|
for {
|
|
|
|
if end {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case <-durationChannel:
|
|
|
|
end = true
|
|
|
|
case <-burstRateLimiter:
|
2022-01-05 06:09:52 +00:00
|
|
|
for i := float64(0); i < quaterRate; i++ {
|
|
|
|
//this might need toggled
|
|
|
|
time.Sleep(time.Millisecond * 5)
|
2022-01-05 00:33:34 +00:00
|
|
|
key := rand.Intn(keyMaxValue)
|
2022-01-05 06:09:52 +00:00
|
|
|
go getRedis(rdbPtr, key, timesChan, errorChan)
|
2022-01-05 00:33:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-01-05 06:09:52 +00:00
|
|
|
|
|
|
|
endChan <- true
|
|
|
|
totalTimesFloat := <-totalTimeChan
|
|
|
|
totalErrors := <-totalErrorChan
|
|
|
|
errorRate := totalErrors / len(totalTimesFloat)
|
|
|
|
totalTimes := intToFloat(totalTimesFloat)
|
|
|
|
mean, stddev, perc99, perc95 := getResponseTimes(totalTimes)
|
2022-01-05 00:33:34 +00:00
|
|
|
fmt.Println("Burst test concluded...")
|
2022-01-05 06:09:52 +00:00
|
|
|
fmt.Println("Total Requests:", len(totalTimesFloat))
|
|
|
|
fmt.Println("Error Rate:", errorRate, "ms")
|
|
|
|
fmt.Println("Mean:", mean, "ms")
|
|
|
|
fmt.Println("Standard Deviation:", stddev, "ms")
|
|
|
|
fmt.Println("95 Percentile:", perc95, "ms")
|
|
|
|
fmt.Println("99 Percentile:", perc99, "ms")
|
2022-01-05 00:33:34 +00:00
|
|
|
}
|