github.com/sunvim/utils@v0.1.0/cachem/cache_test.go (about) 1 package cachem 2 3 import "testing" 4 5 func TestMalloc(t *testing.T) { 6 buf := Malloc(4096) 7 t.Log(cap(buf)) 8 } 9 10 func BenchmarkNormal4096(b *testing.B) { 11 var buf []byte 12 b.ReportAllocs() 13 for i := 0; i < b.N; i++ { 14 buf = make([]byte, 0, 4096) 15 } 16 _ = buf 17 } 18 19 func BenchmarkMCache4096(b *testing.B) { 20 var buf []byte 21 b.ReportAllocs() 22 for i := 0; i < b.N; i++ { 23 buf = Malloc(4096) 24 Free(buf) 25 } 26 _ = buf 27 } 28 29 func BenchmarkNormal10M(b *testing.B) { 30 var buf []byte 31 b.ReportAllocs() 32 for i := 0; i < b.N; i++ { 33 buf = make([]byte, 0, 1024*1024*10) 34 } 35 _ = buf 36 } 37 38 func BenchmarkMCache10M(b *testing.B) { 39 var buf []byte 40 b.ReportAllocs() 41 for i := 0; i < b.N; i++ { 42 buf = Malloc(1024 * 1024 * 10) 43 Free(buf) 44 } 45 _ = buf 46 } 47 48 func BenchmarkNormal4096Parallel(b *testing.B) { 49 b.ReportAllocs() 50 b.RunParallel(func(pb *testing.PB) { 51 var buf []byte 52 for pb.Next() { 53 for i := 0; i < b.N; i++ { 54 buf = make([]byte, 0, 4096) 55 } 56 } 57 _ = buf 58 }) 59 } 60 61 func BenchmarkMCache4096Parallel(b *testing.B) { 62 b.ReportAllocs() 63 b.RunParallel(func(pb *testing.PB) { 64 var buf []byte 65 for pb.Next() { 66 for i := 0; i < b.N; i++ { 67 buf = Malloc(4096) 68 Free(buf) 69 } 70 } 71 _ = buf 72 }) 73 } 74 75 func BenchmarkNormal10MParallel(b *testing.B) { 76 b.ReportAllocs() 77 b.RunParallel(func(pb *testing.PB) { 78 var buf []byte 79 for pb.Next() { 80 for i := 0; i < b.N; i++ { 81 buf = make([]byte, 0, 1024*1024*10) 82 } 83 } 84 _ = buf 85 }) 86 } 87 88 func BenchmarkMCache10MParallel(b *testing.B) { 89 b.ReportAllocs() 90 b.RunParallel(func(pb *testing.PB) { 91 var buf []byte 92 for pb.Next() { 93 for i := 0; i < b.N; i++ { 94 buf = Malloc(1024 * 1024 * 10) 95 Free(buf) 96 } 97 } 98 _ = buf 99 }) 100 }