reintroduce unit test
parent
3c46aa1aee
commit
b6410050ae
|
|
@ -35,20 +35,20 @@ const (
|
|||
|
||||
var (
|
||||
// For testing
|
||||
collectTimeMinikubeStart = timeMinikubeStart
|
||||
collectTimeMinikubeStart = collectTimes
|
||||
)
|
||||
|
||||
// CompareMinikubeStart compares the time to run `minikube start` between two minikube binaries
|
||||
func CompareMinikubeStart(ctx context.Context, out io.Writer, binaries []*Binary) error {
|
||||
durations, err := collectTimes(ctx, binaries)
|
||||
durations, err := collectTimeMinikubeStart(ctx, binaries)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for i, d := range durations {
|
||||
fmt.Printf("Results for %s:\n", binaries[i].Name())
|
||||
fmt.Printf("Times: %v\n", d)
|
||||
fmt.Printf("Average Time: %f\n\n", average(d))
|
||||
fmt.Fprintf(out, "Results for %s:\n", binaries[i].Name())
|
||||
fmt.Fprintf(out, "Times: %v\n", d)
|
||||
fmt.Fprintf(out, "Average Time: %f\n\n", average(d))
|
||||
}
|
||||
|
||||
return nil
|
||||
|
|
@ -63,7 +63,7 @@ func collectTimes(ctx context.Context, binaries []*Binary) ([][]float64, error)
|
|||
for r := 0; r < runs; r++ {
|
||||
log.Printf("Executing run %d...", r)
|
||||
for index, binary := range binaries {
|
||||
duration, err := collectTimeMinikubeStart(ctx, binary)
|
||||
duration, err := timeMinikubeStart(ctx, binary)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "timing run %d with %s", r, binary.Name())
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,9 +17,66 @@ limitations under the License.
|
|||
package perf
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
)
|
||||
|
||||
func mockCollectTimes(times [][]float64) func(ctx context.Context, binaries []*Binary) ([][]float64, error) {
|
||||
return func(ctx context.Context, binaries []*Binary) ([][]float64, error) {
|
||||
return times, nil
|
||||
}
|
||||
}
|
||||
|
||||
func TestCompareMinikubeStartOutput(t *testing.T) {
|
||||
binaries := []*Binary{
|
||||
{
|
||||
path: "minikube1",
|
||||
}, {
|
||||
path: "minikube2",
|
||||
},
|
||||
}
|
||||
tests := []struct {
|
||||
description string
|
||||
times [][]float64
|
||||
expected string
|
||||
}{
|
||||
{
|
||||
description: "standard run",
|
||||
times: [][]float64{{4.5, 6}, {1, 2}},
|
||||
expected: `Results for minikube1:
|
||||
Times: [4.5 6]
|
||||
Average Time: 5.250000
|
||||
|
||||
Results for minikube2:
|
||||
Times: [1 2]
|
||||
Average Time: 1.500000
|
||||
|
||||
`,
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.description, func(t *testing.T) {
|
||||
originalCollectTimes := collectTimes
|
||||
collectTimeMinikubeStart = mockCollectTimes(test.times)
|
||||
defer func() { collectTimeMinikubeStart = originalCollectTimes }()
|
||||
|
||||
buf := bytes.NewBuffer([]byte{})
|
||||
err := CompareMinikubeStart(context.Background(), buf, binaries)
|
||||
if err != nil {
|
||||
t.Fatalf("error comparing minikube start: %v", err)
|
||||
}
|
||||
|
||||
actual := buf.String()
|
||||
if diff := cmp.Diff(test.expected, actual); diff != "" {
|
||||
t.Errorf("machines mismatch (-want +got):\n%s", diff)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
func TestAverage(t *testing.T) {
|
||||
tests := []struct {
|
||||
description string
|
||||
|
|
|
|||
Loading…
Reference in New Issue