github.com/dgraph-io/ristretto@v0.1.2-0.20240116140435-c67e07994f91/z/calloc_test.go (about) 1 /* 2 * Copyright 2020 Dgraph Labs, Inc. and Contributors 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 z 18 19 import ( 20 "fmt" 21 "sync" 22 "testing" 23 "time" 24 25 "math/rand" 26 27 "github.com/stretchr/testify/require" 28 ) 29 30 // $ go test -failfast -run xxx -bench . -benchmem -count 10 > out.txt 31 // $ benchstat out.txt 32 // name time/op 33 // Allocation/Pool-8 200µs ± 5% 34 // Allocation/Calloc-8 100µs ±11% 35 // 36 // name alloc/op 37 // Allocation/Pool-8 477B ±29% 38 // Allocation/Calloc-8 4.00B ± 0% 39 // 40 // name allocs/op 41 // Allocation/Pool-8 1.00 ± 0% 42 // Allocation/Calloc-8 0.00 43 func BenchmarkAllocation(b *testing.B) { 44 b.Run("Pool", func(b *testing.B) { 45 pool := sync.Pool{ 46 New: func() interface{} { 47 return make([]byte, 4<<10) 48 }, 49 } 50 b.RunParallel(func(pb *testing.PB) { 51 source := rand.NewSource(time.Now().UnixNano()) 52 r := rand.New(source) 53 for pb.Next() { 54 x := pool.Get().([]byte) 55 sz := r.Intn(100) << 10 56 if len(x) < sz { 57 x = make([]byte, sz) 58 } 59 r.Read(x) 60 //nolint:staticcheck 61 pool.Put(x) 62 } 63 }) 64 }) 65 66 b.Run("Calloc", func(b *testing.B) { 67 b.RunParallel(func(pb *testing.PB) { 68 source := rand.NewSource(time.Now().UnixNano()) 69 r := rand.New(source) 70 for pb.Next() { 71 sz := r.Intn(100) << 10 72 x := Calloc(sz, "test") 73 r.Read(x) 74 Free(x) 75 } 76 }) 77 }) 78 } 79 80 func TestCalloc(t *testing.T) { 81 // Check if we're using jemalloc. 82 // JE_MALLOC_CONF="abort:true,tcache:false" 83 84 StatsPrint() 85 buf := CallocNoRef(1, "test") 86 if len(buf) == 0 { 87 t.Skipf("Not using jemalloc. Skipping test.") 88 } 89 Free(buf) 90 require.Equal(t, int64(0), NumAllocBytes()) 91 92 buf1 := Calloc(128, "test") 93 require.Equal(t, int64(128), NumAllocBytes()) 94 buf2 := Calloc(128, "test") 95 require.Equal(t, int64(256), NumAllocBytes()) 96 97 Free(buf1) 98 require.Equal(t, int64(128), NumAllocBytes()) 99 100 // _ = buf2 101 Free(buf2) 102 require.Equal(t, int64(0), NumAllocBytes()) 103 fmt.Println(Leaks()) 104 105 // Double free would panic when debug mode is enabled in jemalloc. 106 // Free(buf2) 107 // require.Equal(t, int64(0), NumAllocBytes()) 108 }