github.com/aclements/go-misc@v0.0.0-20240129233631-2f6ede80790c/bench/parse_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 bench
     6  
     7  import (
     8  	"bytes"
     9  	"reflect"
    10  	"testing"
    11  )
    12  
    13  func TestParse(t *testing.T) {
    14  	for _, test := range []struct {
    15  		input string
    16  		want  []*Benchmark
    17  	}{
    18  		// Test basic line.
    19  		{`
    20  BenchmarkX	1	2 ns/op 3 MB/s`,
    21  			[]*Benchmark{
    22  				{"X", 1, map[string]*Config{}, map[string]float64{"ns/op": 2, "MB/s": 3}},
    23  			},
    24  		},
    25  
    26  		// Test short name.
    27  		{`
    28  Benchmark	1	2 ns/op`,
    29  			[]*Benchmark{
    30  				{"", 1, map[string]*Config{}, map[string]float64{"ns/op": 2}},
    31  			},
    32  		},
    33  
    34  		// Test bad names.
    35  		{`
    36  Benchmarkx	1	2 ns/op
    37  benchmarkx	1	2 ns/op
    38  benchmarkX	1	2 ns/op`,
    39  			[]*Benchmark{},
    40  		},
    41  
    42  		// Test short lines.
    43  		{`
    44  BenchmarkX
    45  BenchmarkX	1
    46  BenchmarkX	1	2`,
    47  			[]*Benchmark{},
    48  		},
    49  
    50  		// Test -N.
    51  		{`
    52  BenchmarkX-4	1	2 ns/op`,
    53  			[]*Benchmark{
    54  				{"X", 1, map[string]*Config{
    55  					"gomaxprocs": &Config{"gomaxprocs", "4", "4", false},
    56  				}, map[string]float64{"ns/op": 2}},
    57  			},
    58  		},
    59  
    60  		// Test per-benchmark config.
    61  		{`
    62  BenchmarkX/a:20/b:abc	1	2 ns/op
    63  BenchmarkY/c:123	2	4 ns/op`,
    64  			[]*Benchmark{
    65  				{"X", 1, map[string]*Config{
    66  					"a": &Config{"a", "20", "20", false},
    67  					"b": &Config{"b", "abc", "abc", false},
    68  				}, map[string]float64{"ns/op": 2}},
    69  				{"Y", 2, map[string]*Config{
    70  					"c": &Config{"c", "123", "123", false},
    71  				}, map[string]float64{"ns/op": 4}},
    72  			},
    73  		},
    74  
    75  		// Test block config.
    76  		{`
    77  commit: 123456
    78  date: Jan 1
    79  colon:colon: 42
    80  blank:
    81  #not-config: x
    82  spa ce: x
    83  funny space: x
    84  Not-config: x
    85  BenchmarkX	1	2 ns/op`,
    86  			[]*Benchmark{
    87  				{"X", 1, map[string]*Config{
    88  					"commit":      &Config{"commit", "123456", "123456", true},
    89  					"date":        &Config{"date", "Jan 1", "Jan 1", true},
    90  					"colon:colon": &Config{"colon:colon", "42", "42", true},
    91  					"blank":       &Config{"blank", "", "", true},
    92  				}, map[string]float64{"ns/op": 2}},
    93  			},
    94  		},
    95  
    96  		// Test benchmark config overriding block config.
    97  		{`
    98  commit: 123456
    99  date: Jan 1
   100  BenchmarkX/commit:abcdef	1	2 ns/op`,
   101  			[]*Benchmark{
   102  				{"X", 1, map[string]*Config{
   103  					"commit": &Config{"commit", "abcdef", "abcdef", false},
   104  					"date":   &Config{"date", "Jan 1", "Jan 1", true},
   105  				}, map[string]float64{"ns/op": 2}},
   106  			},
   107  		},
   108  
   109  		// Test block config overriding block config.
   110  		{`
   111  commit: 123456
   112  commit: abcdef
   113  date: Jan 1
   114  BenchmarkX	1	2 ns/op`,
   115  			[]*Benchmark{
   116  				{"X", 1, map[string]*Config{
   117  					"commit": &Config{"commit", "abcdef", "abcdef", true},
   118  					"date":   &Config{"date", "Jan 1", "Jan 1", true},
   119  				}, map[string]float64{"ns/op": 2}},
   120  			},
   121  		},
   122  	} {
   123  		r := bytes.NewBufferString(test.input)
   124  		bs, err := Parse(r)
   125  		if err != nil {
   126  			t.Error("unexpected Parse error", err)
   127  			continue
   128  		}
   129  		if !reflect.DeepEqual(bs, test.want) {
   130  			t.Log("want:")
   131  			for _, b := range test.want {
   132  				t.Logf("%#v", b)
   133  			}
   134  			t.Log("got:")
   135  			for _, b := range bs {
   136  				t.Logf("%#v", b)
   137  			}
   138  			t.Fail()
   139  		}
   140  	}
   141  }