Skip to content

Commit

Permalink
fix(reports): deserialize json documents
Browse files Browse the repository at this point in the history
Update the `FromReader()` function to focus on a JSON document's
`metrics` key and deserialize each test's results into a vegeta.Metrics
object.

Fixes hashicorp#206
  • Loading branch information
gsantos-hc committed Jan 17, 2025
1 parent 81a5e54 commit 84ba8bd
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions benchmarktests/reports.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,29 @@ type Reporter struct {
}

func FromReader(r io.Reader) (*Reporter, error) {
d := json.NewDecoder(r)
// Read the JSON document
jsonBytes, err := io.ReadAll(r)
if err != nil {
return nil, err
}

// Get the JSON document's `metrics` key
var jsonData map[string]json.RawMessage
if err := json.Unmarshal(jsonBytes, &jsonData); err != nil {
return nil, err
}

raw, ok := jsonData["metrics"]
if !ok {
return nil, fmt.Errorf("missing metrics key in JSON document")
}

// Unmarshal the `metrics` key into a map of string (test names) to results
m := make(map[string]*vegeta.Metrics)
if err := d.Decode(&m); err != nil {
if err := json.Unmarshal(raw, &m); err != nil {
return nil, err
}

rpt := newReporter(&TargetMulti{}, nil)
rpt.metrics = m
return rpt, nil
Expand Down

0 comments on commit 84ba8bd

Please sign in to comment.