Skip to content

Instantly share code, notes, and snippets.

@rfguri
Created October 13, 2018 12:03
Show Gist options
  • Save rfguri/ba0e2c58a6f8ed8b5671759389019517 to your computer and use it in GitHub Desktop.
Save rfguri/ba0e2c58a6f8ed8b5671759389019517 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"time"
"github.com/tsenart/vegeta/lib"
)
var target = vegeta.Target{
Method: "GET",
URL: "http://127.0.0.1:8080/api/v1/users/1",
}
func testRate(rate int, sla time.Duration) bool {
duration := 30 * time.Second
targeter := vegeta.NewStaticTargeter(target)
attacker := vegeta.NewAttacker()
var metrics vegeta.Metrics
for res := range attacker.Attack(targeter, uint64(rate), duration, "test") {
metrics.Add(res)
}
metrics.Close()
latency := metrics.Latencies.P95
if latency > sla || metrics.Success < 1 {
fmt.Printf("💥 Failed (%d) at %d req/sec (latency %s)\n", int(metrics.Success*100), rate, latency)
return false
}
fmt.Printf("✨ Success (%d) at %d req/sec (latency %s)\n", int(metrics.Success*100), rate, latency)
return true
}
func main() {
rate := 20
okRate := 1
var nokRate int
sla := 1 * time.Second
// first, find the point at which the system breaks
for {
if testRate(rate, sla) {
okRate = rate
rate *= 2
} else {
nokRate = rate
break
}
}
// next, do a binary search between okRate and nokRate
for (nokRate - okRate) > 1 {
rate = (nokRate + okRate) / 2
if testRate(rate, sla) {
okRate = rate
} else {
nokRate = rate
}
}
fmt.Printf("➡️ Maximum Working Rate: %d req/sec\n", okRate)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment