2025-03-05 18:12:26 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"math/rand"
|
|
|
|
|
"net/http"
|
|
|
|
|
"net/url"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
vegeta "github.com/tsenart/vegeta/lib"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func main() {
|
2025-03-06 10:30:54 +08:00
|
|
|
mock()
|
|
|
|
|
// attack()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func mock() {
|
|
|
|
|
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
waiting := rand.Intn(450) + 50
|
|
|
|
|
time.Sleep(time.Duration(waiting) * time.Millisecond)
|
|
|
|
|
w.Write([]byte("Hello World"))
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
err := http.ListenAndServe(":8080", nil)
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-03-05 18:12:26 +08:00
|
|
|
|
2025-03-06 10:30:54 +08:00
|
|
|
func attack() {
|
2025-03-05 18:12:26 +08:00
|
|
|
targeter := vegeta.NewStaticTargeter(vegeta.Target{
|
|
|
|
|
Method: "GET",
|
|
|
|
|
URL: "http://localhost:8080",
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
rate := vegeta.Rate{Freq: 500, Per: time.Second}
|
|
|
|
|
|
2025-03-06 10:30:54 +08:00
|
|
|
duration := 10 * time.Second
|
2025-03-05 18:12:26 +08:00
|
|
|
|
|
|
|
|
attacker := vegeta.NewAttacker()
|
|
|
|
|
|
|
|
|
|
vegeta.Proxy(func(request *http.Request) (*url.URL, error) {
|
|
|
|
|
return url.Parse("http://test-api.imfree.site:20001")
|
|
|
|
|
})(attacker)
|
|
|
|
|
|
|
|
|
|
result := attacker.Attack(targeter, rate, duration, "test")
|
|
|
|
|
|
|
|
|
|
maxNum := 0
|
|
|
|
|
for res := range result {
|
2025-03-06 10:30:54 +08:00
|
|
|
println(res.Latency)
|
2025-03-05 18:12:26 +08:00
|
|
|
}
|
|
|
|
|
println(maxNum)
|
|
|
|
|
}
|