github.com/TeaOSLab/EdgeNode@v1.3.8/internal/utils/bfs/file_header_lazy_test.go (about) 1 // Copyright 2024 GoEdge CDN goedge.cdn@gmail.com. All rights reserved. Official site: https://goedge.cn . 2 3 package bfs_test 4 5 import ( 6 "github.com/TeaOSLab/EdgeNode/internal/utils/bfs" 7 "runtime" 8 "testing" 9 ) 10 11 func TestNewLazyFileHeaderFromData(t *testing.T) { 12 var header = &bfs.FileHeader{ 13 Version: 1, 14 Status: 200, 15 BodyBlocks: []bfs.BlockInfo{ 16 { 17 BFileOffsetFrom: 0, 18 BFileOffsetTo: 1 << 20, 19 }, 20 }, 21 } 22 blockBytes, err := header.Encode(bfs.Hash("123456")) 23 if err != nil { 24 t.Fatal(err) 25 } 26 27 _, _, rawData, err := bfs.DecodeMetaBlock(blockBytes) 28 if err != nil { 29 t.Fatal(err) 30 } 31 32 var lazyHeader = bfs.NewLazyFileHeaderFromData(rawData) 33 newHeader, err := lazyHeader.FileHeaderUnsafe() 34 if err != nil { 35 t.Fatal(err) 36 } 37 38 t.Log(newHeader) 39 } 40 41 func BenchmarkLazyFileHeader_Decode(b *testing.B) { 42 runtime.GOMAXPROCS(12) 43 44 var header = &bfs.FileHeader{ 45 Version: 1, 46 Status: 200, 47 BodyBlocks: []bfs.BlockInfo{}, 48 } 49 var offset int64 50 for { 51 var end = offset + 16<<10 52 if end > 1<<20 { 53 break 54 } 55 56 header.BodyBlocks = append(header.BodyBlocks, bfs.BlockInfo{ 57 BFileOffsetFrom: offset, 58 BFileOffsetTo: end, 59 }) 60 61 offset = end 62 } 63 64 var hash = bfs.Hash("123456") 65 66 blockBytes, err := header.Encode(hash) 67 if err != nil { 68 b.Fatal(err) 69 } 70 71 b.ResetTimer() 72 73 b.RunParallel(func(pb *testing.PB) { 74 for pb.Next() { 75 _, _, rawData, decodeErr := bfs.DecodeMetaBlock(blockBytes) 76 if decodeErr != nil { 77 b.Fatal(decodeErr) 78 } 79 80 var lazyHeader = bfs.NewLazyFileHeaderFromData(rawData) 81 _, decodeErr = lazyHeader.FileHeaderUnsafe() 82 if decodeErr != nil { 83 b.Fatal(decodeErr) 84 } 85 } 86 }) 87 }