Created
September 20, 2021 06:10
-
-
Save leosunmo/e9122665fda978d4db4f5d76b06914af to your computer and use it in GitHub Desktop.
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 ( | |
"fmt" | |
"math/rand" | |
"net/http" | |
"os" | |
"time" | |
vegeta "github.com/tsenart/vegeta/lib" | |
"github.com/tsenart/vegeta/lib/plot" | |
) | |
type attacker struct { | |
seq uint64 | |
} | |
func main() { | |
var metrics vegeta.Metrics | |
var results vegeta.Results | |
p := plot.New( | |
plot.Label(plotLabeller), | |
plot.Title("Test Plot")) | |
a := attacker{} | |
for i := 0; i < 100; i++ { | |
res := a.do() | |
metrics.Add(res) | |
results.Add(res) | |
p.Add(res) | |
} | |
metrics.Close() | |
results.Close() | |
f, err := os.Create("plot.html") | |
if err != nil { | |
panic(err) | |
} | |
p.WriteTo(f) | |
} | |
func (a *attacker) do() *vegeta.Result { | |
timestamp := time.Now() | |
res := &vegeta.Result{} | |
var code uint16 | |
var resErr string = "OK" | |
////////////////////////////////////////////////////////// | |
// Do stuff, like call the API or whatever | |
////////////////////////////////////////////////////////// | |
rand.Seed(time.Now().UnixNano()) | |
r := rand.Intn(100-0) + 1 | |
if r < 30 { | |
code = http.StatusInternalServerError | |
resErr = "ERROR" | |
} else { | |
code = http.StatusOK | |
} | |
bi := rand.Intn(1024 - 200) | |
bo := rand.Intn(1024 - 200) | |
lat := rand.Intn(100 - 20) | |
resLat, err := time.ParseDuration(fmt.Sprintf("%dms", lat)) | |
if err != nil { | |
panic("invalid duration") | |
} | |
time.Sleep(resLat) | |
////////////////////////////////////////////////////////// | |
// Collect results | |
////////////////////////////////////////////////////////// | |
res.Code = code | |
res.BytesIn = uint64(bi) | |
res.BytesOut = uint64(bo) | |
res.Error = resErr | |
res.Latency = time.Since(timestamp) | |
res.Timestamp = timestamp | |
res.Seq = a.seq | |
a.seq++ | |
return res | |
} | |
func plotLabeller(r *vegeta.Result) (label string) { | |
return r.Error | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment