github.com/april1989/origin-go-tools@v0.0.32/cmd/benchcmp/benchcmp_test.go (about)

     1  package main
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  
     7  	"github.com/april1989/origin-go-tools/benchmark/parse"
     8  )
     9  
    10  func TestSelectBest(t *testing.T) {
    11  	have := parse.Set{
    12  		"Benchmark1": []*parse.Benchmark{
    13  			{
    14  				Name: "Benchmark1",
    15  				N:    10, NsPerOp: 100, Measured: parse.NsPerOp,
    16  				Ord: 0,
    17  			},
    18  			{
    19  				Name: "Benchmark1",
    20  				N:    10, NsPerOp: 50, Measured: parse.NsPerOp,
    21  				Ord: 3,
    22  			},
    23  		},
    24  		"Benchmark2": []*parse.Benchmark{
    25  			{
    26  				Name: "Benchmark2",
    27  				N:    10, NsPerOp: 60, Measured: parse.NsPerOp,
    28  				Ord: 1,
    29  			},
    30  			{
    31  				Name: "Benchmark2",
    32  				N:    10, NsPerOp: 500, Measured: parse.NsPerOp,
    33  				Ord: 2,
    34  			},
    35  		},
    36  	}
    37  
    38  	want := parse.Set{
    39  		"Benchmark1": []*parse.Benchmark{
    40  			{
    41  				Name: "Benchmark1",
    42  				N:    10, NsPerOp: 50, Measured: parse.NsPerOp,
    43  				Ord: 0,
    44  			},
    45  		},
    46  		"Benchmark2": []*parse.Benchmark{
    47  			{
    48  				Name: "Benchmark2",
    49  				N:    10, NsPerOp: 60, Measured: parse.NsPerOp,
    50  				Ord: 1,
    51  			},
    52  		},
    53  	}
    54  
    55  	selectBest(have)
    56  	if !reflect.DeepEqual(want, have) {
    57  		t.Errorf("filtered bench set incorrectly, want %v have %v", want, have)
    58  	}
    59  }
    60  
    61  func TestFormatNs(t *testing.T) {
    62  	tests := []struct {
    63  		input    float64
    64  		expected string
    65  	}{
    66  		{input: 0, expected: "0.00"},
    67  		{input: 0.2, expected: "0.20"},
    68  		{input: 2, expected: "2.00"},
    69  		{input: 2.2, expected: "2.20"},
    70  		{input: 4, expected: "4.00"},
    71  		{input: 16, expected: "16.0"},
    72  		{input: 16.08, expected: "16.1"},
    73  		{input: 128, expected: "128"},
    74  		{input: 256.2, expected: "256"},
    75  	}
    76  
    77  	for _, tt := range tests {
    78  		actual := formatNs(tt.input)
    79  		if actual != tt.expected {
    80  			t.Fatalf("%f. got %q, want %q", tt.input, actual, tt.expected)
    81  		}
    82  	}
    83  }