github.com/graybobo/golang.org-package-offline-cache@v0.0.0-20200626051047-6608995c132f/x/tools/cmd/benchcmp/parse_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  	"strings"
    10  	"testing"
    11  )
    12  
    13  func TestParseLine(t *testing.T) {
    14  	cases := []struct {
    15  		line string
    16  		want *Bench
    17  		err  bool // expect an error
    18  	}{
    19  		{
    20  			line: "BenchmarkEncrypt	100000000	        19.6 ns/op",
    21  			want: &Bench{
    22  				Name: "BenchmarkEncrypt",
    23  				N:    100000000, NsOp: 19.6,
    24  				Measured: NsOp,
    25  			},
    26  		},
    27  		{
    28  			line: "BenchmarkEncrypt	100000000	        19.6 ns/op	 817.77 MB/s",
    29  			want: &Bench{
    30  				Name: "BenchmarkEncrypt",
    31  				N:    100000000, NsOp: 19.6, MbS: 817.77,
    32  				Measured: NsOp | MbS,
    33  			},
    34  		},
    35  		{
    36  			line: "BenchmarkEncrypt	100000000	        19.6 ns/op	 817.77",
    37  			want: &Bench{
    38  				Name: "BenchmarkEncrypt",
    39  				N:    100000000, NsOp: 19.6,
    40  				Measured: NsOp,
    41  			},
    42  		},
    43  		{
    44  			line: "BenchmarkEncrypt	100000000	        19.6 ns/op	 817.77 MB/s	       5 allocs/op",
    45  			want: &Bench{
    46  				Name: "BenchmarkEncrypt",
    47  				N:    100000000, NsOp: 19.6, MbS: 817.77, AllocsOp: 5,
    48  				Measured: NsOp | MbS | AllocsOp,
    49  			},
    50  		},
    51  		{
    52  			line: "BenchmarkEncrypt	100000000	        19.6 ns/op	 817.77 MB/s	       3 B/op	       5 allocs/op",
    53  			want: &Bench{
    54  				Name: "BenchmarkEncrypt",
    55  				N:    100000000, NsOp: 19.6, MbS: 817.77, BOp: 3, AllocsOp: 5,
    56  				Measured: NsOp | MbS | BOp | AllocsOp,
    57  			},
    58  		},
    59  		// error handling cases
    60  		{
    61  			line: "BenchPress	100	        19.6 ns/op", // non-benchmark
    62  			err: true,
    63  		},
    64  		{
    65  			line: "BenchmarkEncrypt	lots	        19.6 ns/op", // non-int iterations
    66  			err: true,
    67  		},
    68  		{
    69  			line: "BenchmarkBridge	100000000	        19.6 smoots", // unknown unit
    70  			want: &Bench{
    71  				Name: "BenchmarkBridge",
    72  				N:    100000000,
    73  			},
    74  		},
    75  		{
    76  			line: "PASS",
    77  			err:  true,
    78  		},
    79  	}
    80  
    81  	for _, tt := range cases {
    82  		have, err := ParseLine(tt.line)
    83  		if tt.err && err == nil {
    84  			t.Errorf("parsing line %q should have failed", tt.line)
    85  			continue
    86  		}
    87  		if !reflect.DeepEqual(have, tt.want) {
    88  			t.Errorf("parsed line %q incorrectly, want %v have %v", tt.line, tt.want, have)
    89  		}
    90  	}
    91  }
    92  
    93  func TestParseBenchSet(t *testing.T) {
    94  	// Test two things:
    95  	// 1. The noise that can accompany testing.B output gets ignored.
    96  	// 2. Benchmarks with the same name have their order preserved.
    97  	in := `
    98  		?   	crypto	[no test files]
    99  		PASS
   100  				pem_decrypt_test.go:17: test 4. %!s(x509.PEMCipher=5)
   101  			... [output truncated]
   102  
   103  		BenchmarkEncrypt	100000000	        19.6 ns/op
   104  		BenchmarkEncrypt	 5000000	       517 ns/op
   105  		=== RUN TestChunk
   106  		--- PASS: TestChunk (0.00 seconds)
   107  		--- SKIP: TestLinuxSendfile (0.00 seconds)
   108  			fs_test.go:716: skipping; linux-only test
   109  		BenchmarkReadRequestApachebench	 1000000	      2960 ns/op	  27.70 MB/s	     839 B/op	       9 allocs/op
   110  		BenchmarkClientServerParallel64	   50000	     59192 ns/op	    7028 B/op	      60 allocs/op
   111  		ok  	net/http	95.783s
   112  	`
   113  
   114  	want := BenchSet{
   115  		"BenchmarkReadRequestApachebench": []*Bench{
   116  			{
   117  				Name: "BenchmarkReadRequestApachebench",
   118  				N:    1000000, NsOp: 2960, MbS: 27.70, BOp: 839, AllocsOp: 9,
   119  				Measured: NsOp | MbS | BOp | AllocsOp,
   120  				ord:      2,
   121  			},
   122  		},
   123  		"BenchmarkClientServerParallel64": []*Bench{
   124  			{
   125  				Name: "BenchmarkClientServerParallel64",
   126  				N:    50000, NsOp: 59192, BOp: 7028, AllocsOp: 60,
   127  				Measured: NsOp | BOp | AllocsOp,
   128  				ord:      3,
   129  			},
   130  		},
   131  		"BenchmarkEncrypt": []*Bench{
   132  			{
   133  				Name: "BenchmarkEncrypt",
   134  				N:    100000000, NsOp: 19.6,
   135  				Measured: NsOp,
   136  				ord:      0,
   137  			},
   138  			{
   139  				Name: "BenchmarkEncrypt",
   140  				N:    5000000, NsOp: 517,
   141  				Measured: NsOp,
   142  				ord:      1,
   143  			},
   144  		},
   145  	}
   146  
   147  	have, err := ParseBenchSet(strings.NewReader(in))
   148  	if err != nil {
   149  		t.Fatalf("unexpected err during ParseBenchSet: %v", err)
   150  	}
   151  	if !reflect.DeepEqual(want, have) {
   152  		t.Errorf("parsed bench set incorrectly, want %v have %v", want, have)
   153  	}
   154  }
   155  
   156  func TestParseBenchSetBest(t *testing.T) {
   157  	// Test that -best mode takes best ns/op.
   158  	*best = true
   159  	defer func() {
   160  		*best = false
   161  	}()
   162  
   163  	in := `
   164  		Benchmark1 10 100 ns/op
   165  		Benchmark2 10 60 ns/op
   166  		Benchmark2 10 500 ns/op
   167  		Benchmark1 10 50 ns/op
   168  	`
   169  
   170  	want := BenchSet{
   171  		"Benchmark1": []*Bench{
   172  			{
   173  				Name: "Benchmark1",
   174  				N:    10, NsOp: 50, Measured: NsOp,
   175  				ord: 0,
   176  			},
   177  		},
   178  		"Benchmark2": []*Bench{
   179  			{
   180  				Name: "Benchmark2",
   181  				N:    10, NsOp: 60, Measured: NsOp,
   182  				ord: 1,
   183  			},
   184  		},
   185  	}
   186  
   187  	have, err := ParseBenchSet(strings.NewReader(in))
   188  	if err != nil {
   189  		t.Fatalf("unexpected err during ParseBenchSet: %v", err)
   190  	}
   191  	if !reflect.DeepEqual(want, have) {
   192  		t.Errorf("parsed bench set incorrectly, want %v have %v", want, have)
   193  	}
   194  }