From 99110a3f05f44234f21d65f79be71d1e2706937d Mon Sep 17 00:00:00 2001 From: realityone Date: Fri, 16 Jun 2023 11:04:32 +0800 Subject: [PATCH] using exp/rand to reduce memory usage (#39) * using exp/rand to reduce memory usage * test --- circuitbreaker/sre/sre.go | 4 ++-- circuitbreaker/sre/sre_test.go | 4 ++-- go.mod | 1 + go.sum | 2 ++ hotkey/hotkey_test.go | 12 ++++++------ internal/consistent/consistent_test.go | 3 ++- ratelimit/bbr/bbr_test.go | 2 +- subset/subset_test.go | 2 +- topk/heavykeeper.go | 3 +-- topk/heavykeeper_test.go | 6 +++--- 10 files changed, 21 insertions(+), 18 deletions(-) diff --git a/circuitbreaker/sre/sre.go b/circuitbreaker/sre/sre.go index 64c0caa..04ceee9 100644 --- a/circuitbreaker/sre/sre.go +++ b/circuitbreaker/sre/sre.go @@ -2,13 +2,13 @@ package sre import ( "math" - "math/rand" "sync" "sync/atomic" "time" "github.com/go-kratos/aegis/circuitbreaker" "github.com/go-kratos/aegis/internal/window" + "golang.org/x/exp/rand" ) // Option is sre breaker option function. @@ -100,7 +100,7 @@ func NewBreaker(opts ...Option) circuitbreaker.CircuitBreaker { stat := window.NewRollingCounter(counterOpts) return &Breaker{ stat: stat, - r: rand.New(rand.NewSource(time.Now().UnixNano())), + r: rand.New(rand.NewSource(uint64(time.Now().UnixNano()))), request: opt.request, k: 1 / opt.success, state: StateClosed, diff --git a/circuitbreaker/sre/sre_test.go b/circuitbreaker/sre/sre_test.go index 28d6c9f..ed3dbf7 100644 --- a/circuitbreaker/sre/sre_test.go +++ b/circuitbreaker/sre/sre_test.go @@ -2,12 +2,12 @@ package sre import ( "math" - "math/rand" "testing" "time" "github.com/go-kratos/aegis/internal/window" "github.com/stretchr/testify/assert" + "golang.org/x/exp/rand" ) func getSREBreaker() *Breaker { @@ -18,7 +18,7 @@ func getSREBreaker() *Breaker { stat := window.NewRollingCounter(counterOpts) return &Breaker{ stat: stat, - r: rand.New(rand.NewSource(time.Now().UnixNano())), + r: rand.New(rand.NewSource(uint64(time.Now().UnixNano()))), request: 100, k: 2, diff --git a/go.mod b/go.mod index e6087e7..521f04f 100644 --- a/go.mod +++ b/go.mod @@ -8,6 +8,7 @@ require ( github.com/shirou/gopsutil/v3 v3.23.2 github.com/stretchr/testify v1.8.2 github.com/twmb/murmur3 v1.1.6 + golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1 ) require ( diff --git a/go.sum b/go.sum index a89f2a4..377af87 100644 --- a/go.sum +++ b/go.sum @@ -35,6 +35,8 @@ github.com/twmb/murmur3 v1.1.6 h1:mqrRot1BRxm+Yct+vavLMou2/iJt0tNVTTC0QoIjaZg= github.com/twmb/murmur3 v1.1.6/go.mod h1:Qq/R7NUyOfr65zD+6Q5IHKsJLwP7exErjN6lyyq3OSQ= github.com/yusufpapurcu/wmi v1.2.2 h1:KBNDSne4vP5mbSWnJbO+51IMOXJB67QiYCSBrubbPRg= github.com/yusufpapurcu/wmi v1.2.2/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0= +golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1 h1:k/i9J1pBpvlfR+9QsetwPyERsqu1GIbi967PQMq3Ivc= +golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1/go.mod h1:V1LtkGg67GoY2N1AnLN78QLrzxkLyJw7RJb1gzOOz9w= golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201204225414-ed752295db88/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= diff --git a/hotkey/hotkey_test.go b/hotkey/hotkey_test.go index 494a668..2142d95 100644 --- a/hotkey/hotkey_test.go +++ b/hotkey/hotkey_test.go @@ -2,12 +2,12 @@ package hotkey import ( "fmt" - "math/rand" "strconv" "testing" "time" "github.com/stretchr/testify/assert" + "golang.org/x/exp/rand" ) func benchmarkHotkey(b *testing.B, autoCache bool, writePercent float64, whilelist ...*CacheRuleConfig) { @@ -23,8 +23,8 @@ func benchmarkHotkey(b *testing.B, autoCache bool, writePercent float64, whileli if err != nil { b.Fatalf("new hot key failed,err:=%v", err) } - random := rand.New(rand.NewSource(time.Now().Unix())) - zipf := rand.NewZipf(rand.New(rand.NewSource(time.Now().Unix())), 2, 2, 1000) + random := rand.New(rand.NewSource(uint64(time.Now().Unix()))) + zipf := rand.NewZipf(rand.New(rand.NewSource(uint64(time.Now().Unix()))), 2, 2, 1000) b.ResetTimer() b.RunParallel(func(pb *testing.PB) { for pb.Next() { @@ -165,7 +165,7 @@ func TestHotkeyBlacklist(t *testing.T) { if err != nil { t.Fatalf("new hot key failed,err:=%v", err) } - zipf := rand.NewZipf(rand.New(rand.NewSource(time.Now().Unix())), 2, 2, 1000) + zipf := rand.NewZipf(rand.New(rand.NewSource(uint64(time.Now().Unix()))), 2, 2, 1000) for i := 0; i < 100000; i++ { key := strconv.FormatInt(int64(zipf.Uint64()), 10) h.AddWithValue(key, key, 1) @@ -214,8 +214,8 @@ func testHotkeyHit(t *testing.T) { if err != nil { t.Fatalf("new hot key failed,err:=%v", err) } - random := rand.New(rand.NewSource(time.Now().Unix())) - zipf := rand.NewZipf(rand.New(rand.NewSource(time.Now().Unix())), 1.1, 2, 10000000) + random := rand.New(rand.NewSource(uint64(time.Now().Unix()))) + zipf := rand.NewZipf(rand.New(rand.NewSource(uint64(time.Now().Unix()))), 1.1, 2, 10000000) var total int var hit int for { diff --git a/internal/consistent/consistent_test.go b/internal/consistent/consistent_test.go index c3e5ff1..e2671e1 100644 --- a/internal/consistent/consistent_test.go +++ b/internal/consistent/consistent_test.go @@ -7,7 +7,6 @@ package consistent import ( "bufio" "encoding/base64" - "math/rand" "os" "runtime" "sort" @@ -16,6 +15,8 @@ import ( "testing" "testing/quick" "time" + + "golang.org/x/exp/rand" ) type member string diff --git a/ratelimit/bbr/bbr_test.go b/ratelimit/bbr/bbr_test.go index 13cf262..8bd55ad 100644 --- a/ratelimit/bbr/bbr_test.go +++ b/ratelimit/bbr/bbr_test.go @@ -1,7 +1,6 @@ package bbr import ( - "math/rand" "sync" "sync/atomic" "testing" @@ -10,6 +9,7 @@ import ( "github.com/go-kratos/aegis/internal/window" "github.com/go-kratos/aegis/ratelimit" "github.com/stretchr/testify/assert" + "golang.org/x/exp/rand" ) var ( diff --git a/subset/subset_test.go b/subset/subset_test.go index c1c2223..34c03df 100644 --- a/subset/subset_test.go +++ b/subset/subset_test.go @@ -3,12 +3,12 @@ package subset import ( "encoding/json" "io/ioutil" - "math/rand" "testing" "github.com/go-kratos/aegis/internal/consistent" "github.com/google/uuid" "github.com/stretchr/testify/assert" + "golang.org/x/exp/rand" ) type member string diff --git a/topk/heavykeeper.go b/topk/heavykeeper.go index 0042ea0..6a90e9d 100644 --- a/topk/heavykeeper.go +++ b/topk/heavykeeper.go @@ -5,11 +5,10 @@ package topk import ( "math" - "math/rand" "github.com/go-kratos/aegis/internal/minheap" - "github.com/twmb/murmur3" + "golang.org/x/exp/rand" ) const LOOKUP_TABLE = 256 diff --git a/topk/heavykeeper_test.go b/topk/heavykeeper_test.go index b32c414..a9c2919 100644 --- a/topk/heavykeeper_test.go +++ b/topk/heavykeeper_test.go @@ -2,17 +2,17 @@ package topk import ( "math" - "math/rand" "strconv" "testing" "time" "github.com/stretchr/testify/assert" + "golang.org/x/exp/rand" ) func TestTopkList(t *testing.T) { // zipfan distribution - zipf := rand.NewZipf(rand.New(rand.NewSource(time.Now().Unix())), 3, 2, 1000) + zipf := rand.NewZipf(rand.New(rand.NewSource(uint64(time.Now().Unix()))), 3, 2, 1000) topk := NewHeavyKeeper(10, 10000, 5, 0.925, 0) dataMap := make(map[string]int) for i := 0; i < 10000; i++ { @@ -33,7 +33,7 @@ func TestTopkList(t *testing.T) { } func BenchmarkAdd(b *testing.B) { - zipf := rand.NewZipf(rand.New(rand.NewSource(time.Now().Unix())), 2, 2, 1000) + zipf := rand.NewZipf(rand.New(rand.NewSource(uint64(time.Now().Unix()))), 2, 2, 1000) var data []string = make([]string, 1000) for i := 0; i < 1000; i++ { data[i] = strconv.FormatUint(zipf.Uint64(), 10)