package bakeoff import ( "encoding/json" "fmt" "os/exec" "strconv" "time" ) // LoadResult is one oha run. type LoadResult struct { Scenario string Concurrency int RPS, P50Ms, P95Ms, P99Ms float64 ErrorRate float64 Status map[string]int `json:",omitempty"` // responses by status code Errors map[string]int `json:",omitempty"` // transport errors by message } // ParseOha reads oha's JSON output. Any status other than ok, and any // transport error except oha's own end-of-run deadline aborts, is an error. func ParseOha(b []byte, ok int) (LoadResult, error) { var o struct { Summary struct { RequestsPerSec float64 `json:"requestsPerSec"` } `json:"summary"` Latency map[string]float64 `json:"latencyPercentiles"` Status map[string]int `json:"statusCodeDistribution"` Errors map[string]int `json:"errorDistribution"` } if err := json.Unmarshal(b, &o); err != nil { return LoadResult{}, err } var good, bad int for code, n := range o.Status { if code == strconv.Itoa(ok) { good += n } else { bad += n } } errs := map[string]int{} for msg, n := range o.Errors { if msg != "aborted due to deadline" { bad += n errs[msg] = n } } r := LoadResult{ RPS: o.Summary.RequestsPerSec, P50Ms: o.Latency["p50"] * 1000, P95Ms: o.Latency["p95"] * 1000, P99Ms: o.Latency["p99"] * 1000, Status: o.Status, } if len(errs) > 0 { r.Errors = errs } if good+bad > 0 { r.ErrorRate = float64(bad) / float64(good+bad) } else { // Nothing completed in the whole step: the app stalled. oha still // reports a rate (from requests it aborted at the deadline). r.ErrorRate, r.RPS = 1, 0 if n := o.Errors["aborted due to deadline"]; n > 0 { r.Errors = map[string]int{"no response completed (aborted at the deadline)": n} } } return r, nil } // RunLoad drives the app with oha for d at the given concurrency. // "read" is GET / (200); "toggle" is POST /todos//toggle (303); // "read-after" is "read" again, run after the toggle ramp, to show any // slow-down left behind by heavy writes. func RunLoad(s *Session, scenario string, concurrency int, d time.Duration) (LoadResult, error) { args := []string{"--no-tui", "--output-format", "json", "-z", d.String(), "-c", strconv.Itoa(concurrency), "--redirect", "0", "-H", "Cookie: " + s.CookieHeader(), "-H", "Origin: " + s.origin()} ok := 200 target := s.Base.String() + "/" switch scenario { case "read", "read-after": case "toggle": if s.ToggleID == "" { return LoadResult{}, fmt.Errorf("toggle needs a list seeded by Seed") } ok = 303 target = s.Base.String() + "/todos/" + s.ToggleID + "/toggle" args = append(args, "-m", "POST", "-T", "application/x-www-form-urlencoded", "-d", "filter=all") default: return LoadResult{}, fmt.Errorf("unknown scenario %q", scenario) } out, err := exec.Command("oha", append(args, target)...).Output() if err != nil { return LoadResult{}, fmt.Errorf("oha: %w", err) } r, err := ParseOha(out, ok) r.Scenario, r.Concurrency = scenario, concurrency return r, err }