github.com/bytedance/gopkg@v0.0.0-20240514070511-01b2cbcf35e1/lang/dirtmake/bytes_test.go (about) 1 // Copyright 2024 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 dirtmake 16 17 import ( 18 "bytes" 19 "fmt" 20 "testing" 21 ) 22 23 var data []byte 24 25 const block1kb = 1024 26 27 func BenchmarkParallelDirtBytes(b *testing.B) { 28 src := make([]byte, block1kb) 29 for i := range src { 30 src[i] = byte(i) 31 } 32 b.RunParallel(func(pb *testing.PB) { 33 for pb.Next() { 34 bs := Bytes(block1kb, block1kb) 35 copy(bs, src) 36 if !bytes.Equal(bs, src) { 37 b.Fatalf("bytes not equal") 38 } 39 } 40 }) 41 } 42 43 func BenchmarkDirtBytes(b *testing.B) { 44 for size := block1kb; size < block1kb*20; size += block1kb * 2 { 45 b.Run(fmt.Sprintf("size=%dkb", size/block1kb), func(b *testing.B) { 46 for i := 0; i < b.N; i++ { 47 data = Bytes(size, size) 48 } 49 }) 50 } 51 } 52 53 func BenchmarkOriginBytes(b *testing.B) { 54 for size := block1kb; size < block1kb*20; size += block1kb * 2 { 55 b.Run(fmt.Sprintf("size=%dkb", size/block1kb), func(b *testing.B) { 56 for i := 0; i < b.N; i++ { 57 data = make([]byte, size) 58 } 59 }) 60 } 61 }