Skip to content

Instantly share code, notes, and snippets.

@jamesdube
Created February 9, 2022 16:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamesdube/4af90d3fafc677173cd3499cf40d9256 to your computer and use it in GitHub Desktop.
Save jamesdube/4af90d3fafc677173cd3499cf40d9256 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"time"
"github.com/tsenart/vegeta/lib"
)
func testRate(rate int, sla time.Duration) bool {
duration := 15 * time.Second
targeter := vegeta.NewStaticTargeter(vegeta.Target{
Method: "GET",
URL: "http://localhost:8755/",
})
attacker := vegeta.NewAttacker()
var metrics vegeta.Metrics
for res := range attacker.Attack(targeter, uint64(rate), duration) {
metrics.Add(res)
}
metrics.Close()
latency := metrics.Latencies.P95
if latency > sla {
fmt.Printf("💥 Failed at %d req/sec (latency %s)\n", rate, latency)
return false
}
fmt.Printf("✨ Success at %d req/sec (latency %s)\n", 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