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

     1  // Copyright (c) 2021 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 metadata
    22  
    23  import (
    24  	"runtime"
    25  	"testing"
    26  
    27  	"github.com/m3db/m3/src/metrics/aggregation"
    28  	"github.com/m3db/m3/src/metrics/generated/proto/metricpb"
    29  	"github.com/m3db/m3/src/metrics/policy"
    30  )
    31  
    32  func isDefault(m StagedMetadatas) bool {
    33  	return m.IsDefault()
    34  }
    35  
    36  func BenchmarkMetadata_IsDefault(b *testing.B) {
    37  	m := StagedMetadatas{
    38  		StagedMetadata{
    39  			CutoverNanos: 0,
    40  			Tombstoned:   false,
    41  			Metadata: Metadata{
    42  				Pipelines: []PipelineMetadata{
    43  					{
    44  						AggregationID:   aggregation.DefaultID,
    45  						StoragePolicies: []policy.StoragePolicy{},
    46  					},
    47  				},
    48  			},
    49  		},
    50  	}
    51  	for i := 0; i < b.N; i++ {
    52  		if !isDefault(m) {
    53  			b.Fail()
    54  		}
    55  	}
    56  	runtime.KeepAlive(m)
    57  }
    58  
    59  func BenchmarkMetadata_FromProto(b *testing.B) {
    60  	var (
    61  		testAllPayload metricpb.StagedMetadatas
    62  		m              StagedMetadatas
    63  	)
    64  
    65  	testAllPayload.Metadatas = append(testAllPayload.Metadatas,
    66  		testSmallStagedMetadatasProto.Metadatas...)
    67  	testAllPayload.Metadatas = append(testAllPayload.Metadatas,
    68  		testLargeStagedMetadatasProto.Metadatas...)
    69  	testAllPayload.Metadatas = append(testAllPayload.Metadatas,
    70  		testSmallStagedMetadatasWithLargeStoragePoliciesProto.Metadatas...)
    71  
    72  	b.Run("large metadatas", func(b *testing.B) {
    73  		for i := 0; i < b.N; i++ {
    74  			if err := m.FromProto(testLargeStagedMetadatasProto); err != nil {
    75  				b.Fail()
    76  			}
    77  		}
    78  	})
    79  
    80  	b.Run("small metadatas", func(b *testing.B) {
    81  		for i := 0; i < b.N; i++ {
    82  			if err := m.FromProto(testSmallStagedMetadatasProto); err != nil {
    83  				b.Fail()
    84  			}
    85  		}
    86  	})
    87  
    88  	b.Run("storage policies", func(b *testing.B) {
    89  		for i := 0; i < b.N; i++ {
    90  			if err := m.FromProto(
    91  				testSmallStagedMetadatasWithLargeStoragePoliciesProto,
    92  			); err != nil {
    93  				b.Fail()
    94  			}
    95  		}
    96  	})
    97  
    98  	b.Run("all", func(b *testing.B) {
    99  		for i := 0; i < b.N; i++ {
   100  			if err := m.FromProto(testAllPayload); err != nil {
   101  				b.Fail()
   102  			}
   103  		}
   104  	})
   105  
   106  	b.Run("reference, large metadatas", func(b *testing.B) {
   107  		for i := 0; i < b.N; i++ {
   108  			if err := m.fromProto(testLargeStagedMetadatasProto); err != nil {
   109  				b.Fail()
   110  			}
   111  		}
   112  	})
   113  
   114  	b.Run("reference, small metadatas", func(b *testing.B) {
   115  		for i := 0; i < b.N; i++ {
   116  			if err := m.fromProto(testSmallStagedMetadatasProto); err != nil {
   117  				b.Fail()
   118  			}
   119  		}
   120  	})
   121  
   122  	b.Run("reference, storage policies", func(b *testing.B) {
   123  		for i := 0; i < b.N; i++ {
   124  			if err := m.fromProto(testSmallStagedMetadatasWithLargeStoragePoliciesProto); err != nil {
   125  				b.Fail()
   126  			}
   127  		}
   128  	})
   129  
   130  	b.Run("reference, all", func(b *testing.B) {
   131  		for i := 0; i < b.N; i++ {
   132  			if err := m.fromProto(testAllPayload); err != nil {
   133  				b.Fail()
   134  			}
   135  		}
   136  	})
   137  
   138  	runtime.KeepAlive(m)
   139  }