github.com/whtcorpsinc/MilevaDB-Prod@v0.0.0-20211104133533-f57f4be3b597/soliton/profile/trackerrecorder_test.go (about)

     1  // Copyright 2020 WHTCORPS INC, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package profile
    15  
    16  import (
    17  	"math/rand"
    18  	"time"
    19  	"unsafe"
    20  
    21  	. "github.com/whtcorpsinc/check"
    22  	"github.com/whtcorpsinc/milevadb/soliton/ekvcache"
    23  )
    24  
    25  var _ = Suite(&trackRecorderSuite{})
    26  
    27  type trackRecorderSuite struct {
    28  }
    29  
    30  type mockCacheKey struct {
    31  	hash []byte
    32  	key  int64
    33  }
    34  
    35  func (mk *mockCacheKey) Hash() []byte {
    36  	if mk.hash != nil {
    37  		return mk.hash
    38  	}
    39  	mk.hash = make([]byte, 8)
    40  	for i := uint(0); i < 8; i++ {
    41  		mk.hash[i] = byte((mk.key >> ((i - 1) * 8)) & 0xff)
    42  	}
    43  	return mk.hash
    44  }
    45  
    46  func newMockHashKey(key int64) *mockCacheKey {
    47  	return &mockCacheKey{
    48  		key: key,
    49  	}
    50  }
    51  
    52  func getRandomString(l int) string {
    53  	str := "0123456789abcdefghijklmnopqrstuvwxyz"
    54  	bytes := []byte(str)
    55  	result := []byte{}
    56  	r := rand.New(rand.NewSource(time.Now().UnixNano()))
    57  	for i := 0; i < l; i++ {
    58  		result = append(result, bytes[r.Intn(len(bytes))])
    59  	}
    60  	return string(result)
    61  }
    62  
    63  func (t *trackRecorderSuite) TestHeapProfileRecorder(c *C) {
    64  	// As runtime.MemProfileRate default values is 512 KB , so the num should be greater than 60000
    65  	// that the memory usage of the values would be greater than 512 KB.
    66  	num := 60000
    67  	lru := ekvcache.NewSimpleLRUCache(uint(num), 0, 0)
    68  
    69  	keys := make([]*mockCacheKey, num)
    70  	for i := 0; i < num; i++ {
    71  		keys[i] = newMockHashKey(int64(i))
    72  		v := getRandomString(10)
    73  		lru.Put(keys[i], v)
    74  	}
    75  	for _, k := range lru.Keys() {
    76  		c.Assert(len(k.Hash()), Equals, 8)
    77  	}
    78  	for _, v := range lru.Values() {
    79  		val := v.(string)
    80  		c.Assert(len(val), Equals, 10)
    81  	}
    82  
    83  	bytes, err := defCaus.getFuncMemUsage(ekvcache.ProfileName)
    84  	c.Assert(err, IsNil)
    85  	valueSize := int(unsafe.Sizeof(getRandomString(10)))
    86  	// ensure that the consumed bytes is at least larger than num * size of value
    87  	c.Assert(int64(valueSize*num), LessEqual, bytes)
    88  	// we should assert lru size last and value size to reference lru in order to avoid gc
    89  	c.Assert(lru.Size(), Equals, num)
    90  }