Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

using exp/rand to reduce memory usage #39

Merged
merged 2 commits into from
Jun 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions circuitbreaker/sre/sre.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions circuitbreaker/sre/sre_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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,
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
12 changes: 6 additions & 6 deletions hotkey/hotkey_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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() {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion internal/consistent/consistent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package consistent
import (
"bufio"
"encoding/base64"
"math/rand"
"os"
"runtime"
"sort"
Expand All @@ -16,6 +15,8 @@ import (
"testing"
"testing/quick"
"time"

"golang.org/x/exp/rand"
)

type member string
Expand Down
2 changes: 1 addition & 1 deletion ratelimit/bbr/bbr_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package bbr

import (
"math/rand"
"sync"
"sync/atomic"
"testing"
Expand All @@ -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 (
Expand Down
2 changes: 1 addition & 1 deletion subset/subset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions topk/heavykeeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions topk/heavykeeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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++ {
Expand All @@ -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)
Expand Down