github.com/siglens/siglens@v0.0.0-20240328180423-f7ce9ae441ed/pkg/segment/search/segsearch_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 search
    18  
    19  import (
    20  	"fmt"
    21  	"math"
    22  	"os"
    23  	"regexp"
    24  	"testing"
    25  	"time"
    26  
    27  	localstorage "github.com/siglens/siglens/pkg/blob/local"
    28  	dtu "github.com/siglens/siglens/pkg/common/dtypeutils"
    29  	"github.com/siglens/siglens/pkg/config"
    30  	"github.com/siglens/siglens/pkg/querytracker"
    31  	"github.com/siglens/siglens/pkg/segment/pqmr"
    32  	"github.com/siglens/siglens/pkg/segment/query/summary"
    33  	"github.com/siglens/siglens/pkg/segment/reader/microreader"
    34  	"github.com/siglens/siglens/pkg/segment/results/segresults"
    35  	"github.com/siglens/siglens/pkg/segment/structs"
    36  	. "github.com/siglens/siglens/pkg/segment/structs"
    37  	"github.com/siglens/siglens/pkg/segment/utils"
    38  	. "github.com/siglens/siglens/pkg/segment/utils"
    39  	"github.com/siglens/siglens/pkg/segment/writer"
    40  	log "github.com/sirupsen/logrus"
    41  	"github.com/stretchr/testify/assert"
    42  )
    43  
    44  func Test_simpleRawSearch(t *testing.T) {
    45  	config.InitializeTestingConfig()
    46  	config.SetSSInstanceName("mock-host")
    47  	err := config.InitDerivedConfig("test")
    48  	assert.NoError(t, err)
    49  	_ = localstorage.InitLocalStorage()
    50  
    51  	dataDir := "data/"
    52  	err = os.MkdirAll(dataDir+"mock-host.test/", 0755)
    53  	if err != nil {
    54  		assert.FailNow(t, "failed to create dir %+v", err)
    55  	}
    56  	numBuffers := 5
    57  	numEntriesForBuffer := 10
    58  	segKey := dataDir + "mock-host.test/raw_search_test"
    59  	_, allBlockSummaries, _, allCols, blockMetadata, _ := writer.WriteMockColSegFile(segKey, numBuffers, numEntriesForBuffer)
    60  
    61  	searchReq := &SegmentSearchRequest{
    62  		SegmentKey:        segKey,
    63  		AllBlocksToSearch: blockMetadata,
    64  		SearchMetadata: &SearchMetadataHolder{
    65  			BlockSummaries: allBlockSummaries,
    66  		},
    67  		VirtualTableName:   "evts",
    68  		AllPossibleColumns: allCols,
    69  	}
    70  
    71  	querySummary := summary.InitQuerySummary(summary.LOGS, 1)
    72  	value1, _ := CreateDtypeEnclosure("value1", 0)
    73  	query := &SearchQuery{
    74  		ExpressionFilter: &SearchExpression{
    75  			LeftSearchInput:  &SearchExpressionInput{ColumnName: "key1"},
    76  			FilterOp:         Equals,
    77  			RightSearchInput: &SearchExpressionInput{ColumnValue: value1},
    78  		},
    79  		SearchType: SimpleExpression,
    80  	}
    81  	timeRange := &dtu.TimeRange{
    82  		StartEpochMs: 1,
    83  		EndEpochMs:   5,
    84  	}
    85  	node := &SearchNode{
    86  		AndSearchConditions: &SearchCondition{
    87  			SearchQueries: []*SearchQuery{query},
    88  		},
    89  		NodeType: ColumnValueQuery,
    90  	}
    91  	allSegFileResults, err := segresults.InitSearchResults(10000, nil, RRCCmd, 1)
    92  	assert.NoError(t, err)
    93  	searchReq.SType = structs.RAW_SEARCH
    94  	rawSearchColumnar(searchReq, node, timeRange, 10000, nil, 1, allSegFileResults, 1, querySummary)
    95  	assert.Len(t, allSegFileResults.GetAllErrors(), 0)
    96  	assert.Equal(t, numBuffers*5, len(allSegFileResults.GetResults()))
    97  	assert.Equal(t, allSegFileResults.GetTotalCount(), uint64(len(allSegFileResults.GetResults())))
    98  
    99  	config.SetPQSEnabled(true)
   100  	// get file name
   101  	pqid := querytracker.GetHashForQuery(node)
   102  	pqidFname := fmt.Sprintf("%v/pqmr/%v.pqmr", searchReq.SegmentKey, pqid)
   103  
   104  	// check if that file exist, assert on file not exist
   105  	_, err = os.Stat(pqidFname)
   106  	assert.Equal(t, true, os.IsNotExist(err))
   107  
   108  	// make query persistent
   109  	querytracker.UpdateQTUsage([]string{searchReq.VirtualTableName}, node, nil)
   110  
   111  	// Call rawSearchColumnar
   112  	rawSearchColumnar(searchReq, node, timeRange, 10000, nil, 1, allSegFileResults, 1, querySummary)
   113  	// We need to sleep because pqmr files are written in background go routines
   114  	time.Sleep(1 * time.Second)
   115  	// Now make sure filename exists
   116  	_, err = os.Stat(pqidFname)
   117  	assert.Nil(t, err)
   118  
   119  	// Read pqmr file
   120  	pqmrReadResults, err := pqmr.ReadPqmr(&pqidFname)
   121  	assert.Nil(t, err)
   122  	assert.NotEmpty(t, pqmrReadResults)
   123  	assert.Equal(t, numBuffers, int(pqmrReadResults.GetNumBlocks()))
   124  
   125  	numOfRecs := uint(0)
   126  	allBlocks := pqmrReadResults.GetAllBlocks()
   127  	assert.Len(t, allBlocks, numBuffers)
   128  	for _, blkNum := range allBlocks {
   129  		block, _ := pqmrReadResults.GetBlockResults(blkNum)
   130  		numOfRecs += block.GetNumberOfSetBits()
   131  	}
   132  	assert.Equal(t, uint(numBuffers*numEntriesForBuffer), numOfRecs)
   133  
   134  	config.SetPQSEnabled(false)
   135  
   136  	zero, _ := CreateDtypeEnclosure(false, 0)
   137  	query = &SearchQuery{
   138  		ExpressionFilter: &SearchExpression{
   139  			LeftSearchInput:  &SearchExpressionInput{ColumnName: "key3"},
   140  			FilterOp:         Equals,
   141  			RightSearchInput: &SearchExpressionInput{ColumnValue: zero},
   142  		},
   143  		SearchType: SimpleExpression,
   144  	}
   145  
   146  	fullTimeRange := &dtu.TimeRange{
   147  		StartEpochMs: 0,
   148  		EndEpochMs:   uint64(numEntriesForBuffer),
   149  	}
   150  	node = &SearchNode{
   151  		AndSearchConditions: &SearchCondition{
   152  			SearchQueries: []*SearchQuery{query},
   153  		},
   154  		NodeType: ColumnValueQuery,
   155  	}
   156  	allSegFileResults, err = segresults.InitSearchResults(10000, nil, RRCCmd, 1)
   157  	assert.NoError(t, err)
   158  	rawSearchColumnar(searchReq, node, fullTimeRange, 10000, nil, 1, allSegFileResults, 3, querySummary)
   159  	assert.Len(t, allSegFileResults.GetAllErrors(), 0)
   160  	assert.Equal(t, (numBuffers*numEntriesForBuffer)/2, len(allSegFileResults.GetResults()))
   161  
   162  	query = &SearchQuery{
   163  		ExpressionFilter: &SearchExpression{
   164  			LeftSearchInput:  &SearchExpressionInput{ColumnName: "invalid_column"},
   165  			FilterOp:         Equals,
   166  			RightSearchInput: &SearchExpressionInput{ColumnValue: zero},
   167  		},
   168  		SearchType: SimpleExpression,
   169  	}
   170  
   171  	node = &SearchNode{
   172  		AndSearchConditions: &SearchCondition{
   173  			SearchQueries: []*SearchQuery{query},
   174  		},
   175  		NodeType: ColumnValueQuery,
   176  	}
   177  
   178  	allSegFileResults, err = segresults.InitSearchResults(10000, nil, RRCCmd, 1)
   179  	assert.NoError(t, err)
   180  	rawSearchColumnar(searchReq, node, fullTimeRange, 10000, nil, 1, allSegFileResults, 0, querySummary)
   181  	assert.NotEqual(t, 0, allSegFileResults.GetAllErrors(), "errors MUST happen")
   182  	assert.Equal(t, 0, len(allSegFileResults.GetResults()))
   183  
   184  	batchZero, _ := CreateDtypeEnclosure("batch-0-*", 0)
   185  	batchOne, _ := CreateDtypeEnclosure("batch-1-*", 0)
   186  	query = &SearchQuery{
   187  		ExpressionFilter: &SearchExpression{
   188  			LeftSearchInput:  &SearchExpressionInput{ColumnName: "key5"},
   189  			FilterOp:         Equals,
   190  			RightSearchInput: &SearchExpressionInput{ColumnValue: batchZero},
   191  		},
   192  		SearchType: RegexExpression,
   193  	}
   194  
   195  	node = &SearchNode{
   196  		AndSearchConditions: &SearchCondition{
   197  			SearchQueries: []*SearchQuery{query},
   198  		},
   199  		NodeType: ColumnValueQuery,
   200  	}
   201  
   202  	allSegFileResults, err = segresults.InitSearchResults(10000, nil, RRCCmd, 1)
   203  	assert.NoError(t, err)
   204  	rawSearchColumnar(searchReq, node, fullTimeRange, 10000, nil, 1, allSegFileResults, 0, querySummary)
   205  	assert.Len(t, allSegFileResults.GetAllErrors(), 0)
   206  	assert.Equal(t, numEntriesForBuffer, len(allSegFileResults.GetResults()))
   207  
   208  	query = &SearchQuery{
   209  		ExpressionFilter: &SearchExpression{
   210  			LeftSearchInput:  &SearchExpressionInput{ColumnName: "*"},
   211  			FilterOp:         Equals,
   212  			RightSearchInput: &SearchExpressionInput{ColumnValue: batchZero},
   213  		},
   214  		SearchType: RegexExpressionAllColumns,
   215  	}
   216  
   217  	node = &SearchNode{
   218  		AndSearchConditions: &SearchCondition{
   219  			SearchQueries: []*SearchQuery{query},
   220  		},
   221  		NodeType: ColumnValueQuery,
   222  	}
   223  
   224  	allSegFileResults, err = segresults.InitSearchResults(10000, nil, RRCCmd, 1)
   225  	assert.NoError(t, err)
   226  	searchReq.CmiPassedCnames = make(map[uint16]map[string]bool)
   227  	for blkNum := range searchReq.AllBlocksToSearch {
   228  		searchReq.CmiPassedCnames[blkNum] = make(map[string]bool)
   229  		for cname := range allCols {
   230  			searchReq.CmiPassedCnames[blkNum][cname] = true
   231  		}
   232  	}
   233  
   234  	rawSearchColumnar(searchReq, node, fullTimeRange, 10000, nil, 1, allSegFileResults, 5, querySummary)
   235  	assert.Len(t, allSegFileResults.GetAllErrors(), 0)
   236  	assert.Equal(t, numEntriesForBuffer, len(allSegFileResults.GetResults()))
   237  
   238  	// // (col5==batch-0-* OR col5==batch-1-*) AND key1=value1
   239  	batch0Query := &SearchQuery{
   240  		ExpressionFilter: &SearchExpression{
   241  			LeftSearchInput:  &SearchExpressionInput{ColumnName: "*"},
   242  			FilterOp:         Equals,
   243  			RightSearchInput: &SearchExpressionInput{ColumnValue: batchZero},
   244  		},
   245  		SearchType: RegexExpressionAllColumns,
   246  	}
   247  
   248  	batch1Query := &SearchQuery{
   249  		ExpressionFilter: &SearchExpression{
   250  			LeftSearchInput:  &SearchExpressionInput{ColumnName: "*"},
   251  			FilterOp:         Equals,
   252  			RightSearchInput: &SearchExpressionInput{ColumnValue: batchOne},
   253  		},
   254  		SearchType: RegexExpressionAllColumns,
   255  	}
   256  
   257  	valueQuery := &SearchQuery{
   258  		ExpressionFilter: &SearchExpression{
   259  			LeftSearchInput:  &SearchExpressionInput{ColumnName: "key1"},
   260  			FilterOp:         Equals,
   261  			RightSearchInput: &SearchExpressionInput{ColumnValue: value1},
   262  		},
   263  		SearchType: SimpleExpression,
   264  	}
   265  
   266  	orNode := &SearchNode{
   267  		OrSearchConditions: &SearchCondition{
   268  			SearchQueries: []*SearchQuery{batch0Query, batch1Query},
   269  		},
   270  		NodeType: ColumnValueQuery,
   271  	}
   272  
   273  	nestedQuery := &SearchNode{
   274  		AndSearchConditions: &SearchCondition{
   275  			SearchQueries: []*SearchQuery{valueQuery},
   276  			SearchNode:    []*SearchNode{orNode},
   277  		},
   278  		NodeType: ColumnValueQuery,
   279  	}
   280  
   281  	allSegFileResults, err = segresults.InitSearchResults(10000, nil, RRCCmd, 1)
   282  	assert.NoError(t, err)
   283  	rawSearchColumnar(searchReq, nestedQuery, fullTimeRange, 10000, nil, 1, allSegFileResults, 0, querySummary)
   284  	assert.Len(t, allSegFileResults.GetAllErrors(), 0)
   285  	assert.Equal(t, numEntriesForBuffer*2, len(allSegFileResults.GetResults()))
   286  
   287  	testAggsQuery(t, numEntriesForBuffer, searchReq)
   288  
   289  	err = os.RemoveAll(dataDir)
   290  	assert.Nil(t, err)
   291  }
   292  
   293  func Test_simpleRawSearch_jaeger(t *testing.T) {
   294  	config.InitializeTestingConfig()
   295  	config.SetSSInstanceName("mock-host")
   296  	err := config.InitDerivedConfig("test")
   297  	assert.Nil(t, err)
   298  	_ = localstorage.InitLocalStorage()
   299  	dataDir := "data/"
   300  	err = os.MkdirAll(dataDir+"mock-host/", 0755)
   301  	if err != nil {
   302  		assert.FailNow(t, "failed to create dir %+v", err)
   303  	}
   304  	numBuffers := 1
   305  	numEntriesForBuffer := 1
   306  	segKey := dataDir + "mock-host/raw_search_test_jaeger"
   307  	_, allBlockSummaries, _, allCols, blockMetadata := writer.WriteMockTraceFile(segKey, numBuffers, numEntriesForBuffer)
   308  
   309  	searchReq := &SegmentSearchRequest{
   310  		SegmentKey:        segKey,
   311  		AllBlocksToSearch: blockMetadata,
   312  		SearchMetadata: &SearchMetadataHolder{
   313  			BlockSummaries: allBlockSummaries,
   314  		},
   315  		VirtualTableName:   "jaeger-evts",
   316  		AllPossibleColumns: allCols,
   317  	}
   318  	value1, _ := CreateDtypeEnclosure("const", 0)
   319  	querySummary := summary.InitQuerySummary(summary.LOGS, 1)
   320  	query := &SearchQuery{
   321  		MatchFilter: &MatchFilter{
   322  			MatchColumn: "tags",
   323  			MatchDictArray: &MatchDictArrayRequest{
   324  				MatchKey:   []byte("sampler.type"),
   325  				MatchValue: value1,
   326  			},
   327  			MatchType: MATCH_DICT_ARRAY,
   328  		},
   329  		SearchType: MatchDictArraySingleColumn,
   330  	}
   331  	timeRange := &dtu.TimeRange{
   332  		StartEpochMs: 1,
   333  		EndEpochMs:   5,
   334  	}
   335  	node := &SearchNode{
   336  		AndSearchConditions: &SearchCondition{
   337  			SearchQueries: []*SearchQuery{query},
   338  		},
   339  		NodeType: ColumnValueQuery,
   340  	}
   341  	allSegFileResults, err := segresults.InitSearchResults(10000, nil, RRCCmd, 1)
   342  	assert.NoError(t, err)
   343  	searchReq.SType = structs.RAW_SEARCH
   344  	rawSearchColumnar(searchReq, node, timeRange, 10000, nil, 1, allSegFileResults, 1, querySummary)
   345  
   346  	assert.Len(t, allSegFileResults.GetAllErrors(), 0)
   347  	assert.Equal(t, numBuffers, len(allSegFileResults.GetResults()))
   348  	assert.Equal(t, allSegFileResults.GetTotalCount(), uint64(len(allSegFileResults.GetResults())))
   349  
   350  	value2, _ := CreateDtypeEnclosure("200", 1)
   351  	query2 := &SearchQuery{
   352  		MatchFilter: &MatchFilter{
   353  			MatchColumn: "tags",
   354  			MatchDictArray: &MatchDictArrayRequest{
   355  				MatchKey:   []byte("http.status_code"),
   356  				MatchValue: value2,
   357  			},
   358  			MatchType: MATCH_DICT_ARRAY,
   359  		},
   360  		SearchType: MatchDictArraySingleColumn,
   361  	}
   362  
   363  	node2 := &SearchNode{
   364  		AndSearchConditions: &SearchCondition{
   365  			SearchQueries: []*SearchQuery{query2},
   366  		},
   367  		NodeType: ColumnValueQuery,
   368  	}
   369  	assert.NoError(t, err)
   370  	searchReq.SType = structs.RAW_SEARCH
   371  	rawSearchColumnar(searchReq, node2, timeRange, 10000, nil, 1, allSegFileResults, 1, querySummary)
   372  
   373  	assert.Len(t, allSegFileResults.GetAllErrors(), 0)
   374  	assert.Equal(t, numBuffers*2, len(allSegFileResults.GetResults()))
   375  	assert.Equal(t, allSegFileResults.GetTotalCount(), uint64(len(allSegFileResults.GetResults())))
   376  
   377  	err = os.RemoveAll(dataDir)
   378  	assert.Nil(t, err)
   379  }
   380  
   381  func testAggsQuery(t *testing.T, numEntriesForBuffer int, searchReq *structs.SegmentSearchRequest) {
   382  	querySummary := summary.InitQuerySummary(summary.LOGS, 101010)
   383  
   384  	batchZero, _ := CreateDtypeEnclosure("batch-0-*", 0)
   385  	query := &SearchQuery{
   386  		ExpressionFilter: &SearchExpression{
   387  			LeftSearchInput:  &SearchExpressionInput{ColumnName: "key5"},
   388  			FilterOp:         Equals,
   389  			RightSearchInput: &SearchExpressionInput{ColumnValue: batchZero},
   390  		},
   391  		SearchType: RegexExpression,
   392  	}
   393  	fullTimeRange := &dtu.TimeRange{
   394  		StartEpochMs: 0,
   395  		EndEpochMs:   uint64(numEntriesForBuffer),
   396  	}
   397  
   398  	node := &SearchNode{
   399  		AndSearchConditions: &SearchCondition{
   400  			SearchQueries: []*SearchQuery{query},
   401  		},
   402  		NodeType: ColumnValueQuery,
   403  	}
   404  	measureOps := make([]*structs.MeasureAggregator, 2)
   405  	measureOps[0] = &structs.MeasureAggregator{MeasureCol: "key0", MeasureFunc: utils.Cardinality}
   406  	measureOps[1] = &structs.MeasureAggregator{MeasureCol: "key6", MeasureFunc: utils.Min}
   407  
   408  	allSegFileResults, err := segresults.InitSearchResults(10000, nil, SegmentStatsCmd, 1000)
   409  	assert.Nil(t, err)
   410  
   411  	block0, err := RawComputeSegmentStats(searchReq, 5, node, fullTimeRange, measureOps, allSegFileResults, 123, querySummary)
   412  	assert.Nil(t, err)
   413  	assert.Len(t, block0, 2)
   414  	assert.Contains(t, block0, "key0")
   415  	assert.Contains(t, block0, "key6")
   416  	key0Block0Stats := block0["key0"]
   417  	assert.False(t, key0Block0Stats.IsNumeric)
   418  	assert.Equal(t, key0Block0Stats.Count, uint64(numEntriesForBuffer))
   419  	assert.GreaterOrEqual(t, key0Block0Stats.Hll.Estimate(), uint64(0))
   420  	assert.LessOrEqual(t, key0Block0Stats.Hll.Estimate(), uint64(2), "key0 always has same value")
   421  
   422  	key6Block0Stats := block0["key6"]
   423  	assert.True(t, key6Block0Stats.IsNumeric)
   424  	assert.Equal(t, key6Block0Stats.Count, uint64(numEntriesForBuffer))
   425  	assert.Equal(t, key6Block0Stats.NumStats.Dtype, utils.SS_DT_FLOAT)
   426  	assert.Equal(t, key6Block0Stats.NumStats.Min.FloatVal, float64(0))
   427  	assert.Equal(t, key6Block0Stats.NumStats.Max.FloatVal, float64(numEntriesForBuffer-1)*2)
   428  }
   429  
   430  type BenchQueryConds struct {
   431  	colNameToSearch   string
   432  	colValStrToSearch string
   433  	queryType         SearchQueryType
   434  	isRegex           bool
   435  }
   436  
   437  func Benchmark_simpleRawSearch(b *testing.B) {
   438  	config.InitializeDefaultConfig()
   439  	config.SetDebugMode(true)
   440  
   441  	querySummary := summary.InitQuerySummary(summary.LOGS, 1)
   442  
   443  	cond1 := &BenchQueryConds{colNameToSearch: "device_type", colValStrToSearch: "mobile", queryType: SimpleExpression, isRegex: false}
   444  	cond2 := &BenchQueryConds{colNameToSearch: "referer_medium", colValStrToSearch: "internal", queryType: SimpleExpression, isRegex: false}
   445  	allconds := []*BenchQueryConds{cond1, cond2}
   446  
   447  	// cond1 := &BenchQueryConds{colNameToSearch: "*", colValStrToSearch: "chrome", queryType: MatchAll, isRegex: false}
   448  	// allconds := []*BenchQueryConds{cond1}
   449  
   450  	segKey := "/Users/ssubramanian/Desktop/SigLens/siglens/data/Sris-MacBook-Pro.local/final/2022/03/03/03/ind-v1-valtix/1149711685912017186/0"
   451  	start := time.Now()
   452  
   453  	b.ReportAllocs()
   454  	b.ResetTimer()
   455  
   456  	node, searchReq, fullTimeRange, agg := createBenchQuery(b, segKey, allconds)
   457  
   458  	count := 50
   459  	for i := 0; i < count; i++ {
   460  		allSegFileResults, err := segresults.InitSearchResults(100, agg, RRCCmd, 8)
   461  		assert.NoError(b, err)
   462  		rawSearchColumnar(searchReq, node, fullTimeRange, 100, agg, 8, allSegFileResults, uint64(i), querySummary)
   463  		b := allSegFileResults.GetBucketResults()
   464  		c := allSegFileResults.GetTotalCount()
   465  		log.Infof("num buckets %+v, count %+v", len(b["date histogram"].Results), c)
   466  	}
   467  
   468  	totalTime := time.Since(start).Seconds()
   469  	avgTime := totalTime / float64(count)
   470  	log.Warnf("Total time=%f. Average time=%f", totalTime, avgTime)
   471  
   472  	/*
   473  	   cd pkg/segment/search
   474  	   go test -run=Bench -bench=Benchmark_simpleRawSearch -cpuprofile cpuprofile.out -o rawsearch_cpu
   475  	   go tool pprof ./rawsearch_cpu cpuprofile.out
   476  
   477  	   (for mem profile)
   478  	   go test -run=Bench -bench=Benchmark_simpleRawSearch -benchmem -memprofile memprofile.out -o rawsearch_mem
   479  	   go tool pprof ./rawsearch_mem memprofile.out
   480  
   481  	   **** History *****
   482  	   recording history of this benchmark perf test
   483  	   1-Dec: develop: 0.428 s, mem goes up only till 1GB
   484  
   485  	*/
   486  }
   487  
   488  func Benchmark_simpleAggregations(b *testing.B) {
   489  	config.InitializeDefaultConfig()
   490  	config.SetDebugMode(true)
   491  
   492  	querySummary := summary.InitQuerySummary(summary.LOGS, 1)
   493  
   494  	cond1 := &BenchQueryConds{colNameToSearch: "j", colValStrToSearch: "group 0", queryType: SimpleExpression, isRegex: false}
   495  	allconds := []*BenchQueryConds{cond1}
   496  
   497  	segKey := "/Users/ssubramanian/Desktop/SigLens/siglens/data/Sris-MBP.lan/final/ind-0/0-3544697602014606120/0/0"
   498  	node, searchReq, fullTimeRange, _ := createBenchQuery(b, segKey, allconds)
   499  	agg := &structs.QueryAggregators{
   500  		GroupByRequest: &GroupByRequest{
   501  			GroupByColumns: []string{"a", "d"},
   502  			MeasureOperations: []*structs.MeasureAggregator{
   503  				{MeasureCol: "a", MeasureFunc: utils.Count},
   504  				{MeasureCol: "a", MeasureFunc: utils.Avg},
   505  			},
   506  			AggName: "test",
   507  		},
   508  	}
   509  	start := time.Now()
   510  	b.ReportAllocs()
   511  	b.ResetTimer()
   512  
   513  	count := 50
   514  	for i := 0; i < count; i++ {
   515  		allSegFileResults, err := segresults.InitSearchResults(100, agg, RRCCmd, 8)
   516  		assert.NoError(b, err)
   517  		rawSearchColumnar(searchReq, node, fullTimeRange, 100, agg, 8, allSegFileResults, uint64(i), querySummary)
   518  		b := allSegFileResults.GetBucketResults()
   519  		c := allSegFileResults.GetTotalCount()
   520  		log.Infof("num buckets %+v, count %+v", len(b["test"].Results), c)
   521  		if len(b["test"].Results) > 0 {
   522  			log.Infof("%+v %+v %+v", b["test"].Results[0].BucketKey, b["test"].Results[0].ElemCount, b["test"].Results[0].StatRes)
   523  		}
   524  	}
   525  
   526  	totalTime := time.Since(start).Seconds()
   527  	avgTime := totalTime / float64(count)
   528  	log.Warnf("Total time=%f. Average time=%f", totalTime, avgTime)
   529  
   530  	/*
   531  	   cd pkg/segment/search
   532  	   go test -run=Bench -bench=Benchmark_simpleRawSearch -cpuprofile cpuprofile.out -o rawsearch_cpu
   533  	   go tool pprof ./rawsearch_cpu cpuprofile.out
   534  
   535  	   (for mem profile)
   536  	   go test -run=Bench -bench=Benchmark_simpleRawSearch -benchmem -memprofile memprofile.out -o rawsearch_mem
   537  	   go tool pprof ./rawsearch_mem memprofile.out
   538  
   539  	   **** History *****
   540  	   recording history of this benchmark perf test
   541  	   1-Dec: develop: 0.428 s, mem goes up only till 1GB
   542  
   543  	*/
   544  }
   545  
   546  func createBenchQuery(b *testing.B, segKey string,
   547  	allconds []*BenchQueryConds) (*SearchNode, *SegmentSearchRequest, *dtu.TimeRange, *QueryAggregators) {
   548  
   549  	fullTimeRange := &dtu.TimeRange{
   550  		StartEpochMs: 0,
   551  		EndEpochMs:   math.MaxUint64,
   552  	}
   553  
   554  	allsqs := make([]*SearchQuery, 0)
   555  
   556  	for _, cond := range allconds {
   557  		dtype, err := CreateDtypeEnclosure(cond.colValStrToSearch, 0)
   558  		if err != nil {
   559  			b.Fatal(err)
   560  		}
   561  		if cond.colNameToSearch == "*" {
   562  			continue
   563  		}
   564  
   565  		if cond.isRegex {
   566  			rexpC, _ := regexp.Compile(dtu.ReplaceWildcardStarWithRegex(cond.colValStrToSearch))
   567  			dtype.SetRegexp(rexpC)
   568  		}
   569  
   570  		sq := &SearchQuery{
   571  			ExpressionFilter: &SearchExpression{
   572  				LeftSearchInput:  &SearchExpressionInput{ColumnName: cond.colNameToSearch},
   573  				FilterOp:         Equals,
   574  				RightSearchInput: &SearchExpressionInput{ColumnValue: dtype},
   575  			},
   576  			SearchType: cond.queryType,
   577  		}
   578  		allsqs = append(allsqs, sq)
   579  
   580  	}
   581  
   582  	node := &SearchNode{
   583  		AndSearchConditions: &SearchCondition{
   584  			SearchQueries: allsqs,
   585  		},
   586  		NodeType: ColumnValueQuery,
   587  	}
   588  
   589  	agg := &QueryAggregators{
   590  		Sort: &SortRequest{
   591  			ColName:   "timestamp",
   592  			Ascending: true,
   593  		},
   594  		TimeHistogram: &TimeBucket{
   595  			IntervalMillis: 60000,
   596  		},
   597  	}
   598  
   599  	bSumFile := structs.GetBsuFnameFromSegKey(segKey)
   600  	blockSummaries, allBlockInfo, _, err := microreader.ReadBlockSummaries(bSumFile, []byte{})
   601  	if err != nil {
   602  		log.Fatal(err)
   603  	}
   604  
   605  	searchReq := &SegmentSearchRequest{
   606  		SegmentKey: segKey,
   607  		SearchMetadata: &SearchMetadataHolder{
   608  			BlockSummaries: blockSummaries,
   609  		},
   610  		AllBlocksToSearch: allBlockInfo,
   611  	}
   612  
   613  	allSearchColumns, _ := node.GetAllColumnsToSearch()
   614  	searchReq.AllPossibleColumns = allSearchColumns
   615  
   616  	return node, searchReq, fullTimeRange, agg
   617  
   618  }