gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/tools/parsers/go_parser_test.go (about)

     1  // Copyright 2020 The gVisor Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package parsers
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/google/go-cmp/cmp"
    21  	"gvisor.dev/gvisor/tools/bigquery"
    22  )
    23  
    24  func TestParseLine(t *testing.T) {
    25  	testCases := []struct {
    26  		name string
    27  		data string
    28  		want *bigquery.Benchmark
    29  	}{
    30  		{
    31  			name: "Iperf",
    32  			data: "BenchmarkIperf/Upload-6 1	11094914892 ns/op	4751711232 bandwidth.bytes_per_second",
    33  			want: &bigquery.Benchmark{
    34  				Name: "BenchmarkIperf",
    35  				Condition: []*bigquery.Condition{
    36  					{
    37  						Name:  "iterations",
    38  						Value: "1",
    39  					},
    40  					{
    41  						Name:  "GOMAXPROCS",
    42  						Value: "6",
    43  					},
    44  					{
    45  						Name:  "Upload",
    46  						Value: "Upload",
    47  					},
    48  				},
    49  				Metric: []*bigquery.Metric{
    50  					{
    51  						Name:   "ns/op",
    52  						Unit:   "ns/op",
    53  						Sample: 11094914892.0,
    54  					},
    55  					{
    56  						Name:   "bandwidth",
    57  						Unit:   "bytes_per_second",
    58  						Sample: 4751711232.0,
    59  					},
    60  				},
    61  			},
    62  		},
    63  		{
    64  			name: "Ruby",
    65  			data: "BenchmarkRuby/server_threads.1-6 1	1397875880 ns/op	0.00710 average_latency.s 140 requests_per_second.QPS",
    66  			want: &bigquery.Benchmark{
    67  				Name: "BenchmarkRuby",
    68  				Condition: []*bigquery.Condition{
    69  					{
    70  						Name:  "iterations",
    71  						Value: "1",
    72  					},
    73  					{
    74  						Name:  "GOMAXPROCS",
    75  						Value: "6",
    76  					},
    77  					{
    78  						Name:  "server_threads",
    79  						Value: "1",
    80  					},
    81  				},
    82  				Metric: []*bigquery.Metric{
    83  					{
    84  						Name:   "ns/op",
    85  						Unit:   "ns/op",
    86  						Sample: 1397875880.0,
    87  					},
    88  					{
    89  						Name:   "average_latency",
    90  						Unit:   "s",
    91  						Sample: 0.00710,
    92  					},
    93  					{
    94  						Name:   "requests_per_second",
    95  						Unit:   "QPS",
    96  						Sample: 140.0,
    97  					},
    98  				},
    99  			},
   100  		},
   101  	}
   102  
   103  	for _, tc := range testCases {
   104  		t.Run(tc.name, func(t *testing.T) {
   105  			got, err := parseLine(tc.data)
   106  			if err != nil {
   107  				t.Fatalf("parseLine failed with: %v", err)
   108  			}
   109  
   110  			if !cmp.Equal(tc.want, got, nil) {
   111  				for i := range got.Condition {
   112  					t.Logf("Metric: want: %+v got:%+v", got.Condition[i], tc.want.Condition[i])
   113  				}
   114  
   115  				for i := range got.Metric {
   116  					t.Logf("Metric: want: %+v got:%+v", got.Metric[i], tc.want.Metric[i])
   117  				}
   118  
   119  				t.Fatalf("Compare failed want: %+v got: %+v", tc.want, got)
   120  			}
   121  		})
   122  
   123  	}
   124  }
   125  
   126  func TestParseOutput(t *testing.T) {
   127  	testCases := []struct {
   128  		name          string
   129  		data          string
   130  		numBenchmarks int
   131  		numMetrics    int
   132  		numConditions int
   133  	}{
   134  		{
   135  			name: "Startup",
   136  			data: `
   137  				BenchmarkStartupEmpty
   138  				BenchmarkStartupEmpty-6                2         766377884 ns/op	1 allocs/op
   139  				BenchmarkStartupNode
   140  				BenchmarkStartupNode-6                 1        1752158409 ns/op	1 allocs/op
   141  			`,
   142  			numBenchmarks: 2,
   143  			numMetrics:    1,
   144  			numConditions: 2,
   145  		},
   146  		{
   147  			name: "Ruby",
   148  			data: `BenchmarkRuby
   149  BenchmarkRuby/server_threads.1
   150  BenchmarkRuby/server_threads.1-6 1	1397875880 ns/op 0.00710 average_latency.s 140 requests_per_second.QPS
   151  BenchmarkRuby/server_threads.5
   152  BenchmarkRuby/server_threads.5-6 1	1416003331 ns/op	0.00950 average_latency.s 465 requests_per_second.QPS`,
   153  			numBenchmarks: 2,
   154  			numMetrics:    3,
   155  			numConditions: 3,
   156  		},
   157  	}
   158  
   159  	for _, tc := range testCases {
   160  		t.Run(tc.name, func(t *testing.T) {
   161  			suite, err := ParseOutput(tc.data, "", false)
   162  			if err != nil {
   163  				t.Fatalf("parseOutput failed: %v", err)
   164  			} else if len(suite.Benchmarks) != tc.numBenchmarks {
   165  				t.Fatalf("NumBenchmarks failed want: %d got: %d %+v", tc.numBenchmarks, len(suite.Benchmarks), suite.Benchmarks)
   166  			}
   167  
   168  			for _, bm := range suite.Benchmarks {
   169  				if len(bm.Metric) != tc.numMetrics {
   170  					t.Fatalf("NumMetrics failed want: %d got: %d %+v", tc.numMetrics, len(bm.Metric), bm.Metric)
   171  				}
   172  
   173  				if len(bm.Condition) != tc.numConditions {
   174  					t.Fatalf("NumConditions failed want: %d got: %d %+v", tc.numConditions, len(bm.Condition), bm.Condition)
   175  				}
   176  			}
   177  		})
   178  	}
   179  }