github.com/TeaOSLab/EdgeNode@v1.3.8/internal/caches/partial_ranges_test.go (about)

     1  // Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
     2  
     3  package caches_test
     4  
     5  import (
     6  	"encoding/json"
     7  	"github.com/TeaOSLab/EdgeNode/internal/caches"
     8  	"github.com/iwind/TeaGo/assert"
     9  	"github.com/iwind/TeaGo/logs"
    10  	"testing"
    11  	"time"
    12  )
    13  
    14  func TestNewPartialRanges(t *testing.T) {
    15  	var r = caches.NewPartialRanges(0)
    16  	r.Add(1, 100)
    17  	r.Add(50, 300)
    18  
    19  	r.Add(30, 80)
    20  	r.Add(30, 100)
    21  	r.Add(30, 400)
    22  	r.Add(1000, 10000)
    23  	r.Add(200, 1000)
    24  	r.Add(200, 10040)
    25  
    26  	logs.PrintAsJSON(r.Ranges, t)
    27  	t.Log("max:", r.Max())
    28  }
    29  
    30  func TestNewPartialRanges1(t *testing.T) {
    31  	var a = assert.NewAssertion(t)
    32  
    33  	var r = caches.NewPartialRanges(0)
    34  	r.Add(1, 100)
    35  	r.Add(1, 101)
    36  	r.Add(1, 102)
    37  	r.Add(2, 103)
    38  	r.Add(200, 300)
    39  	r.Add(1, 1000)
    40  
    41  	var rs = r.Ranges
    42  	logs.PrintAsJSON(rs, t)
    43  	a.IsTrue(len(rs) == 1)
    44  	if len(rs) == 1 {
    45  		a.IsTrue(rs[0][0] == 1)
    46  		a.IsTrue(rs[0][1] == 1000)
    47  	}
    48  }
    49  
    50  func TestNewPartialRanges2(t *testing.T) {
    51  	// low -> high
    52  	var r = caches.NewPartialRanges(0)
    53  	r.Add(1, 100)
    54  	r.Add(1, 101)
    55  	r.Add(1, 102)
    56  	r.Add(2, 103)
    57  	r.Add(200, 300)
    58  	r.Add(301, 302)
    59  	r.Add(303, 304)
    60  	r.Add(250, 400)
    61  
    62  	var rs = r.Ranges
    63  	logs.PrintAsJSON(rs, t)
    64  }
    65  
    66  func TestNewPartialRanges3(t *testing.T) {
    67  	// high -> low
    68  	var r = caches.NewPartialRanges(0)
    69  	r.Add(301, 302)
    70  	r.Add(303, 304)
    71  	r.Add(200, 300)
    72  	r.Add(250, 400)
    73  
    74  	var rs = r.Ranges
    75  	logs.PrintAsJSON(rs, t)
    76  }
    77  
    78  func TestNewPartialRanges4(t *testing.T) {
    79  	// nearby
    80  	var r = caches.NewPartialRanges(0)
    81  	r.Add(301, 302)
    82  	r.Add(303, 304)
    83  	r.Add(305, 306)
    84  
    85  	r.Add(417, 417)
    86  	r.Add(410, 415)
    87  	r.Add(400, 409)
    88  
    89  	var rs = r.Ranges
    90  	logs.PrintAsJSON(rs, t)
    91  	t.Log(r.Contains(400, 416))
    92  }
    93  
    94  func TestNewPartialRanges5(t *testing.T) {
    95  	var r = caches.NewPartialRanges(0)
    96  	for j := 0; j < 1000; j++ {
    97  		r.Add(int64(j), int64(j+100))
    98  	}
    99  	logs.PrintAsJSON(r.Ranges, t)
   100  }
   101  
   102  func TestNewPartialRanges_Nearest(t *testing.T) {
   103  	{
   104  		// nearby
   105  		var r = caches.NewPartialRanges(0)
   106  		r.Add(301, 400)
   107  		r.Add(401, 500)
   108  		r.Add(501, 600)
   109  
   110  		t.Log(r.Nearest(100, 200))
   111  		t.Log(r.Nearest(300, 350))
   112  		t.Log(r.Nearest(302, 350))
   113  	}
   114  
   115  	{
   116  		// nearby
   117  		var r = caches.NewPartialRanges(0)
   118  		r.Add(301, 400)
   119  		r.Add(450, 500)
   120  		r.Add(550, 600)
   121  
   122  		t.Log(r.Nearest(100, 200))
   123  		t.Log(r.Nearest(300, 350))
   124  		t.Log(r.Nearest(302, 350))
   125  		t.Log(r.Nearest(302, 440))
   126  		t.Log(r.Nearest(302, 1000))
   127  	}
   128  }
   129  
   130  func TestNewPartialRanges_Large_Range(t *testing.T) {
   131  	var a = assert.NewAssertion(t)
   132  
   133  	var largeSize int64 = 10000000000000
   134  	t.Log(largeSize/1024/1024/1024, "G")
   135  
   136  	var r = caches.NewPartialRanges(0)
   137  	r.Add(1, largeSize)
   138  	var s = r.String()
   139  	t.Log(s)
   140  
   141  	r2, err := caches.NewPartialRangesFromData([]byte(s))
   142  	if err != nil {
   143  		t.Fatal(err)
   144  	}
   145  
   146  	a.IsTrue(largeSize == r2.Ranges[0][1])
   147  	logs.PrintAsJSON(r, t)
   148  }
   149  
   150  func TestPartialRanges_Encode_JSON(t *testing.T) {
   151  	var r = caches.NewPartialRanges(0)
   152  	for i := 0; i < 10; i++ {
   153  		r.Ranges = append(r.Ranges, [2]int64{int64(i * 100), int64(i*100 + 100)})
   154  	}
   155  	var before = time.Now()
   156  	data, err := json.Marshal(r)
   157  	if err != nil {
   158  		t.Fatal(err)
   159  	}
   160  	t.Log(time.Since(before).Seconds()*1000, "ms")
   161  	t.Log(len(data))
   162  }
   163  
   164  func TestPartialRanges_Encode_String(t *testing.T) {
   165  	var r = caches.NewPartialRanges(0)
   166  	r.BodySize = 1024
   167  	for i := 0; i < 10; i++ {
   168  		r.Ranges = append(r.Ranges, [2]int64{int64(i * 100), int64(i*100 + 100)})
   169  	}
   170  	var before = time.Now()
   171  	var data = r.String()
   172  	t.Log(time.Since(before).Seconds()*1000, "ms")
   173  	t.Log(len(data))
   174  
   175  	r2, err := caches.NewPartialRangesFromData([]byte(data))
   176  	if err != nil {
   177  		t.Fatal(err)
   178  	}
   179  	logs.PrintAsJSON(r2, t)
   180  }
   181  
   182  func TestPartialRanges_Version(t *testing.T) {
   183  	{
   184  		ranges, err := caches.NewPartialRangesFromData([]byte(`e:1668928495
   185  r:0,1048576
   186  r:1140260864,1140295164`))
   187  		if err != nil {
   188  			t.Fatal(err)
   189  		}
   190  		t.Log("version:", ranges.Version)
   191  	}
   192  	{
   193  		ranges, err := caches.NewPartialRangesFromData([]byte(`e:1668928495
   194  r:0,1048576
   195  r:1140260864,1140295164
   196  v:0
   197  `))
   198  		if err != nil {
   199  			t.Fatal(err)
   200  		}
   201  		t.Log("version:", ranges.Version)
   202  	}
   203  	{
   204  		ranges, err := caches.NewPartialRangesFromJSON([]byte(`{}`))
   205  		if err != nil {
   206  			t.Fatal(err)
   207  		}
   208  		t.Log("version:", ranges.Version)
   209  	}
   210  }
   211  
   212  func BenchmarkNewPartialRanges(b *testing.B) {
   213  	for i := 0; i < b.N; i++ {
   214  		var r = caches.NewPartialRanges(0)
   215  		for j := 0; j < 1000; j++ {
   216  			r.Add(int64(j), int64(j+100))
   217  		}
   218  	}
   219  }
   220  
   221  func BenchmarkPartialRanges_String(b *testing.B) {
   222  	var r = caches.NewPartialRanges(0)
   223  	r.BodySize = 1024
   224  	for i := 0; i < 10; i++ {
   225  		r.Ranges = append(r.Ranges, [2]int64{int64(i * 100), int64(i*100 + 100)})
   226  	}
   227  	b.ResetTimer()
   228  
   229  	for i := 0; i < b.N; i++ {
   230  		_ = r.String()
   231  	}
   232  }