github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/m3ninx/index/segment/mem/terms_dict_bench_test.go (about)

     1  // Copyright (c) 2018 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 mem
    22  
    23  import (
    24  	"regexp"
    25  	"testing"
    26  
    27  	"github.com/m3db/m3/src/m3ninx/doc"
    28  	"github.com/m3db/m3/src/m3ninx/postings"
    29  	"github.com/m3db/m3/src/m3ninx/util"
    30  )
    31  
    32  var (
    33  	benchTermsDictField    = []byte("__name__")
    34  	benchTermsDictRegexp   = []byte("node_netstat_Tcp_.*")
    35  	benchTermsDictCompiled = regexp.MustCompile(string(benchTermsDictRegexp))
    36  )
    37  
    38  func BenchmarkTermsDict(b *testing.B) {
    39  	benchmarks := []struct {
    40  		name string
    41  		fn   func(docs []doc.Metadata, b *testing.B)
    42  	}{
    43  		{
    44  			name: "benchmark Insert",
    45  			fn:   benchmarkTermsDictInsert,
    46  		},
    47  		{
    48  			name: "benchmark MatchTerm",
    49  			fn:   benchmarkTermsDictMatchTerm,
    50  		},
    51  		{
    52  			name: "benchmark MatchRegex",
    53  			fn:   benchmarkTermsDictMatchRegex,
    54  		},
    55  	}
    56  
    57  	docs, err := util.ReadDocs("../../../util/testdata/node_exporter.json", 2000)
    58  	if err != nil {
    59  		b.Fatalf("unable to read documents for benchmarks: %v", err)
    60  	}
    61  
    62  	for _, bm := range benchmarks {
    63  		b.Run(bm.name, func(b *testing.B) {
    64  			bm.fn(docs, b)
    65  		})
    66  	}
    67  }
    68  
    69  func benchmarkTermsDictInsert(docs []doc.Metadata, b *testing.B) {
    70  	b.ReportAllocs()
    71  
    72  	for n := 0; n < b.N; n++ {
    73  		b.StopTimer()
    74  		dict := newTermsDict(NewOptions())
    75  		b.StartTimer()
    76  		for i, d := range docs {
    77  			for _, f := range d.Fields {
    78  				dict.Insert(f, postings.ID(i))
    79  			}
    80  		}
    81  	}
    82  }
    83  
    84  func benchmarkTermsDictMatchTerm(docs []doc.Metadata, b *testing.B) {
    85  	b.ReportAllocs()
    86  
    87  	dict := newTermsDict(NewOptions())
    88  	for i, d := range docs {
    89  		for _, f := range d.Fields {
    90  			dict.Insert(f, postings.ID(i))
    91  		}
    92  	}
    93  
    94  	b.ResetTimer()
    95  	for n := 0; n < b.N; n++ {
    96  		for _, d := range docs {
    97  			for _, f := range d.Fields {
    98  				dict.MatchTerm(f.Name, f.Value)
    99  			}
   100  		}
   101  	}
   102  }
   103  
   104  func benchmarkTermsDictMatchRegex(docs []doc.Metadata, b *testing.B) {
   105  	b.ReportAllocs()
   106  
   107  	dict := newTermsDict(NewOptions())
   108  	for i, d := range docs {
   109  		for _, f := range d.Fields {
   110  			dict.Insert(f, postings.ID(i))
   111  		}
   112  	}
   113  
   114  	b.ResetTimer()
   115  	for n := 0; n < b.N; n++ {
   116  		dict.MatchRegexp(benchTermsDictField, benchTermsDictCompiled)
   117  	}
   118  }