Last active
August 8, 2020 17:26
-
-
Save simcap/5f58272fa5406d773f17 to your computer and use it in GitHub Desktop.
Vegeta example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bufio" | |
"encoding/json" | |
"fmt" | |
"net/url" | |
"net/http" | |
"os" | |
"time" | |
vegeta "github.com/tsenart/vegeta/lib" | |
) | |
func main() { | |
rate := uint64(1) // per second | |
duration := 100 * time.Second | |
file, err := os.Open("./mpdurls.txt") | |
if err != nil { | |
fmt.Printf("url file: %s", err) | |
os.Exit(1) | |
} | |
defer file.Close() | |
scanner := bufio.NewScanner(file) | |
targets := []*vegeta.Target{} | |
headers := http.Header{} | |
headers.Add("Authorization", "Bearer f7msgaTxh4Wero9442kT6vFieteMVDRchzElxJ0AqtA=") | |
headers.Add("Content-Type", "application/x-www-form-urlencoded") | |
for scanner.Scan() { | |
data := url.Values{} | |
data.Set("url", scanner.Text()) | |
data.Set("ttl", "2") | |
t := &vegeta.Target{Method: "POST", URL: "http://localhost:5555/manifests", Body: []byte(data.Encode()), Header: headers} | |
targets = append(targets, t) | |
} | |
targeter := vegeta.NewStaticTargeter(targets...) | |
attacker := vegeta.NewAttacker() | |
var results vegeta.Results | |
for res := range attacker.Attack(targeter, rate, duration) { | |
results = append(results, res) | |
} | |
metrics := vegeta.NewMetrics(results) | |
content, _ := json.MarshalIndent(metrics, "", " ") | |
fmt.Printf("%s\n", content) | |
fmt.Printf("Status codes: 500 %d, 200 %d\n", metrics.StatusCodes["500"], metrics.StatusCodes["200"]) | |
fmt.Printf("Requests: %d, successes %f\n", metrics.Requests, metrics.Success) | |
fmt.Printf("99th percentile: %s\n", metrics.Latencies.P99) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment