github.com/siglens/siglens@v0.0.0-20240328180423-f7ce9ae441ed/pkg/segment/reader/segread/timereader_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 segread
    18  
    19  import (
    20  	"os"
    21  	"testing"
    22  
    23  	"github.com/siglens/siglens/pkg/config"
    24  	"github.com/siglens/siglens/pkg/segment/reader/microreader"
    25  	"github.com/siglens/siglens/pkg/segment/structs"
    26  	"github.com/siglens/siglens/pkg/segment/writer"
    27  	log "github.com/sirupsen/logrus"
    28  	"github.com/stretchr/testify/assert"
    29  )
    30  
    31  func Test_readTimeStamps(t *testing.T) {
    32  
    33  	config.InitializeTestingConfig()
    34  	segDir := "data/"
    35  	_ = os.MkdirAll(segDir, 0755)
    36  	segKey := segDir + "test"
    37  	numBlocks := 10
    38  	numEntriesInBlock := 20000
    39  	_, blockSums, _, _, blockmeta, _ := writer.WriteMockColSegFile(segKey, numBlocks, numEntriesInBlock)
    40  
    41  	colName := config.GetTimeStampKey()
    42  	fileReader, err := InitNewTimeReaderFromBlockSummaries(segKey, colName, blockmeta, blockSums, 0)
    43  	assert.Nil(t, err)
    44  
    45  	totalRead := uint64(0)
    46  	totalInSummaries := uint64(0)
    47  	idx := 0
    48  	for blkNum, bSum := range blockSums {
    49  		allTime, err := fileReader.GetAllTimeStampsForBlock(uint16(blkNum))
    50  		assert.Nil(t, err, "no errors should occur")
    51  		assert.Equal(t, len(allTime), int(bSum.RecCount))
    52  		log.Infof("block %+v has %+v records. Supposed to have %+v", blkNum, len(allTime), int(blockSums[idx].RecCount))
    53  		totalRead += uint64(len(allTime))
    54  		totalInSummaries += uint64(blockSums[idx].RecCount)
    55  		idx++
    56  	}
    57  
    58  	err = fileReader.Close()
    59  	assert.Nil(t, err)
    60  
    61  	log.Infof("Total time stamps read %+v num in summaries %+v", totalRead, totalInSummaries)
    62  	assert.Equal(t, totalRead, totalInSummaries)
    63  	os.RemoveAll(segDir)
    64  }
    65  
    66  func Benchmark_readTimeFile(b *testing.B) {
    67  	config.InitializeTestingConfig()
    68  	segKey := "/Users/ssubramanian/Desktop/SigLens/siglens/data/Sris-MacBook-Pro.local/final/2022/02/21/01/valtix2/10005995996882630313/0"
    69  	sumFile := structs.GetBsuFnameFromSegKey(segKey)
    70  
    71  	blockSums, allBlockInfo, _, err := microreader.ReadBlockSummaries(sumFile, []byte{})
    72  	assert.Nil(b, err)
    73  
    74  	colName := config.GetTimeStampKey()
    75  
    76  	fileReader, err := InitNewTimeReaderFromBlockSummaries(segKey, colName, allBlockInfo, blockSums, 0)
    77  	assert.Nil(b, err)
    78  
    79  	// b.ResetTimer()
    80  	totalRead := uint64(0)
    81  	totalInSummaries := uint64(0)
    82  
    83  	numBlocks := len(blockSums)
    84  	for i := numBlocks - 1; i >= 0; i-- {
    85  		allTime, err := fileReader.GetAllTimeStampsForBlock(uint16(i))
    86  		assert.Nil(b, err, "no errors should occur")
    87  		expectedCount := int(blockSums[i].RecCount)
    88  		assert.Equal(b, len(allTime), expectedCount)
    89  		totalRead += uint64(len(allTime))
    90  		totalInSummaries += uint64(expectedCount)
    91  
    92  	}
    93  
    94  	err = fileReader.Close()
    95  	assert.Nil(b, err)
    96  
    97  	log.Infof("Total time stamps read %+v num in summaries %+v", totalRead, totalInSummaries)
    98  }