-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathcompare.go
125 lines (111 loc) · 3.39 KB
/
compare.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// Copyright © 2017 NAME HERE <EMAIL ADDRESS>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cmd
import (
"encoding/csv"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"strings"
"text/tabwriter"
"time"
"github.com/linuxkit/rtf/local"
"github.com/spf13/cobra"
)
// compareCmd represents the compare command
var compareCmd = &cobra.Command{
Use: "compare",
Short: "Compare test results",
Long: `compare takes one or more JSON files, generated by 'rtf run', and shows them side by side. It optionally creates a CSV file with the results.
Note, this currently requires the set of tests contained in the JSON files to be identical.`,
RunE: compare,
}
var (
csvCompare bool
)
func init() {
flags := compareCmd.Flags()
flags.BoolVarP(&csvCompare, "csv", "", false, "Generate a CSV file")
RootCmd.AddCommand(compareCmd)
}
func compare(_ *cobra.Command, args []string) error {
if len(args) == 0 {
return fmt.Errorf("missing files to compare")
}
var summaries []local.Summary
for _, fileName := range args {
data, err := ioutil.ReadFile(fileName)
if err != nil {
return err
}
var s local.Summary
if err := json.Unmarshal(data, &s); err != nil {
return fmt.Errorf("Failed to unmarshal %s: %v", fileName, err)
}
summaries = append(summaries, s)
}
tw := new(tabwriter.Writer)
tw.Init(os.Stdout, 0, 8, 0, '\t', 0)
cw := csv.NewWriter(os.Stdout)
heading := []string{"Name"}
if !csvCompare {
heading = append(heading, args...)
fmt.Fprintln(tw, strings.Join(heading, "\t"))
} else {
ids := []string{"ID"}
starts := []string{"Start Time"}
ends := []string{"End Time"}
oses := []string{"OS"}
hws := []string{"Hardware"}
for _, s := range summaries {
ids = append(ids, s.ID)
starts = append(starts, s.StartTime.Format(time.RFC3339))
ends = append(ends, s.EndTime.Format(time.RFC3339))
si := s.SystemInfo
oses = append(oses, fmt.Sprintf("%s %s (%s)", si.Name, si.Version, si.Arch))
hws = append(hws, fmt.Sprintf("%s CPU:%s %d GB", si.Model, si.CPU, si.Memory/(1024*1024*1024)))
heading = append(heading, "")
}
if err := cw.WriteAll([][]string{ids, starts, ends, oses, hws, heading}); err != nil {
return err
}
}
for i := range summaries[0].Results {
name := summaries[0].Results[i].Name
results := []string{name}
for j := range summaries {
r := summaries[j].Results[i]
if r.Name != name {
return fmt.Errorf("List of results for %s don't match %s: %s != %s", args[0], args[j], r.Name, name)
}
resStr := r.TestResult.Sprintf("%s (%s)",
local.TestResultNames[r.TestResult], fmt.Sprintf("%.2fs", r.Duration.Seconds()))
if r.TestResult == local.Pass && r.BenchmarkResult != "" {
resStr = r.BenchmarkResult
}
results = append(results, resStr)
}
if !csvCompare {
fmt.Fprintln(tw, strings.Join(results, "\t"))
} else {
if err := cw.Write(results); err != nil {
return err
}
}
}
tw.Flush()
cw.Flush()
return nil
}