redisLoadTest/redisHandler.go

85 lines
2.0 KiB
Go

package main
import (
"fmt"
"github.com/gomodule/redigo/redis"
"sync"
"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: 80,
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, waitGroup *sync.WaitGroup) {
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...")
var waitGroup sync.WaitGroup
for i := 1; i < 50000; i++ {
key := i
value := "ThisIsATestStringThatShouldBeReplacedWithSomethingMoreRandomMaybeFromAnInputOrCSV"
setRedis(rdbPtr, key, value, &waitGroup)
}
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) {
rdb := *rdbPtr
_, err := rdb.Do("Get", key)
if err != nil {
fmt.Println("Unhandled error:", err)
}
}