github.com/siglens/siglens@v0.0.0-20240328180423-f7ce9ae441ed/pkg/utils/segutils_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 utils
    18  
    19  import (
    20  	"fmt"
    21  	"testing"
    22  	"time"
    23  
    24  	"github.com/cespare/xxhash"
    25  	"github.com/google/uuid"
    26  	"github.com/rogpeppe/fastuuid"
    27  )
    28  
    29  func Test_IsSubWordPresent(t *testing.T) {
    30  	type args struct {
    31  		haystack []byte
    32  		needle   []byte
    33  	}
    34  	tests := []struct {
    35  		name string
    36  		args args
    37  		want bool
    38  	}{
    39  		{
    40  			name: "Either haystack or needle is empty",
    41  			args: args{
    42  				haystack: []byte(""),
    43  				needle:   []byte("abc"),
    44  			},
    45  			want: false,
    46  		},
    47  		{
    48  			name: "When haystack and needle are the same length",
    49  			args: args{
    50  				haystack: []byte("abc"),
    51  				needle:   []byte("abc"),
    52  			},
    53  			want: true,
    54  		},
    55  		{
    56  			name: "When needle is bigger than haystack",
    57  			args: args{
    58  				haystack: []byte("abc"),
    59  				needle:   []byte("abcd"),
    60  			},
    61  			want: false,
    62  		},
    63  		{
    64  			name: "When needle present in haystack",
    65  			args: args{
    66  				haystack: []byte("abc"),
    67  				needle:   []byte("ab"),
    68  			},
    69  			want: false,
    70  		},
    71  		{
    72  			name: "When needle is not present in haystack",
    73  			args: args{
    74  				haystack: []byte("abc"),
    75  				needle:   []byte("ef"),
    76  			},
    77  			want: false,
    78  		},
    79  		{
    80  			name: "complex words 1",
    81  			args: args{
    82  				haystack: []byte("abc def hij"),
    83  				needle:   []byte("ef"),
    84  			},
    85  			want: false,
    86  		},
    87  		{
    88  			name: "complex words 2",
    89  			args: args{
    90  				haystack: []byte("abc def hij"),
    91  				needle:   []byte("ij"),
    92  			},
    93  			want: false,
    94  		},
    95  		{
    96  			name: "complex words 3",
    97  			args: args{
    98  				haystack: []byte("abc def hij"),
    99  				needle:   []byte("ab"),
   100  			},
   101  			want: false,
   102  		},
   103  		{
   104  			name: "complex words 4",
   105  			args: args{
   106  				haystack: []byte("abc def hij"),
   107  				needle:   []byte("abc"),
   108  			},
   109  			want: true,
   110  		},
   111  		{
   112  			name: "complex words 5",
   113  			args: args{
   114  				haystack: []byte("abc def hij"),
   115  				needle:   []byte("def"),
   116  			},
   117  			want: true,
   118  		},
   119  		{
   120  			name: "complex words 6",
   121  			args: args{
   122  				haystack: []byte("abc def hij"),
   123  				needle:   []byte("hij"),
   124  			},
   125  			want: true,
   126  		},
   127  		{
   128  			name: "complex phrase 1",
   129  			args: args{
   130  				haystack: []byte("abc def hij"),
   131  				needle:   []byte("abc def"),
   132  			},
   133  			want: true,
   134  		},
   135  		{
   136  			name: "complex phrase 2",
   137  			args: args{
   138  				haystack: []byte("abc def hij"),
   139  				needle:   []byte("def hij"),
   140  			},
   141  			want: true,
   142  		},
   143  		{
   144  			name: "complex phrase 3",
   145  			args: args{
   146  				haystack: []byte("abc def hij"),
   147  				needle:   []byte("abc def hij"),
   148  			},
   149  			want: true,
   150  		},
   151  		{
   152  			name: "complex phrase 4",
   153  			args: args{
   154  				haystack: []byte("batch-777"),
   155  				needle:   []byte("batch-77"),
   156  			},
   157  			want: false,
   158  		},
   159  		{
   160  			name: "complex phrase 5",
   161  			args: args{
   162  				haystack: []byte("test1 batch-777"),
   163  				needle:   []byte("batch-77"),
   164  			},
   165  			want: false,
   166  		},
   167  		{
   168  			name: "complex phrase 6",
   169  			args: args{
   170  				haystack: []byte("test1 batch-777"),
   171  				needle:   []byte("batch-777"),
   172  			},
   173  			want: true,
   174  		},
   175  		{
   176  			name: "complex phrase 6",
   177  			args: args{
   178  				haystack: []byte("batch-777 test1 "),
   179  				needle:   []byte("batch-777"),
   180  			},
   181  			want: true,
   182  		},
   183  		{
   184  			name: "complex phrase 7",
   185  			args: args{
   186  				haystack: []byte("batch-777"),
   187  				needle:   []byte("batch-777"),
   188  			},
   189  			want: true,
   190  		},
   191  	}
   192  	for _, tt := range tests {
   193  		t.Run(tt.name, func(t *testing.T) {
   194  			if got := IsSubWordPresent(tt.args.haystack, tt.args.needle); got != tt.want {
   195  				t.Errorf("IsSubWordPresent() = %v, want %v", got, tt.want)
   196  			}
   197  		})
   198  	}
   199  }
   200  
   201  func Benchmark_UUIDNew(b *testing.B) {
   202  	b.ResetTimer()
   203  	b.ReportAllocs()
   204  
   205  	for i := 0; i < b.N; i++ {
   206  		_ = uuid.New().String()
   207  	}
   208  }
   209  
   210  func Benchmark_UUIDRandPool(b *testing.B) {
   211  	uuid.EnableRandPool()
   212  	b.ResetTimer()
   213  	b.ReportAllocs()
   214  
   215  	for i := 0; i < b.N; i++ {
   216  		_ = uuid.New().String()
   217  	}
   218  }
   219  
   220  func Benchmark_FastUUID(b *testing.B) {
   221  	g, _ := fastuuid.NewGenerator()
   222  	b.ResetTimer()
   223  	b.ReportAllocs()
   224  	for i := 0; i < b.N; i++ {
   225  		_ = g.Hex128()
   226  	}
   227  }
   228  
   229  func Benchmark_StringCreate(b *testing.B) {
   230  	ts_millis := uint64(time.Now().UTC().UnixNano()) / uint64(time.Millisecond)
   231  	sizeBytes := uint64(100000)
   232  	indexNameIn := "abcs"
   233  	hostname := "localhost"
   234  	b.ResetTimer()
   235  	b.ReportAllocs()
   236  	for i := 0; i < b.N; i++ {
   237  		tmpStr := fmt.Sprintf("%s-%d-%d-%s", hostname, ts_millis, sizeBytes, indexNameIn)
   238  		_ = fmt.Sprintf("%d", xxhash.Sum64String(tmpStr))
   239  	}
   240  }