Unfinished changes

This commit is contained in:
vesem 2021-12-29 10:54:43 -05:00
parent 77a1ab2e8c
commit e39129618b
2 changed files with 49 additions and 17 deletions

5
LICENSE.md Normal file
View File

@ -0,0 +1,5 @@
"THE BEER-WARE LICENSE" (Revision 42)
<mitch@nerdfortress.dev> wrote this file. As long as you retain this notice you
can do whatever you want with this stuff. If we meet some day, and you think
this stuff is worth it, you can buy me a beer in return

View File

@ -1,5 +1,14 @@
package main
/*
* ----------------------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42):
* <mitch@nerdfortress.dev> wrote this file. As long as you retain this notice you
* can do whatever you want with this stuff. If we meet some day, and you think
* this stuff is worth it, you can buy me a beer in return
* ----------------------------------------------------------------------------
*/
import (
"flag"
"fmt"
@ -19,13 +28,13 @@ buildTestData iterates through a goroutine of setRedis to build test data into t
func buildTestData(rdbPtr *redis.Conn, size int) {
fmt.Println("Initializing Test Data...")
var waitGroup sync.WaitGroup
for i := 0; i < size; i++ {
for i := 1; i < 50000; i++ {
key := i
value := "ThisIsATestStringThatShouldBeReplacedWithSomethingMoreRandomMaybeFromAnInputOrCSV"
go setRedis(rdbPtr, key, value, &waitGroup)
waitGroup.Add(1)
setRedis(rdbPtr, key, value, &waitGroup)
//waitGroup.Add(1)
}
waitGroup.Wait()
//waitGroup.Wait()
fmt.Println("Database Initialized...")
}
@ -37,13 +46,15 @@ Then starts goroutines for getRedis at that interval
@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) {
var durationChannel chan bool
durationChannel := make(chan bool, 1)
x := int(math.Abs(60 / float64(rate) * 1000))
rateLimiter := time.Tick(time.Millisecond * time.Duration(x))
fmt.Println("Starting Test at", rateLimiter, "milliseconds per request...")
go func() {
for i := 0; i < int(duration); i++ {
for i := 0; i < int(duration.Seconds()); i++ {
fmt.Println(i, int(duration.Seconds()))
<-time.Tick(time.Second)
}
durationChannel <- true
@ -54,11 +65,13 @@ func getRateTest(rdbPtr *redis.Conn, rate int, keyMaxValue int, duration time.Du
break
}
select {
case <-durationChannel:
fmt.Println("Closing Test...")
end = true
case <-rateLimiter:
key := rand.Intn(keyMaxValue)
fmt.Println(key)
go getRedis(rdbPtr, key)
case <-durationChannel:
end = true
}
}
fmt.Println("Rate test concluded...")
@ -71,10 +84,10 @@ getBurstTest starts goroutines for getRedis at the burstRateLimit per second
@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) {
var durationChannel chan bool
durationChannel := make(chan bool, 1)
burstRateLimiter := time.Tick(time.Second)
go func() {
for i := 0; i < int(duration); i++ {
for i := 0; i < int(duration.Seconds()); i++ {
<-time.Tick(time.Second)
}
durationChannel <- true
@ -86,13 +99,13 @@ func getBurstTest(rdbPtr *redis.Conn, rate int, keyMaxValue int, duration time.D
break
}
select {
case <-durationChannel:
end = true
case <-burstRateLimiter:
for i := 0; i < rate; i++ {
key := rand.Intn(keyMaxValue)
go getRedis(rdbPtr, key)
}
case <-durationChannel:
end = true
}
}
fmt.Println("Burst test concluded...")
@ -106,12 +119,12 @@ setRateTest sets key/value into database
*/
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 {
defer waitGroup.Done()
fmt.Println(err, key, value)
panic(err)
}
defer waitGroup.Done()
}
/**
@ -147,7 +160,20 @@ func newPool(host string) *redis.Pool {
}
}
func keepTotal(timesSlice *[]float64, timesChan chan float64, endChan chan bool) {
func keepTotal(timesChan chan float64, errorChan chan bool, endChan chan bool) ([]float64, int) {
var timesSlice []float64
var errorRate int
for {
select {
case <-endChan:
break
case timeValue := <-timesChan:
timesSlice = append(timesSlice, timeValue)
case <-errorChan:
errorRate++
}
}
return timesSlice, errorRate
}
@ -173,8 +199,9 @@ func main() {
burstPtr := flag.Bool("burst", true, "Boolean burst test default true")
//todo this should be off that same csv rather than an input
maxEntriesPtr := flag.Int("dbEntries", 50000, "Test rate limit, default 50/sec")
testDurationPtr := flag.Int("duration", 60, "Duration of each test")
testDurationPtr := flag.Int("duration", 10, "Duration of each test")
flag.Parse()
host := *hostPtr
//todo add username/password support
//username := *usernamePtr
@ -191,7 +218,7 @@ func main() {
client := pool.Get()
defer client.Close()
if initializeDB {
if !initializeDB {
buildTestData(&client, 50000)
}
getRateTest(&client, rate, keyMaxValue, duration)