github.com/siglens/siglens@v0.0.0-20240328180423-f7ce9ae441ed/pkg/segment/segexecutionbench_test.go (about)

     1  /*
     2  Copyright 2023.
     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 segment
    18  
    19  import (
    20  	"encoding/json"
    21  	"fmt"
    22  	"io"
    23  	"math"
    24  	"os"
    25  	"testing"
    26  	"time"
    27  
    28  	dtu "github.com/siglens/siglens/pkg/common/dtypeutils"
    29  	"github.com/siglens/siglens/pkg/config"
    30  	"github.com/siglens/siglens/pkg/segment/aggregations"
    31  	"github.com/siglens/siglens/pkg/segment/query"
    32  	"github.com/siglens/siglens/pkg/segment/structs"
    33  	. "github.com/siglens/siglens/pkg/segment/utils"
    34  	log "github.com/sirupsen/logrus"
    35  
    36  	esquery "github.com/siglens/siglens/pkg/es/query"
    37  )
    38  
    39  func Benchmark_ApplyFilterOpAndAggs(b *testing.B) {
    40  
    41  	b.ReportAllocs()
    42  	b.ResetTimer()
    43  
    44  	config.InitializeDefaultConfig()
    45  
    46  	fullTimeRange := &dtu.TimeRange{
    47  		StartEpochMs: 0,
    48  		EndEpochMs:   math.MaxUint64,
    49  	}
    50  
    51  	dtype, err := CreateDtypeEnclosure("chrome", 0)
    52  	if err != nil {
    53  		b.Fatal(err)
    54  	}
    55  	_ = &structs.SearchQuery{
    56  		ExpressionFilter: &structs.SearchExpression{
    57  			LeftSearchInput:  &structs.SearchExpressionInput{ColumnName: "browser_name"},
    58  			FilterOp:         Equals,
    59  			RightSearchInput: &structs.SearchExpressionInput{ColumnValue: dtype},
    60  		},
    61  		SearchType: structs.SimpleExpression,
    62  	}
    63  
    64  	// matchOSQuery := &SearchQuery{
    65  	//	ExpressionFilter: &SearchExpression{
    66  	//		LeftSearchInput:  &SearchExpressionInput{ColumnName: "os_name"},
    67  	//		FilterOp:         Equals,
    68  	//		RightSearchInput: &SearchExpressionInput{Literal: "MacOS"},
    69  	//	},
    70  	// }
    71  
    72  	// filterRefererQuery := &SearchQuery{
    73  	//	ExpressionFilter: &SearchExpression{
    74  	//		LeftSearchInput:  &SearchExpressionInput{ColumnName: "referer_medium"},
    75  	//		FilterOp:         Equals,
    76  	//		RightSearchInput: &SearchExpressionInput{Literal: "google"},
    77  	//	},
    78  	// }
    79  
    80  	// filterMobileQuery := &SearchQuery{
    81  	//	ExpressionFilter: &SearchExpression{
    82  	//		LeftSearchInput:  &SearchExpressionInput{ColumnName: "device_is_mobile"},
    83  	//		FilterOp:         Equals,
    84  	//		RightSearchInput: &SearchExpressionInput{Literal: "1"},
    85  	//	},
    86  	// }
    87  
    88  	valueFilter := structs.FilterCriteria{
    89  		ExpressionFilter: &structs.ExpressionFilter{
    90  			LeftInput:      &structs.FilterInput{Expression: &structs.Expression{LeftInput: &structs.ExpressionInput{ColumnName: "browser_name"}}},
    91  			FilterOperator: Equals,
    92  			RightInput:     &structs.FilterInput{Expression: &structs.Expression{LeftInput: &structs.ExpressionInput{ColumnValue: dtype}}},
    93  		},
    94  	}
    95  	simpleNode := &structs.ASTNode{
    96  		AndFilterCondition: &structs.Condition{FilterCriteria: []*structs.FilterCriteria{&valueFilter}},
    97  		TimeRange: &dtu.TimeRange{
    98  			StartEpochMs: 0,
    99  			EndEpochMs:   math.MaxUint64,
   100  		},
   101  	}
   102  
   103  	agg := &structs.QueryAggregators{
   104  		Sort: &structs.SortRequest{
   105  			ColName:   "timestamp",
   106  			Ascending: true,
   107  		},
   108  		TimeHistogram: &structs.TimeBucket{
   109  			IntervalMillis: 60000,
   110  		},
   111  	}
   112  
   113  	fileprefix := "/Users/ssubramanian/Desktop/SigLens/data/blocksum_encoding"
   114  
   115  	numSegfiles := 1
   116  	allSerRequests := make(map[string]*structs.SegmentSearchRequest)
   117  	for i := 0; i < numSegfiles; i += 1 {
   118  		segfilename := fmt.Sprintf("%v_%v", fileprefix, i)
   119  		serReq := createSearchReq(segfilename)
   120  		allSerRequests[segfilename] = serReq
   121  	}
   122  
   123  	doRespGen := true
   124  	sizeLimit := uint64(100)
   125  	count := 5
   126  
   127  	start := time.Now()
   128  	ti := structs.InitTableInfo("test", 0, false)
   129  	qc := structs.QueryContext{
   130  		TableInfo: ti,
   131  	}
   132  	for i := 0; i < count; i++ {
   133  		qItTime := time.Now()
   134  		nodeRes := query.ApplyFilterOperator(simpleNode, fullTimeRange, agg, 0, &qc)
   135  		nodeRes = aggregations.PostQueryBucketCleaning(nodeRes, agg, nil, nil, nil, 1, false)
   136  		if doRespGen {
   137  			esquery.GetQueryResponseJson(nodeRes, "test", qItTime, sizeLimit, 0, agg)
   138  		}
   139  	}
   140  
   141  	totalTime := time.Since(start).Seconds()
   142  	avgTime := totalTime / float64(count)
   143  	log.Warnf("Total time=%f. Average time=%f", totalTime, avgTime)
   144  
   145  	/*
   146  	   cd pkg/segment
   147  	   go test -run=Bench -bench=Benchmark_ApplyFilterOpAndAggs -cpuprofile cpuprofile.out -o rawsearch_cpu
   148  	   go tool pprof ./rawsearch_cpu cpuprofile.out
   149  
   150  	   (for mem profile)
   151  	   go test -run=Bench -bench=Benchmark_ApplyFilterOpAndAggs -benchmem -memprofile memprofile.out -o rawsearch_mem
   152  	   go tool pprof ./rawsearch_mem memprofile.out
   153  
   154  	*/
   155  }
   156  
   157  func createSearchReq(fileprefix string) *structs.SegmentSearchRequest {
   158  	fd, err := os.OpenFile(structs.GetBsuFnameFromSegKey(fileprefix), os.O_RDONLY, 0644)
   159  	if err != nil {
   160  		log.Fatal(err)
   161  	}
   162  	defer fd.Close()
   163  	decoder := json.NewDecoder(fd)
   164  	blockSummaries := make([]*structs.BlockSummary, 0)
   165  	for {
   166  		blockSummary := &structs.BlockSummary{}
   167  		err := decoder.Decode(blockSummary)
   168  		if err != nil {
   169  			if err != io.EOF {
   170  				log.Fatal(err)
   171  			}
   172  			break
   173  		}
   174  		blockSummaries = append(blockSummaries, blockSummary)
   175  	}
   176  
   177  	searchReq := &structs.SegmentSearchRequest{
   178  		SegmentKey: fileprefix + ".bsg",
   179  		SearchMetadata: &structs.SearchMetadataHolder{
   180  			BlockSummaries: blockSummaries,
   181  		},
   182  	}
   183  	return searchReq
   184  }