Use Duration's string representation in JSON output

This commit is contained in:
Alan Bernstein 2016-12-08 15:06:02 -06:00
parent 40bf1347f9
commit 7dfb53d08e
2 changed files with 22 additions and 1 deletions

View file

@ -118,7 +118,7 @@ func (sb *serialBenchmark) Init(hosts []string, agentNum int) error {
// and the values are the results of each benchmark's Run method.
func (sb *serialBenchmark) Run(ctx context.Context, agentNum int) map[string]interface{} {
results := make(map[string]interface{}, len(sb.benchmarkers))
runtimes := make(map[string]time.Duration)
runtimes := make(map[string]interface{})
total_start := time.Now()
for i, b := range sb.benchmarkers {
start := time.Now()
@ -139,3 +139,23 @@ func Serial(bs ...Benchmark) Benchmark {
benchmarkers: bs,
}
}
type PrettyDuration time.Duration
func (d PrettyDuration) MarshalJSON() ([]byte, error) {
s := time.Duration(d).String()
return []byte("\"" + s + "\""), nil
}
func Prettify(m map[string]interface{}) map[string]interface{} {
newmap := make(map[string]interface{})
for k, v := range m {
switch v.(type) {
case time.Duration:
newmap[k] = PrettyDuration(v.(time.Duration))
default:
newmap[k] = Prettify(v.(map[string]interface{}))
}
}
return newmap
}

View file

@ -1226,6 +1226,7 @@ func (cmd *BagentCommand) Run(ctx context.Context) error {
res := sbm.Run(ctx, cmd.AgentNum)
enc := json.NewEncoder(cmd.Stdout)
enc.SetIndent("", " ")
res = bench.Prettify(res)
err = enc.Encode(res)
if err != nil {
fmt.Fprintln(cmd.Stderr, err)