github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/metrics/policy/policy_benchmark_test.go (about)

     1  // Copyright (c) 2017 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package policy
    22  
    23  import (
    24  	"testing"
    25  	"time"
    26  
    27  	"github.com/m3db/m3/src/metrics/aggregation"
    28  	xtime "github.com/m3db/m3/src/x/time"
    29  )
    30  
    31  var (
    32  	testNowNanos = time.Now().UnixNano()
    33  	testPolicies = []Policy{
    34  		NewPolicy(NewStoragePolicy(10*time.Second, xtime.Second, 2*24*time.Hour), aggregation.DefaultID),
    35  		NewPolicy(NewStoragePolicy(time.Minute, xtime.Minute, 30*24*time.Hour), aggregation.DefaultID),
    36  	}
    37  )
    38  
    39  func BenchmarkStagedPoliciesAsStruct(b *testing.B) {
    40  	sp := NewStagedPolicies(testNowNanos, false, testPolicies)
    41  	for n := 0; n < b.N; n++ {
    42  		validatePolicyByValue(b, sp)
    43  	}
    44  }
    45  
    46  func BenchmarkStagedPoliciesAsPointer(b *testing.B) {
    47  	sp := NewStagedPolicies(testNowNanos, false, testPolicies)
    48  	for n := 0; n < b.N; n++ {
    49  		validatePolicyByPointer(b, &sp)
    50  	}
    51  }
    52  
    53  func BenchmarkStagedPoliciesAsInterface(b *testing.B) {
    54  	sp := &testStagedPolicies{cutoverNanos: testNowNanos, policies: testPolicies}
    55  	for n := 0; n < b.N; n++ {
    56  		validatePolicyByInterface(b, sp)
    57  	}
    58  }
    59  
    60  func BenchmarkStagedPoliciesAsStructExported(b *testing.B) {
    61  	sp := testStagedPolicies{cutoverNanos: testNowNanos, policies: testPolicies}
    62  	for n := 0; n < b.N; n++ {
    63  		validatePolicyByStructExported(b, sp)
    64  	}
    65  }
    66  
    67  type testStagedPoliciesInt64 interface {
    68  	CutoverNanos() int64
    69  }
    70  
    71  // StagedPolicies represent a list of policies at a specified version.
    72  type testStagedPolicies struct {
    73  	cutoverNanos int64
    74  	policies     []Policy
    75  }
    76  
    77  func (v testStagedPolicies) ValCutoverNanos() int64 {
    78  	return v.cutoverNanos
    79  }
    80  
    81  func (v *testStagedPolicies) CutoverNanos() int64 {
    82  	return v.cutoverNanos
    83  }
    84  
    85  func validatePolicyByValue(b *testing.B, sp StagedPolicies) {
    86  	if sp.CutoverNanos != testNowNanos {
    87  		b.FailNow()
    88  	}
    89  }
    90  
    91  func validatePolicyByPointer(b *testing.B, sp *StagedPolicies) {
    92  	if sp.CutoverNanos != testNowNanos {
    93  		b.FailNow()
    94  	}
    95  }
    96  
    97  func validatePolicyByInterface(b *testing.B, sp testStagedPoliciesInt64) {
    98  	if sp.CutoverNanos() != testNowNanos {
    99  		b.FailNow()
   100  	}
   101  }
   102  
   103  func validatePolicyByStructExported(b *testing.B, sp testStagedPolicies) {
   104  	if sp.ValCutoverNanos() != testNowNanos {
   105  		b.FailNow()
   106  	}
   107  }