From 0a329cf0f3b83bbd1ab45536af768c8818168cc3 Mon Sep 17 00:00:00 2001 From: kpango Date: Thu, 23 Mar 2023 15:51:55 +0900 Subject: [PATCH] update deps Signed-off-by: kpango --- gache.go | 1 - gache_benchmark_test.go | 252 ++++++++++++++++++++++++++++++++++++++++ go.mod | 4 +- go.sum | 10 +- 4 files changed, 259 insertions(+), 8 deletions(-) diff --git a/gache.go b/gache.go index e5d350a..31e9e87 100644 --- a/gache.go +++ b/gache.go @@ -329,7 +329,6 @@ func (g *gache[V]) Write(ctx context.Context, w io.Writer) error { return true }) gob.Register(map[string]V{}) - return gob.NewEncoder(w).Encode(&m) } diff --git a/gache_benchmark_test.go b/gache_benchmark_test.go index ac1e839..4520937 100644 --- a/gache_benchmark_test.go +++ b/gache_benchmark_test.go @@ -1,7 +1,13 @@ package gache import ( + "fmt" "math/rand" + "os" + "runtime" + "runtime/debug" + "strconv" + "strings" "sync" "testing" "time" @@ -165,3 +171,249 @@ func BenchmarkGacheSetBigDataWithTTL(b *testing.B) { func(k, v string, t time.Duration) { g.SetWithExpire(k, v, t) }, func(k string) { g.Get(k) }) } + +func TestMain(m *testing.M) { + setup() + code := m.Run() + shutdown() + os.Exit(code) +} + +func setup() { + debug.SetGCPercent(10) +} + +func shutdown() { + PrintGCPause() + PrintMem() + PrintRate() +} + +func BenchmarkHeavyMixedInt_gache(b *testing.B) { + gc := New[int]().SetDefaultExpire(10 * time.Second) + var wg sync.WaitGroup + for index := 0; index < 10000; index++ { + wg.Add(1) + go func() { + for i := 0; i < 8192; i++ { + gc.Set(Int64Key(int64(i)), i+1) + } + wg.Done() + }() + wg.Add(1) + go func() { + for i := 0; i < 8192; i++ { + gc.Get(Int64Key(int64(i))) + } + wg.Done() + }() + } + wg.Wait() + + AddMem() +} + +func BenchmarkPutInt_gache(b *testing.B) { + gc := New[int]().SetDefaultExpire(10 * time.Second) + // slen = 512 + for i := 0; i < b.N; i++ { + gc.Set(Int64Key(int64(i)), i+1) + } +} + +func BenchmarkGetInt_gache(b *testing.B) { + gc := New[string]().SetDefaultExpire(10 * time.Second) + // slen = 512 + gc.Set("0", "0") + for i := 0; i < b.N; i++ { + gc.Get("0") + } +} + +func BenchmarkPut1K_gache(b *testing.B) { + gc := New[[]byte]().SetDefaultExpire(10 * time.Second) + // slen = 512 + for i := 0; i < b.N; i++ { + gc.Set(Int64Key(int64(i)), Data1K) + } +} + +func BenchmarkPut1M_gache(b *testing.B) { + gc := New[[]byte]().SetDefaultExpire(10 * time.Second) + // slen = 512 + for i := 0; i < b.N; i++ { + gc.Set(Int64Key(int64(i)), Data1M) + } +} + +func BenchmarkPutTinyObject_gache(b *testing.B) { + gc := New[dummyData]().SetDefaultExpire(10 * time.Second) + // slen = 512 + for i := 0; i < b.N; i++ { + gc.Set(Int64Key(int64(i)), dummyData{}) + } +} + +func BenchmarkChangeOutAllInt_gache(b *testing.B) { + gc := New[int]().SetDefaultExpire(10 * time.Second) + // slen = 512 + for i := 0; i < b.N*1024; i++ { + gc.Set(Int64Key(int64(i)), i+1) + } +} + +func BenchmarkHeavyReadInt_gache(b *testing.B) { + gc := New[int]().SetDefaultExpire(10 * time.Second) + GCPause() + + // slen = 512 + for i := 0; i < 1024; i++ { + gc.Set(Int64Key(int64(i)), i+1) + } + var wg sync.WaitGroup + for index := 0; index < 10000; index++ { + wg.Add(1) + go func() { + for i := 0; i < 1024; i++ { + gc.Get(Int64Key(int64(i))) + } + wg.Done() + }() + } + wg.Wait() + + AddGCPause() +} + +func BenchmarkHeavyWriteInt_gache(b *testing.B) { + gc := New[int]().SetDefaultExpire(10 * time.Second) + GCPause() + + // slen = 512 + var wg sync.WaitGroup + for index := 0; index < 10000; index++ { + start := index + wg.Add(1) + go func() { + for i := 0; i < 8192; i++ { + gc.Set(Int64Key(int64(i+start)), i+1) + } + wg.Done() + }() + } + wg.Wait() + + AddGCPause() +} + +func BenchmarkHeavyWrite1K_gache(b *testing.B) { + gc := New[[]byte]().SetDefaultExpire(10 * time.Second) + GCPause() + + // slen = 512 + var wg sync.WaitGroup + for index := 0; index < 10000; index++ { + start := index + wg.Add(1) + go func() { + for i := 0; i < 8192; i++ { + gc.Set(Int64Key(int64(i+start)), Data1K) + } + wg.Done() + }() + } + wg.Wait() + + AddGCPause() +} + +func Int64Key(d int64) string { + return strconv.FormatInt(d, 10) +} + +func randomString(n int) []byte { + b := make([]byte, n) + for i := range b { + b[i] = byte(rand.Intn(26) + 'a') + } + return b +} + +var ( + Data1K = randomString(1024) + Data1M = randomString(1048576) +) + +var previousPause time.Duration + +func GCPause() time.Duration { + runtime.GC() + var stats debug.GCStats + debug.ReadGCStats(&stats) + pause := stats.PauseTotal - previousPause + previousPause = stats.PauseTotal + return pause +} + +var gcResult = make(map[string]time.Duration, 0) + +func AddGCPause() { + pc, _, _, _ := runtime.Caller(1) + name := strings.Replace(runtime.FuncForPC(pc).Name(), "_", "GC_", 1) + name = name[strings.Index(name, "Benchmark"):] + if _, ok := gcResult[name]; !ok { + gcResult[name] = GCPause() + } +} + +func PrintGCPause() { + for k, v := range gcResult { + fmt.Printf("%s-1 1 %d ns/op\n", k, v) + } +} + +func PrintMem() { + for k, v := range memResult { + fmt.Printf("%s-1 1 %d B\n", k, v) + } +} + +var memResult = make(map[string]uint64, 0) + +func AddMem() { + var ms runtime.MemStats + runtime.ReadMemStats(&ms) + pc, _, _, _ := runtime.Caller(1) + name := strings.Replace(runtime.FuncForPC(pc).Name(), "_", "Mem_", 1) + name = name[strings.Index(name, "Benchmark"):] + if _, ok := memResult[name]; !ok { + memResult[name] = ms.Sys + } +} + +var rateResult = make(map[string]float64, 0) + +func AddRate(r float64) { + var ms runtime.MemStats + runtime.ReadMemStats(&ms) + pc, _, _, _ := runtime.Caller(1) + name := runtime.FuncForPC(pc).Name() + name = name[strings.Index(name, "Benchmark"):] + if _, ok := rateResult[name]; !ok { + rateResult[name] = r + } +} + +func PrintRate() { + for k, v := range rateResult { + fmt.Printf("%s-1 1 %.2f %%\n", k, 100.*v) + } +} + +type dummyData struct { + Name string + Age int32 + Gender int32 + Company string + Skills []string +} diff --git a/go.mod b/go.mod index ac35b33..96b78ea 100644 --- a/go.mod +++ b/go.mod @@ -5,12 +5,12 @@ go 1.20 require ( github.com/cornelk/hashmap v1.0.8 github.com/kpango/fastime v1.1.9 - github.com/kpango/glg v1.6.14 + github.com/kpango/glg v1.6.15 github.com/zeebo/xxh3 v1.0.2 golang.org/x/sync v0.1.0 ) require ( - github.com/goccy/go-json v0.9.11 // indirect + github.com/goccy/go-json v0.10.2 // indirect github.com/klauspost/cpuid/v2 v2.0.9 // indirect ) diff --git a/go.sum b/go.sum index d90d917..5015428 100644 --- a/go.sum +++ b/go.sum @@ -1,20 +1,20 @@ github.com/cornelk/hashmap v1.0.8 h1:nv0AWgw02n+iDcawr5It4CjQIAcdMMKRrs10HOJYlrc= github.com/cornelk/hashmap v1.0.8/go.mod h1:RfZb7JO3RviW/rT6emczVuC/oxpdz4UsSB2LJSclR1k= -github.com/goccy/go-json v0.9.11 h1:/pAaQDLHEoCq/5FFmSKBswWmK6H0e8g4159Kc/X/nqk= -github.com/goccy/go-json v0.9.11/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I= +github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU= +github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I= github.com/klauspost/cpuid/v2 v2.0.9 h1:lgaqFMSdTdQYdZ04uHyN2d/eKdOMyi2YLSvlQIBFYa4= github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg= github.com/kpango/fastime v1.1.9 h1:xVQHcqyPt5M69DyFH7g1EPRns1YQNap9d5eLhl/Jy84= github.com/kpango/fastime v1.1.9/go.mod h1:vyD7FnUn08zxY4b/QFBZVG+9EWMYsNl+QF0uE46urD4= -github.com/kpango/glg v1.6.14 h1:Ss3ZvTQ23blUCDYizSAijiFTZsgGeYr/lanUGgQ10rY= -github.com/kpango/glg v1.6.14/go.mod h1:2djk7Zr4zKIYPHlORH8tJVlhCEh+XXW8W4K3qJyNXMI= +github.com/kpango/glg v1.6.15 h1:nw0xSxpSyrDIWHeb3dvnE08PW+SCbK+aYFETT75IeLA= +github.com/kpango/glg v1.6.15/go.mod h1:cmsc7Yeu8AS3wHLmN7bhwENXOpxfq+QoqxCIk2FneRk= github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0= github.com/zeebo/assert v1.3.0 h1:g7C04CbJuIDKNPFHmsk4hwZDO5O+kntRxzaUoNXj+IQ= github.com/zeebo/xxh3 v1.0.2 h1:xZmwmqxHZA8AI603jOQ0tMqmBr9lPeFwGg6d+xy9DC0= github.com/zeebo/xxh3 v1.0.2/go.mod h1:5NWz9Sef7zIDm2JHfFlcQvNekmcEl9ekUZQQKCYaDcA= go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw= go.uber.org/multierr v1.6.0 h1:y6IPFStTAIT5Ytl7/XYmHvzXQ7S3g/IeZW9hyZ5thw4= -go.uber.org/zap v1.23.0 h1:OjGQ5KQDEUawVHxNwQgPpiypGHOxo2mNZsOqTak4fFY= +go.uber.org/zap v1.24.0 h1:FiJd5l1UOLj0wCgbSE0rwwXHzEdAZS6hiiSnxJN/D60= golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o= golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ=