Skip to content

Commit

Permalink
Merge pull request #25 from bojand/report_date
Browse files Browse the repository at this point in the history
add date to report struct and add it to the JSON output
  • Loading branch information
bojand committed Aug 1, 2018
2 parents 2d0f23a + d78f87b commit cd8bfd0
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
16 changes: 16 additions & 0 deletions reporter.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ghz

import (
"encoding/json"
"sort"
"time"
)
Expand All @@ -23,6 +24,8 @@ type Reporter struct {

// Report holds the data for the full test
type Report struct {
Date time.Time `json:"date"`

Count uint64 `json:"count"`
Total time.Duration `json:"total"`
Average time.Duration `json:"average"`
Expand All @@ -38,6 +41,18 @@ type Report struct {
Details []ResultDetail `json:"details"`
}

// MarshalJSON is custom marshal for report to properly format the date
func (r Report) MarshalJSON() ([]byte, error) {
type Alias Report
return json.Marshal(&struct {
Date string `json:"date"`
*Alias
}{
Date: r.Date.Format(time.RFC3339),
Alias: (*Alias)(&r),
})
}

// LatencyDistribution holds latency distribution data
type LatencyDistribution struct {
Percentage int `json:"percentage"`
Expand Down Expand Up @@ -107,6 +122,7 @@ func (r *Reporter) Finalize(total time.Duration) *Report {
rps := float64(r.totalCount) / total.Seconds()

rep := &Report{
Date: time.Now(),
Count: r.totalCount,
Total: total,
Average: avgDuration,
Expand Down
28 changes: 28 additions & 0 deletions reporter_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package ghz

import (
"encoding/json"
"testing"
"time"

"github.com/stretchr/testify/assert"
)

func TestReport_MarshalJSON(t *testing.T) {
z, _ := time.Parse(time.RFC822Z, "02 Jan 06 15:04 -0700")
r := &Report{
Date: z,
Count: 1000,
Total: time.Duration(10) * time.Second,
Average: time.Duration(500) * time.Millisecond,
Fastest: time.Duration(10) * time.Millisecond,
Slowest: time.Duration(1000) * time.Millisecond,
Rps: 34567.89,
}

json, err := json.Marshal(&r)
assert.NoError(t, err)

expected := `{"date":"2006-01-02T15:04:00-07:00","count":1000,"total":10000000000,"average":500000000,"fastest":10000000,"slowest":1000000000,"rps":34567.89,"errorDistribution":null,"statusCodeDistribution":null,"latencyDistribution":null,"histogram":null,"details":null}`
assert.Equal(t, expected, string(json))
}

0 comments on commit cd8bfd0

Please sign in to comment.