github.com/jgbaldwinbrown/perf@v0.1.1/storage/benchfmt/benchfmt_test.go (about)

     1  // Copyright 2016 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 benchfmt
     6  
     7  import (
     8  	"bytes"
     9  	"fmt"
    10  	"reflect"
    11  	"strings"
    12  	"testing"
    13  
    14  	"golang.org/x/perf/internal/diff"
    15  )
    16  
    17  func readAllResults(t *testing.T, r *Reader) []*Result {
    18  	var out []*Result
    19  	for r.Next() {
    20  		out = append(out, r.Result())
    21  	}
    22  	if err := r.Err(); err != nil {
    23  		t.Fatal(err)
    24  	}
    25  	return out
    26  }
    27  
    28  func TestBenchmarkReader(t *testing.T) {
    29  	tests := []struct {
    30  		name, input string
    31  		want        []*Result
    32  	}{
    33  		{
    34  			"basic",
    35  			`key: value
    36  BenchmarkOne 1 ns/sec
    37  `,
    38  			[]*Result{{
    39  				Labels{"key": "value"},
    40  				Labels{"name": "One"},
    41  				2,
    42  				"BenchmarkOne 1 ns/sec",
    43  			}},
    44  		},
    45  		{
    46  			"two results with indexed and named subnames",
    47  			`key: value
    48  BenchmarkOne/foo/bar=1-2 1 ns/sec
    49  BenchmarkTwo 2 ns/sec
    50  `,
    51  			[]*Result{
    52  				{
    53  					Labels{"key": "value"},
    54  					Labels{"name": "One", "sub1": "foo", "bar": "1", "gomaxprocs": "2"},
    55  					2,
    56  					"BenchmarkOne/foo/bar=1-2 1 ns/sec",
    57  				},
    58  				{
    59  					Labels{"key": "value"},
    60  					Labels{"name": "Two"},
    61  					3,
    62  					"BenchmarkTwo 2 ns/sec",
    63  				},
    64  			},
    65  		},
    66  		{
    67  			"remove existing label",
    68  			`key: value
    69  key:
    70  BenchmarkOne 1 ns/sec
    71  `,
    72  			[]*Result{
    73  				{
    74  					Labels{},
    75  					Labels{"name": "One"},
    76  					3,
    77  					"BenchmarkOne 1 ns/sec",
    78  				},
    79  			},
    80  		},
    81  		{
    82  			"parse file headers",
    83  			`key: fixed
    84  
    85  key: haha
    86  BenchmarkOne 1 ns/sec
    87  `,
    88  			[]*Result{
    89  				{
    90  					Labels{"key": "fixed"},
    91  					Labels{"name": "One"},
    92  					4,
    93  					"BenchmarkOne 1 ns/sec",
    94  				},
    95  			},
    96  		},
    97  	}
    98  	for _, test := range tests {
    99  		t.Run(test.name, func(t *testing.T) {
   100  			r := NewReader(strings.NewReader(test.input))
   101  			have := readAllResults(t, r)
   102  			want := test.want
   103  			diff := ""
   104  			mismatch := false
   105  			for i := 0; i < len(have) || i < len(want); i++ {
   106  				if i < len(have) && i < len(want) && reflect.DeepEqual(have[i], want[i]) {
   107  					diff += fmt.Sprintf(" %+v\n", have[i])
   108  					continue
   109  				}
   110  				mismatch = true
   111  				if i < len(have) {
   112  					diff += fmt.Sprintf("-%+v\n", have[i])
   113  				}
   114  				if i < len(want) {
   115  					diff += fmt.Sprintf("+%+v\n", want[i])
   116  				}
   117  			}
   118  			if mismatch {
   119  				t.Errorf("wrong results: (- have/+ want)\n%s", diff)
   120  			}
   121  		})
   122  	}
   123  }
   124  
   125  func TestBenchmarkPrinter(t *testing.T) {
   126  	tests := []struct {
   127  		name, input, want string
   128  	}{
   129  		{
   130  			"basic",
   131  			`key: value
   132  BenchmarkOne 1 ns/sec
   133  `,
   134  			`key: value
   135  BenchmarkOne 1 ns/sec
   136  `,
   137  		},
   138  		{
   139  			"missing newline",
   140  			`key: value
   141  BenchmarkOne 1 ns/sec`,
   142  			`key: value
   143  BenchmarkOne 1 ns/sec
   144  `,
   145  		},
   146  		{
   147  			"duplicate and removed fields",
   148  			`one: 1
   149  two: 2
   150  BenchmarkOne 1 ns/sec
   151  one: 1
   152  two: 3
   153  BenchmarkOne 1 ns/sec
   154  two:
   155  BenchmarkOne 1 ns/sec
   156  `,
   157  			`one: 1
   158  two: 2
   159  BenchmarkOne 1 ns/sec
   160  two: 3
   161  BenchmarkOne 1 ns/sec
   162  two:
   163  BenchmarkOne 1 ns/sec
   164  `,
   165  		},
   166  	}
   167  	for _, test := range tests {
   168  		t.Run(test.name, func(t *testing.T) {
   169  			r := NewReader(strings.NewReader(test.input))
   170  			results := readAllResults(t, r)
   171  			var have bytes.Buffer
   172  			bp := NewPrinter(&have)
   173  			for _, result := range results {
   174  				if err := bp.Print(result); err != nil {
   175  					t.Errorf("Print returned %v", err)
   176  				}
   177  			}
   178  			if diff := diff.Diff(have.String(), test.want); diff != "" {
   179  				t.Errorf("wrong output: (- got/+ want)\n%s", diff)
   180  			}
   181  		})
   182  	}
   183  }