redisLoadTest/redisHandler.go

87 lines
2.1 KiB
Go
Raw Permalink Normal View History

2022-01-05 00:33:34 +00:00
package main
import (
"fmt"
"github.com/gomodule/redigo/redis"
"time"
)
/**
newPool builds redis database connection
@param host string host:port of redis server
returns redis.Connection of connection to database
returns err from database connection
*/
func newPool(host string, username string, password string) *redis.Pool {
return &redis.Pool{
MaxIdle: 12000,
2022-01-05 00:33:34 +00:00
MaxActive: 12000,
Dial: func() (redis.Conn, error) {
c, err := redis.Dial("tcp", host)
if err != nil {
panic(err.Error())
}
if username != "" && password != "" {
_, err = c.Do("AUTH", password)
if err != nil {
c.Close()
panic(err.Error())
}
}
return c, err
},
TestOnBorrow: func(c redis.Conn, t time.Time) error {
_, err := c.Do("PING")
return err
},
}
}
/**
setRateTest sets key/value into database
@param rdbPtr connection is a pointer to the redis connection
@param key int redis key for the storage
@param value string string for the value to store
*/
func setRedis(rdbPtr *redis.Conn, key int, value string) {
2022-01-05 00:33:34 +00:00
rdb := *rdbPtr
//defer waitGroup.Done()
_, err := rdb.Do("Set", key, value)
if err != nil {
fmt.Println(err, key, value)
panic(err)
}
}
/**
buildTestData iterates through a goroutine of setRedis to build test data into the redis db
@param redisPtr connection to redis
@param size int number of entries into the database
*/
func buildTestData(rdbPtr *redis.Conn, size int) {
fmt.Println("Initializing Test Data...")
for i := 1; i < size; i++ {
2022-01-05 00:33:34 +00:00
key := i
value := "ThisIsATestStringThatShouldBeReplacedWithSomethingMoreRandomMaybeFromAnInputOrCSV"
setRedis(rdbPtr, key, value)
2022-01-05 00:33:34 +00:00
}
fmt.Println("Database Initialized...")
}
/**
getRedis queries the redis database for value of key
@param rdbPtr connection is a pointer to the redis connection
@param key int is key value to query in database
*/
func getRedis(rdbPtr *redis.Conn, key int, timesChan chan time.Duration, errorChan chan bool) {
2022-01-05 00:33:34 +00:00
rdb := *rdbPtr
startTime := time.Now()
2022-01-05 00:33:34 +00:00
_, err := rdb.Do("Get", key)
responseTime := time.Now().Sub(startTime)
2022-01-05 00:33:34 +00:00
if err != nil {
errorChan <- true
2022-01-05 00:33:34 +00:00
fmt.Println("Unhandled error:", err)
}
timesChan <- responseTime
2022-01-05 00:33:34 +00:00
}