k8s.io/test-infra@v0.0.0-20240520184403-27c6b4c223d8/experiment/dummybenchmarks/dummy_test.go (about)

     1  /*
     2  Copyright 2019 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package dummybenchmarks
    18  
    19  import "testing"
    20  
    21  // TestDontRun is for validating that we are not running tests with benchmarks.
    22  func TestDontRun(t *testing.T) {
    23  	t.Log("This is a Test not a Benchmark!")
    24  }
    25  
    26  // The first group of benchmarks in this file include "Core" in their names so
    27  // that they may be selected with a regexp when running integration tests.
    28  // This allows us to avoid running all the benchmarks to reduce testing time.
    29  
    30  func BenchmarkCoreSimple(b *testing.B) {
    31  	for i := 0; i < b.N; i++ {
    32  		DoTheThing()
    33  	}
    34  }
    35  
    36  func BenchmarkCoreAllocsAndBytes(b *testing.B) {
    37  	b.ReportAllocs()
    38  	for i := 0; i < b.N; i++ {
    39  		DoTheThing()
    40  		b.SetBytes(20)
    41  	}
    42  }
    43  
    44  func BenchmarkCoreParallel(b *testing.B) {
    45  	b.RunParallel(func(pb *testing.PB) {
    46  		for pb.Next() {
    47  			DoTheThing()
    48  		}
    49  	})
    50  }
    51  
    52  func BenchmarkCoreLog(b *testing.B) {
    53  	b.Logf("About to DoTheThing() x%d.", b.N)
    54  	for i := 0; i < b.N; i++ {
    55  		DoTheThing()
    56  	}
    57  }
    58  
    59  func BenchmarkCoreSkip(b *testing.B) {
    60  	b.Skip("This Benchmark is skipped.")
    61  }
    62  
    63  func BenchmarkCoreSkipNow(b *testing.B) {
    64  	b.SkipNow()
    65  }
    66  
    67  func BenchmarkCoreError(b *testing.B) {
    68  	b.Error("Early Benchmark error.")
    69  	BenchmarkCoreLog(b)
    70  }
    71  
    72  func BenchmarkCoreFatal(b *testing.B) {
    73  	b.Fatal("This Benchmark failed.")
    74  }
    75  
    76  func BenchmarkCoreFailNow(b *testing.B) {
    77  	b.FailNow()
    78  }
    79  
    80  func BenchmarkCoreNestedShallow(b *testing.B) {
    81  	b.Run("simple", BenchmarkCoreSimple)
    82  	b.Run("parallel", BenchmarkCoreParallel)
    83  }
    84  
    85  // The following benchmarks produce output to check additional edge cases, that
    86  // are already somewhat covered by the preceding benchmarks.
    87  // They do not include "Core" so that they may be omitted when running
    88  // integration tests to reduce runtimes.
    89  
    90  func Benchmark(b *testing.B) {
    91  	BenchmarkCoreSimple(b)
    92  }
    93  
    94  func BenchmarkReportAllocs(b *testing.B) {
    95  	b.ReportAllocs()
    96  	for i := 0; i < b.N; i++ {
    97  		DoTheThing()
    98  	}
    99  }
   100  
   101  func BenchmarkSetBytes(b *testing.B) {
   102  	for i := 0; i < b.N; i++ {
   103  		DoTheThing()
   104  		b.SetBytes(20)
   105  	}
   106  }
   107  
   108  func BenchmarkNestedDeep(b *testing.B) {
   109  	b.Run("1", func(b1 *testing.B) {
   110  		b.Run("1 simple", BenchmarkCoreSimple)
   111  		b.Run("1 parallel", BenchmarkCoreParallel)
   112  
   113  		b.Run("2", func(b2 *testing.B) {
   114  			b.Run("3A", func(b3 *testing.B) {
   115  				b.Run("3A simple", BenchmarkCoreSimple)
   116  			})
   117  			b.Run("3B", func(b3 *testing.B) {
   118  				b.Run("3B parallel", BenchmarkCoreParallel)
   119  			})
   120  		})
   121  	})
   122  }