Skip to content

Instantly share code, notes, and snippets.

@joshdurbin
Created October 8, 2019 12:46
Show Gist options
  • Save joshdurbin/25f8bab40ab49a992293c46487b93ff2 to your computer and use it in GitHub Desktop.
Save joshdurbin/25f8bab40ab49a992293c46487b93ff2 to your computer and use it in GitHub Desktop.
package main
import (
"flag"
"github.com/thoas/go-funk"
"log"
"time"
)
type result struct {
time int
}
func main() {
tc := flag.Int("threads", 100, "Thread Count")
rut := flag.Int("rampup", 30, "Ramp up time in seconds")
et := flag.Int("etime", 1, "Execution time in minutes")
flag.Parse()
//Check if execution time is more than ramp up time
if *et*60 < *rut {
log.Fatalln("Total execution time needs to be more than ramp up time")
}
waitTime := *rut / *tc
log.Printf("Execution will happen with %d users with a ramp up time of %d seconds for %d minutes\n", *tc, *rut, *et)
timeout := time.After(time.Duration(*et*60) * time.Second)
rchan := make(chan result, 1000)
routineCount := 0
go func(r chan result) {
for {
value := <-r
log.Printf("producer slept for %v", value.time)
}
}(rchan)
ProcessLoop:
for {
select {
case <-timeout:
log.Printf("Execution completed")
break ProcessLoop
case <-time.After(time.Duration(waitTime) * time.Second):
log.Printf("Spinning up another go routine")
go func(r chan result) {
for {
randomInt := funk.RandomInt(1, 100)
randomSleepTimeIn := time.Duration(randomInt)
time.Sleep(randomSleepTimeIn * time.Millisecond)
r <- result {
time: randomInt,
}
}
}(rchan)
}
}
close(rchan)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment