From 1e56247b9c95ded1102bc367aad99d877ed1a139 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E7=A5=A5=E8=B6=85?= Date: Mon, 6 Nov 2023 20:26:57 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4=E7=BC=93=E5=AD=98=E7=B4=A2?= =?UTF-8?q?=E5=BC=95=E6=95=B0=E6=8D=AE=E5=BA=93=E7=BC=93=E5=AD=98=E5=B0=BA?= =?UTF-8?q?=E5=AF=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/caches/list_file_db.go | 6 +-- internal/caches/list_file_db_test.go | 80 ++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+), 3 deletions(-) diff --git a/internal/caches/list_file_db.go b/internal/caches/list_file_db.go index d796d42..ddc7a3b 100644 --- a/internal/caches/list_file_db.go +++ b/internal/caches/list_file_db.go @@ -65,10 +65,10 @@ func (this *FileListDB) Open(dbPath string) error { this.dbPath = dbPath // 动态调整Cache值 - var cacheSize = 32000 + var cacheSize = 512 var memoryGB = utils.SystemMemoryGB() - if memoryGB >= 8 { - cacheSize += 32000 * memoryGB / 8 + if memoryGB >= 1 { + cacheSize = 128 * memoryGB } // write db diff --git a/internal/caches/list_file_db_test.go b/internal/caches/list_file_db_test.go index 868579e..af19ef7 100644 --- a/internal/caches/list_file_db_test.go +++ b/internal/caches/list_file_db_test.go @@ -4,12 +4,20 @@ package caches_test import ( "github.com/TeaOSLab/EdgeNode/internal/caches" + "github.com/TeaOSLab/EdgeNode/internal/utils/testutils" "github.com/iwind/TeaGo/Tea" _ "github.com/iwind/TeaGo/bootstrap" + "runtime" + "runtime/debug" "testing" + "time" ) func TestFileListDB_ListLFUItems(t *testing.T) { + if !testutils.IsSingleTesting() { + return + } + var db = caches.NewFileListDB() defer func() { @@ -34,6 +42,10 @@ func TestFileListDB_ListLFUItems(t *testing.T) { } func TestFileListDB_CleanMatchKey(t *testing.T) { + if !testutils.IsSingleTesting() { + return + } + var db = caches.NewFileListDB() defer func() { @@ -62,6 +74,10 @@ func TestFileListDB_CleanMatchKey(t *testing.T) { } func TestFileListDB_CleanMatchPrefix(t *testing.T) { + if !testutils.IsSingleTesting() { + return + } + var db = caches.NewFileListDB() defer func() { @@ -88,3 +104,67 @@ func TestFileListDB_CleanMatchPrefix(t *testing.T) { t.Fatal(err) } } + +func TestFileListDB_Memory(t *testing.T) { + if !testutils.IsSingleTesting() { + return + } + + var db = caches.NewFileListDB() + + defer func() { + _ = db.Close() + }() + + err := db.Open(Tea.Root + "/data/db-0.db") + if err != nil { + t.Fatal(err) + } + + err = db.Init() + if err != nil { + t.Fatal(err) + } + + t.Log(db.Total()) + + // load hashes + var maxId int64 + var hashList []string + var before = time.Now() + for i := 0; i < 1_000; i++ { + hashList, maxId, err = db.ListHashes(maxId) + if err != nil { + t.Fatal(err) + } + if len(hashList) == 0 { + t.Log("hashes loaded", time.Since(before).Seconds()*1000, "ms") + break + } + if i%100 == 0 { + t.Log(i) + } + } + + runtime.GC() + debug.FreeOSMemory() + + //time.Sleep(600 * time.Second) + + for i := 0; i < 1_000; i++ { + _, err = db.ListLFUItems(5000) + if err != nil { + t.Fatal(err) + } + if i%100 == 0 { + t.Log(i) + } + } + + t.Log("loaded") + + runtime.GC() + debug.FreeOSMemory() + + time.Sleep(600 * time.Second) +}