Skip to content

Commit

Permalink
update deps
Browse files Browse the repository at this point in the history
Signed-off-by: kpango <kpango@vdaas.org>
  • Loading branch information
kpango committed Mar 23, 2023
1 parent 14bb7f1 commit 0a329cf
Show file tree
Hide file tree
Showing 4 changed files with 259 additions and 8 deletions.
1 change: 0 additions & 1 deletion gache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
252 changes: 252 additions & 0 deletions gache_benchmark_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
package gache

import (
"fmt"
"math/rand"
"os"
"runtime"
"runtime/debug"
"strconv"
"strings"
"sync"
"testing"
"time"
Expand Down Expand Up @@ -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
}
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
10 changes: 5 additions & 5 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=

0 comments on commit 0a329cf

Please sign in to comment.