github.com/m3db/m3@v1.5.0/src/m3ninx/index/segment/mem/segment_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/util"
    29  )
    30  
    31  var (
    32  	benchSegmentField    = []byte("__name__")
    33  	benchSegmentRegexp   = []byte("node_netstat_Tcp_.*")
    34  	benchSegmentCompiled = regexp.MustCompile(string(benchSegmentRegexp))
    35  )
    36  
    37  func BenchmarkSegment(b *testing.B) {
    38  	benchmarks := []struct {
    39  		name string
    40  		fn   func(docs []doc.Metadata, b *testing.B)
    41  	}{
    42  		{
    43  			name: "benchmark Insert with segment",
    44  			fn:   benchmarkInsertSegment,
    45  		},
    46  		{
    47  			name: "benchmark matchTerm with segment",
    48  			fn:   benchmarkMatchTermSegment,
    49  		},
    50  		{
    51  			name: "benchmark matchRegex with segment",
    52  			fn:   benchmarkMatchRegexSegment,
    53  		},
    54  	}
    55  
    56  	docs, err := util.ReadDocs("../../../util/testdata/node_exporter.json", 2000)
    57  	if err != nil {
    58  		b.Fatalf("unable to read documents for benchmarks: %v", err)
    59  	}
    60  
    61  	for _, bm := range benchmarks {
    62  		b.Run(bm.name, func(b *testing.B) {
    63  			bm.fn(docs, b)
    64  		})
    65  	}
    66  }
    67  
    68  func benchmarkInsertSegment(docs []doc.Metadata, b *testing.B) {
    69  	b.ReportAllocs()
    70  
    71  	for n := 0; n < b.N; n++ {
    72  		b.StopTimer()
    73  		s, err := NewSegment(NewOptions())
    74  		if err != nil {
    75  			b.Fatalf("unable to construct new segment: %v", err)
    76  		}
    77  		b.StartTimer()
    78  
    79  		for _, d := range docs {
    80  			s.Insert(d)
    81  		}
    82  	}
    83  }
    84  
    85  func benchmarkMatchTermSegment(docs []doc.Metadata, b *testing.B) {
    86  	b.ReportAllocs()
    87  
    88  	sgmt, err := NewSegment(NewOptions())
    89  	if err != nil {
    90  		b.Fatalf("unable to construct new segment: %v", err)
    91  	}
    92  	s := sgmt.(*memSegment)
    93  	for _, d := range docs {
    94  		s.Insert(d)
    95  	}
    96  
    97  	b.ResetTimer()
    98  	for n := 0; n < b.N; n++ {
    99  		for _, d := range docs {
   100  			for _, f := range d.Fields {
   101  				s.matchTerm(f.Name, f.Value)
   102  			}
   103  		}
   104  	}
   105  }
   106  
   107  func benchmarkMatchRegexSegment(docs []doc.Metadata, b *testing.B) {
   108  	b.ReportAllocs()
   109  
   110  	sgmt, err := NewSegment(NewOptions())
   111  	if err != nil {
   112  		b.Fatalf("unable to construct new segment: %v", err)
   113  	}
   114  	s := sgmt.(*memSegment)
   115  	for _, d := range docs {
   116  		s.Insert(d)
   117  	}
   118  
   119  	b.ResetTimer()
   120  	for n := 0; n < b.N; n++ {
   121  		s.matchRegexp(benchSegmentField, benchSegmentCompiled)
   122  	}
   123  }