github.com/TeaOSLab/EdgeNode@v1.3.8/internal/utils/bfs/file_header_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  	"encoding/json"
     7  	"github.com/TeaOSLab/EdgeNode/internal/utils/bfs"
     8  	"github.com/TeaOSLab/EdgeNode/internal/utils/fasttime"
     9  	"github.com/iwind/TeaGo/assert"
    10  	"github.com/iwind/TeaGo/logs"
    11  	"math/rand"
    12  	"runtime"
    13  	"testing"
    14  )
    15  
    16  func TestFileHeader_Compact(t *testing.T) {
    17  	var a = assert.NewAssertion(t)
    18  
    19  	{
    20  		var header = &bfs.FileHeader{
    21  			Version:  1,
    22  			Status:   200,
    23  			BodySize: 100,
    24  			BodyBlocks: []bfs.BlockInfo{
    25  				{
    26  					OriginOffsetFrom: 0,
    27  					OriginOffsetTo:   100,
    28  				},
    29  			},
    30  		}
    31  		header.Compact()
    32  		a.IsTrue(header.IsCompleted)
    33  	}
    34  
    35  	{
    36  		var header = &bfs.FileHeader{
    37  			Version:  1,
    38  			Status:   200,
    39  			BodySize: 200,
    40  			BodyBlocks: []bfs.BlockInfo{
    41  				{
    42  					OriginOffsetFrom: 100,
    43  					OriginOffsetTo:   200,
    44  				},
    45  				{
    46  					OriginOffsetFrom: 0,
    47  					OriginOffsetTo:   100,
    48  				},
    49  			},
    50  		}
    51  		header.Compact()
    52  		a.IsTrue(header.IsCompleted)
    53  	}
    54  
    55  	{
    56  		var header = &bfs.FileHeader{
    57  			Version:  1,
    58  			Status:   200,
    59  			BodySize: 200,
    60  			BodyBlocks: []bfs.BlockInfo{
    61  				{
    62  					OriginOffsetFrom: 10,
    63  					OriginOffsetTo:   99,
    64  				},
    65  				{
    66  					OriginOffsetFrom: 110,
    67  					OriginOffsetTo:   200,
    68  				},
    69  				{
    70  					OriginOffsetFrom: 88,
    71  					OriginOffsetTo:   120,
    72  				},
    73  				{
    74  					OriginOffsetFrom: 0,
    75  					OriginOffsetTo:   100,
    76  				},
    77  			},
    78  		}
    79  		header.Compact()
    80  		a.IsTrue(header.IsCompleted)
    81  	}
    82  
    83  	{
    84  		var header = &bfs.FileHeader{
    85  			Version:  1,
    86  			Status:   200,
    87  			BodySize: 100,
    88  			BodyBlocks: []bfs.BlockInfo{
    89  				{
    90  					OriginOffsetFrom: 10,
    91  					OriginOffsetTo:   100,
    92  				},
    93  				{
    94  					OriginOffsetFrom: 100,
    95  					OriginOffsetTo:   200,
    96  				},
    97  			},
    98  		}
    99  		header.Compact()
   100  		a.IsFalse(header.IsCompleted)
   101  	}
   102  
   103  	{
   104  		var header = &bfs.FileHeader{
   105  			Version:  1,
   106  			Status:   200,
   107  			BodySize: 200,
   108  			BodyBlocks: []bfs.BlockInfo{
   109  				{
   110  					OriginOffsetFrom: 0,
   111  					OriginOffsetTo:   100,
   112  				},
   113  				{
   114  					OriginOffsetFrom: 100,
   115  					OriginOffsetTo:   199,
   116  				},
   117  			},
   118  		}
   119  		header.Compact()
   120  		a.IsFalse(header.IsCompleted)
   121  	}
   122  
   123  	{
   124  		var header = &bfs.FileHeader{
   125  			Version:  1,
   126  			Status:   200,
   127  			BodySize: 200,
   128  			BodyBlocks: []bfs.BlockInfo{
   129  				{
   130  					OriginOffsetFrom: 0,
   131  					OriginOffsetTo:   100,
   132  				},
   133  				{
   134  					OriginOffsetFrom: 101,
   135  					OriginOffsetTo:   200,
   136  				},
   137  			},
   138  		}
   139  		header.Compact()
   140  		a.IsFalse(header.IsCompleted)
   141  	}
   142  }
   143  
   144  func TestFileHeader_Compact_Merge(t *testing.T) {
   145  	var a = assert.NewAssertion(t)
   146  
   147  	var header = &bfs.FileHeader{
   148  		Version: 1,
   149  		Status:  200,
   150  		HeaderBlocks: []bfs.BlockInfo{
   151  			{
   152  				BFileOffsetFrom:  1000,
   153  				BFileOffsetTo:    1100,
   154  				OriginOffsetFrom: 1200,
   155  				OriginOffsetTo:   1300,
   156  			},
   157  			{
   158  				BFileOffsetFrom:  1100,
   159  				BFileOffsetTo:    1200,
   160  				OriginOffsetFrom: 1300,
   161  				OriginOffsetTo:   1400,
   162  			},
   163  		},
   164  		BodyBlocks: []bfs.BlockInfo{
   165  			{
   166  				BFileOffsetFrom:  0,
   167  				BFileOffsetTo:    100,
   168  				OriginOffsetFrom: 200,
   169  				OriginOffsetTo:   300,
   170  			},
   171  			{
   172  				BFileOffsetFrom:  100,
   173  				BFileOffsetTo:    200,
   174  				OriginOffsetFrom: 300,
   175  				OriginOffsetTo:   400,
   176  			},
   177  			{
   178  				BFileOffsetFrom:  200,
   179  				BFileOffsetTo:    300,
   180  				OriginOffsetFrom: 400,
   181  				OriginOffsetTo:   500,
   182  			},
   183  		},
   184  	}
   185  	header.Compact()
   186  	logs.PrintAsJSON(header.HeaderBlocks)
   187  	logs.PrintAsJSON(header.BodyBlocks)
   188  
   189  	a.IsTrue(len(header.HeaderBlocks) == 1)
   190  	a.IsTrue(len(header.BodyBlocks) == 1)
   191  }
   192  
   193  func TestFileHeader_Compact_Merge2(t *testing.T) {
   194  	var header = &bfs.FileHeader{
   195  		Version: 1,
   196  		Status:  200,
   197  		BodyBlocks: []bfs.BlockInfo{
   198  			{
   199  				BFileOffsetFrom:  0,
   200  				BFileOffsetTo:    100,
   201  				OriginOffsetFrom: 200,
   202  				OriginOffsetTo:   300,
   203  			},
   204  			{
   205  				BFileOffsetFrom:  101,
   206  				BFileOffsetTo:    200,
   207  				OriginOffsetFrom: 301,
   208  				OriginOffsetTo:   400,
   209  			},
   210  			{
   211  				BFileOffsetFrom:  200,
   212  				BFileOffsetTo:    300,
   213  				OriginOffsetFrom: 400,
   214  				OriginOffsetTo:   500,
   215  			},
   216  		},
   217  	}
   218  	header.Compact()
   219  	logs.PrintAsJSON(header.BodyBlocks)
   220  }
   221  
   222  func TestFileHeader_Clone(t *testing.T) {
   223  	var a = assert.NewAssertion(t)
   224  
   225  	var header = &bfs.FileHeader{
   226  		Version: 1,
   227  		Status:  200,
   228  		BodyBlocks: []bfs.BlockInfo{
   229  			{
   230  				BFileOffsetFrom: 0,
   231  				BFileOffsetTo:   100,
   232  			},
   233  		},
   234  	}
   235  
   236  	var clonedHeader = header.Clone()
   237  	t.Log("=== cloned header ===")
   238  	logs.PrintAsJSON(clonedHeader, t)
   239  	a.IsTrue(len(clonedHeader.BodyBlocks) == 1)
   240  
   241  	header.BodyBlocks = append(header.BodyBlocks, bfs.BlockInfo{
   242  		BFileOffsetFrom: 100,
   243  		BFileOffsetTo:   200,
   244  	})
   245  	header.BodyBlocks = append(header.BodyBlocks, bfs.BlockInfo{
   246  		BFileOffsetFrom: 300,
   247  		BFileOffsetTo:   400,
   248  	})
   249  
   250  	clonedHeader.BodyBlocks[0].OriginOffsetFrom = 100000000
   251  
   252  	t.Log("=== after changed ===")
   253  	logs.PrintAsJSON(clonedHeader, t)
   254  	a.IsTrue(len(clonedHeader.BodyBlocks) == 1)
   255  
   256  	t.Log("=== original header ===")
   257  	logs.PrintAsJSON(header, t)
   258  	a.IsTrue(header.BodyBlocks[0].OriginOffsetFrom != clonedHeader.BodyBlocks[0].OriginOffsetFrom)
   259  }
   260  
   261  func TestFileHeader_Encode(t *testing.T) {
   262  	{
   263  		var header = &bfs.FileHeader{
   264  			Version:    1,
   265  			Status:     200,
   266  			ModifiedAt: fasttime.Now().Unix(),
   267  			ExpiresAt:  fasttime.Now().Unix() + 3600,
   268  			BodySize:   1 << 20,
   269  			HeaderSize: 1 << 10,
   270  			BodyBlocks: []bfs.BlockInfo{
   271  				{
   272  					BFileOffsetFrom: 1 << 10,
   273  					BFileOffsetTo:   1 << 20,
   274  				},
   275  			},
   276  		}
   277  		data, err := header.Encode(bfs.Hash("123456"))
   278  		if err != nil {
   279  			t.Fatal(err)
   280  		}
   281  		jsonBytes, _ := json.Marshal(header)
   282  		t.Log(len(header.BodyBlocks), "blocks", len(data), "bytes", "json:", len(jsonBytes), "bytes")
   283  
   284  		_, _, _, err = bfs.DecodeMetaBlock(data)
   285  		if err != nil {
   286  			t.Fatal(err)
   287  		}
   288  	}
   289  
   290  	{
   291  		var header = &bfs.FileHeader{
   292  			Version:    1,
   293  			Status:     200,
   294  			BodyBlocks: []bfs.BlockInfo{},
   295  		}
   296  		var offset int64
   297  		for {
   298  			var end = offset + 16<<10
   299  			if end > 256<<10 {
   300  				break
   301  			}
   302  
   303  			header.BodyBlocks = append(header.BodyBlocks, bfs.BlockInfo{
   304  				BFileOffsetFrom: offset,
   305  				BFileOffsetTo:   end,
   306  			})
   307  
   308  			offset = end
   309  		}
   310  		data, err := header.Encode(bfs.Hash("123456"))
   311  		if err != nil {
   312  			t.Fatal(err)
   313  		}
   314  		jsonBytes, _ := json.Marshal(header)
   315  		t.Log(len(header.BodyBlocks), "blocks", len(data), "bytes", "json:", len(jsonBytes), "bytes")
   316  	}
   317  
   318  	{
   319  		var header = &bfs.FileHeader{
   320  			Version:    1,
   321  			Status:     200,
   322  			BodyBlocks: []bfs.BlockInfo{},
   323  		}
   324  		var offset int64
   325  		for {
   326  			var end = offset + 16<<10
   327  			if end > 512<<10 {
   328  				break
   329  			}
   330  
   331  			header.BodyBlocks = append(header.BodyBlocks, bfs.BlockInfo{
   332  				BFileOffsetFrom: offset,
   333  				BFileOffsetTo:   end,
   334  			})
   335  
   336  			offset = end
   337  		}
   338  		data, err := header.Encode(bfs.Hash("123456"))
   339  		if err != nil {
   340  			t.Fatal(err)
   341  		}
   342  		jsonBytes, _ := json.Marshal(header)
   343  		t.Log(len(header.BodyBlocks), "blocks", len(data), "bytes", "json:", len(jsonBytes), "bytes")
   344  	}
   345  
   346  	{
   347  		var header = &bfs.FileHeader{
   348  			Version:    1,
   349  			Status:     200,
   350  			BodyBlocks: []bfs.BlockInfo{},
   351  		}
   352  		var offset int64
   353  		for {
   354  			var end = offset + 16<<10
   355  			if end > 1<<20 {
   356  				break
   357  			}
   358  
   359  			header.BodyBlocks = append(header.BodyBlocks, bfs.BlockInfo{
   360  				BFileOffsetFrom: offset,
   361  				BFileOffsetTo:   end,
   362  			})
   363  
   364  			offset = end
   365  		}
   366  		data, err := header.Encode(bfs.Hash("123456"))
   367  		if err != nil {
   368  			t.Fatal(err)
   369  		}
   370  		jsonBytes, _ := json.Marshal(header)
   371  		t.Log(len(header.BodyBlocks), "blocks", len(data), "bytes", "json:", len(jsonBytes), "bytes")
   372  	}
   373  }
   374  
   375  func BenchmarkFileHeader_Compact(b *testing.B) {
   376  	for i := 0; i < b.N; i++ {
   377  		var header = &bfs.FileHeader{
   378  			Version:    1,
   379  			Status:     200,
   380  			BodySize:   200,
   381  			BodyBlocks: nil,
   382  		}
   383  
   384  		for j := 0; j < 100; j++ {
   385  			header.BodyBlocks = append(header.BodyBlocks, bfs.BlockInfo{
   386  				OriginOffsetFrom: int64(j * 100),
   387  				OriginOffsetTo:   int64(j * 200),
   388  				BFileOffsetFrom:  0,
   389  				BFileOffsetTo:    0,
   390  			})
   391  		}
   392  
   393  		header.Compact()
   394  	}
   395  }
   396  
   397  func BenchmarkFileHeader_Encode(b *testing.B) {
   398  	runtime.GOMAXPROCS(12)
   399  
   400  	b.RunParallel(func(pb *testing.PB) {
   401  		for pb.Next() {
   402  			var header = &bfs.FileHeader{
   403  				Version:    1,
   404  				Status:     200,
   405  				ModifiedAt: rand.Int63(),
   406  				BodySize:   rand.Int63(),
   407  				BodyBlocks: []bfs.BlockInfo{},
   408  			}
   409  			var offset int64
   410  			for {
   411  				var end = offset + 16<<10
   412  				if end > 2<<20 {
   413  					break
   414  				}
   415  
   416  				header.BodyBlocks = append(header.BodyBlocks, bfs.BlockInfo{
   417  					BFileOffsetFrom: offset + int64(rand.Int()%1000000),
   418  					BFileOffsetTo:   end + int64(rand.Int()%1000000),
   419  				})
   420  
   421  				offset = end
   422  			}
   423  
   424  			var hash = bfs.Hash("123456")
   425  
   426  			_, err := header.Encode(hash)
   427  			if err != nil {
   428  				b.Fatal(err)
   429  			}
   430  		}
   431  	})
   432  }