@@ -392,7 +392,7 @@ type RunResult struct {
392392 RunID string `json:"run_id"`
393393 Mode string `json:"mode"`
394394 Routines int `json:"routines"`
395- OtherRoutes int `json:"other_routines"`
395+ OtherRoutines int `json:"other_routines"`
396396 ResultFile string `json:"result_file"`
397397 Status string `json:"status"`
398398 Error string `json:"error,omitempty"`
@@ -478,6 +478,26 @@ func (rc *ResultCollector) GetResults() []RunResult {
478478 return results
479479}
480480
481+ // benchResultFile represents the structure of a bench result JSON file.
482+ type benchResultFile struct {
483+ Metrics map [string ]benchMetric `json:"metrics"`
484+ }
485+
486+ type benchMetric struct {
487+ RatePerSec float64 `json:"rate_per_sec"`
488+ Success float64 `json:"success"`
489+ Errors float64 `json:"errors"`
490+ Timeouts float64 `json:"timeouts"`
491+ Latency benchLatency `json:"latency"`
492+ }
493+
494+ type benchLatency struct {
495+ P50 string `json:"p50"`
496+ P90 string `json:"p90"`
497+ P95 string `json:"p95"`
498+ P99 string `json:"p99"`
499+ }
500+
481501// LoadResultFile loads and parses a bench result file.
482502func LoadResultFile (path string ) (* ResultSummary , map [string ]interface {}, error ) {
483503 data , err := os .ReadFile (path )
@@ -490,40 +510,22 @@ func LoadResultFile(path string) (*ResultSummary, map[string]interface{}, error)
490510 return nil , nil , err
491511 }
492512
493- summary := & ResultSummary {}
513+ var result benchResultFile
514+ if err := json .Unmarshal (data , & result ); err != nil {
515+ return nil , nil , err
516+ }
494517
495- if metrics , ok := raw ["metrics" ].(map [string ]interface {}); ok {
496- for _ , v := range metrics {
497- if m , ok := v .(map [string ]interface {}); ok {
498- if rate , ok := m ["rate_per_sec" ].(float64 ); ok {
499- summary .Throughput = rate
500- }
501- if success , ok := m ["success" ].(float64 ); ok {
502- summary .Success = int64 (success )
503- }
504- if errors , ok := m ["errors" ].(float64 ); ok {
505- summary .Errors = int64 (errors )
506- }
507- if timeouts , ok := m ["timeouts" ].(float64 ); ok {
508- summary .Timeouts = int64 (timeouts )
509- }
510- if lat , ok := m ["latency" ].(map [string ]interface {}); ok {
511- if p50 , ok := lat ["p50" ].(string ); ok {
512- summary .P50 = p50
513- }
514- if p90 , ok := lat ["p90" ].(string ); ok {
515- summary .P90 = p90
516- }
517- if p95 , ok := lat ["p95" ].(string ); ok {
518- summary .P95 = p95
519- }
520- if p99 , ok := lat ["p99" ].(string ); ok {
521- summary .P99 = p99
522- }
523- }
524- break
525- }
526- }
518+ summary := & ResultSummary {}
519+ for _ , m := range result .Metrics {
520+ summary .Throughput = m .RatePerSec
521+ summary .Success = int64 (m .Success )
522+ summary .Errors = int64 (m .Errors )
523+ summary .Timeouts = int64 (m .Timeouts )
524+ summary .P50 = m .Latency .P50
525+ summary .P90 = m .Latency .P90
526+ summary .P95 = m .Latency .P95
527+ summary .P99 = m .Latency .P99
528+ break // Only need the first metric
527529 }
528530
529531 return summary , raw , nil
0 commit comments