github.com/cockroachdb/tools@v0.0.0-20230222021103-a6d27438930d/cmd/benchcmp/benchcmp_test.go (about)

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