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

     1  // Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
     2  
     3  package caches_test
     4  
     5  import (
     6  	"github.com/TeaOSLab/EdgeNode/internal/caches"
     7  	"github.com/TeaOSLab/EdgeNode/internal/utils/testutils"
     8  	"github.com/iwind/TeaGo/Tea"
     9  	_ "github.com/iwind/TeaGo/bootstrap"
    10  	"runtime"
    11  	"runtime/debug"
    12  	"testing"
    13  	"time"
    14  )
    15  
    16  func TestFileListDB_ListLFUItems(t *testing.T) {
    17  	if !testutils.IsSingleTesting() {
    18  		return
    19  	}
    20  
    21  	var db = caches.NewSQLiteFileListDB()
    22  
    23  	defer func() {
    24  		_ = db.Close()
    25  	}()
    26  
    27  	err := db.Open(Tea.Root + "/data/cache-db-large.db")
    28  	//err := db.Open(Tea.Root + "/data/cache-index/p1/db-0.db")
    29  	if err != nil {
    30  		t.Fatal(err)
    31  	}
    32  	err = db.Init()
    33  	if err != nil {
    34  		t.Fatal(err)
    35  	}
    36  
    37  	hashList, err := db.ListLFUItems(100)
    38  	if err != nil {
    39  		t.Fatal(err)
    40  	}
    41  	t.Log("[", len(hashList), "]", hashList)
    42  }
    43  
    44  func TestFileListDB_CleanMatchKey(t *testing.T) {
    45  	if !testutils.IsSingleTesting() {
    46  		return
    47  	}
    48  
    49  	var db = caches.NewSQLiteFileListDB()
    50  
    51  	defer func() {
    52  		_ = db.Close()
    53  	}()
    54  
    55  	err := db.Open(Tea.Root + "/data/cache-db-large.db")
    56  	if err != nil {
    57  		t.Fatal(err)
    58  	}
    59  
    60  	err = db.Init()
    61  	if err != nil {
    62  		t.Fatal(err)
    63  	}
    64  
    65  	err = db.CleanMatchKey("https://*.goedge.cn/large-text")
    66  	if err != nil {
    67  		t.Fatal(err)
    68  	}
    69  
    70  	err = db.CleanMatchKey("https://*.goedge.cn:1234/large-text?%2B____")
    71  	if err != nil {
    72  		t.Fatal(err)
    73  	}
    74  }
    75  
    76  func TestFileListDB_CleanMatchPrefix(t *testing.T) {
    77  	if !testutils.IsSingleTesting() {
    78  		return
    79  	}
    80  
    81  	var db = caches.NewSQLiteFileListDB()
    82  
    83  	defer func() {
    84  		_ = db.Close()
    85  	}()
    86  
    87  	err := db.Open(Tea.Root + "/data/cache-db-large.db")
    88  	if err != nil {
    89  		t.Fatal(err)
    90  	}
    91  
    92  	err = db.Init()
    93  	if err != nil {
    94  		t.Fatal(err)
    95  	}
    96  
    97  	err = db.CleanMatchPrefix("https://*.goedge.cn/large-text")
    98  	if err != nil {
    99  		t.Fatal(err)
   100  	}
   101  
   102  	err = db.CleanMatchPrefix("https://*.goedge.cn:1234/large-text?%2B____")
   103  	if err != nil {
   104  		t.Fatal(err)
   105  	}
   106  }
   107  
   108  func TestFileListDB_Memory(t *testing.T) {
   109  	if !testutils.IsSingleTesting() {
   110  		return
   111  	}
   112  
   113  	var db = caches.NewSQLiteFileListDB()
   114  
   115  	defer func() {
   116  		_ = db.Close()
   117  	}()
   118  
   119  	err := db.Open(Tea.Root + "/data/cache-index/p1/db-0.db")
   120  	if err != nil {
   121  		t.Fatal(err)
   122  	}
   123  
   124  	err = db.Init()
   125  	if err != nil {
   126  		t.Fatal(err)
   127  	}
   128  
   129  	t.Log(db.Total())
   130  
   131  	// load hashes
   132  	var maxId int64
   133  	var hashList []string
   134  	var before = time.Now()
   135  	for i := 0; i < 1_000; i++ {
   136  		hashList, maxId, err = db.ListHashes(maxId)
   137  		if err != nil {
   138  			t.Fatal(err)
   139  		}
   140  		if len(hashList) == 0 {
   141  			t.Log("hashes loaded", time.Since(before).Seconds()*1000, "ms")
   142  			break
   143  		}
   144  		if i%100 == 0 {
   145  			t.Log(i)
   146  		}
   147  	}
   148  
   149  	runtime.GC()
   150  	debug.FreeOSMemory()
   151  
   152  	//time.Sleep(600 * time.Second)
   153  
   154  	for i := 0; i < 1_000; i++ {
   155  		_, err = db.ListLFUItems(5000)
   156  		if err != nil {
   157  			t.Fatal(err)
   158  		}
   159  		if i%100 == 0 {
   160  			t.Log(i)
   161  		}
   162  	}
   163  
   164  	t.Log("loaded")
   165  
   166  	runtime.GC()
   167  	debug.FreeOSMemory()
   168  
   169  	time.Sleep(600 * time.Second)
   170  }