github.com/bytedance/gopkg@v0.0.0-20240514070511-01b2cbcf35e1/lang/mcache/mcache_test.go (about)

     1  // Copyright 2021 ByteDance 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  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package mcache
    16  
    17  import "testing"
    18  
    19  func TestMalloc(t *testing.T) {
    20  	buf := Malloc(4096)
    21  	t.Log(cap(buf))
    22  }
    23  
    24  func BenchmarkNormal4096(b *testing.B) {
    25  	var buf []byte
    26  	b.ReportAllocs()
    27  	for i := 0; i < b.N; i++ {
    28  		buf = make([]byte, 0, 4096)
    29  	}
    30  	_ = buf
    31  }
    32  
    33  func BenchmarkMCache4096(b *testing.B) {
    34  	var buf []byte
    35  	b.ReportAllocs()
    36  	for i := 0; i < b.N; i++ {
    37  		buf = Malloc(4096)
    38  		Free(buf)
    39  	}
    40  	_ = buf
    41  }
    42  
    43  func BenchmarkNormal10M(b *testing.B) {
    44  	var buf []byte
    45  	b.ReportAllocs()
    46  	for i := 0; i < b.N; i++ {
    47  		buf = make([]byte, 0, 1024*1024*10)
    48  	}
    49  	_ = buf
    50  }
    51  
    52  func BenchmarkMCache10M(b *testing.B) {
    53  	var buf []byte
    54  	b.ReportAllocs()
    55  	for i := 0; i < b.N; i++ {
    56  		buf = Malloc(1024 * 1024 * 10)
    57  		Free(buf)
    58  	}
    59  	_ = buf
    60  }
    61  
    62  func BenchmarkNormal4096Parallel(b *testing.B) {
    63  	b.ReportAllocs()
    64  	b.RunParallel(func(pb *testing.PB) {
    65  		var buf []byte
    66  		for pb.Next() {
    67  			for i := 0; i < b.N; i++ {
    68  				buf = make([]byte, 0, 4096)
    69  			}
    70  		}
    71  		_ = buf
    72  	})
    73  }
    74  
    75  func BenchmarkMCache4096Parallel(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 = Malloc(4096)
    82  				Free(buf)
    83  			}
    84  		}
    85  		_ = buf
    86  	})
    87  }
    88  
    89  func BenchmarkNormal10MParallel(b *testing.B) {
    90  	b.ReportAllocs()
    91  	b.RunParallel(func(pb *testing.PB) {
    92  		var buf []byte
    93  		for pb.Next() {
    94  			for i := 0; i < b.N; i++ {
    95  				buf = make([]byte, 0, 1024*1024*10)
    96  			}
    97  		}
    98  		_ = buf
    99  	})
   100  }
   101  
   102  func BenchmarkMCache10MParallel(b *testing.B) {
   103  	b.ReportAllocs()
   104  	b.RunParallel(func(pb *testing.PB) {
   105  		var buf []byte
   106  		for pb.Next() {
   107  			for i := 0; i < b.N; i++ {
   108  				buf = Malloc(1024 * 1024 * 10)
   109  				Free(buf)
   110  			}
   111  		}
   112  		_ = buf
   113  	})
   114  }