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

perf: improve throughput of the Do and DoMulti when -cpu=1 #291

Merged
merged 2 commits into from
Jul 2, 2023

Conversation

rueian
Copy link
Collaborator

@rueian rueian commented Jul 1, 2023

Improve throughput of the Do and DoMulti by 15% and 8% when -cpu=1.

# go test -run='^$' -bench=. -benchmem -benchtime=2s -count=10 -cpu=1

goos: linux
goarch: amd64
pkg: github.com/redis/rueidis/ben
cpu: AMD EPYC 7B12
                              │   old-1.txt   │              new-1.txt               │
                              │    sec/op     │    sec/op     vs base                │
/OneNode/128_RueidisSingle      102.39µ ±  7%   86.42µ ± 11%  -15.60% (p=0.001 n=10)
/OneNode/128_RueidisDoMulti      242.8µ ±  5%   223.1µ ±  6%   -8.11% (p=0.019 n=10)
geomean                          146.8µ         139.4µ         -5.03%

                              │  old-1.txt   │               new-1.txt                │
                              │     B/op     │     B/op      vs base                  │
/OneNode/128_RueidisSingle       243.50 ± 4%     64.00 ± 0%  -73.72% (p=0.000 n=10)
/OneNode/128_RueidisDoMulti     18.46Ki ± 0%   18.02Ki ± 0%   -2.37% (p=0.000 n=10)
geomean                         2.389Ki        1.700Ki       -28.83%
¹ all samples are equal

                              │ old-1.txt  │              new-1.txt              │
                              │ allocs/op  │ allocs/op   vs base                 │
/OneNode/128_RueidisSingle      1.000 ± 0%   1.000 ± 0%       ~ (p=1.000 n=10) ¹
/OneNode/128_RueidisDoMulti     131.0 ± 0%   130.0 ± 0%  -0.76% (p=0.000 n=10)
geomean                         27.27        27.22       -0.19%
¹ all samples are equal

Benchmark source:

package ops

import (
	"context"
	"github.com/redis/go-redis/v9"
	"github.com/redis/rueidis"
	"math/rand"
	"strconv"
	"testing"
)

var charset = []byte("abcdefghijklmnopqrstuvwxyz")

func randstr(n int) string {
	b := make([]byte, n)
	for i := range b {
		b[i] = charset[rand.Intn(len(charset))]
	}
	return string(b)
}

func Benchmark(b *testing.B) {
	testfn := func(b *testing.B, n int, addresses []string) {
		ns := strconv.Itoa(n)
		makeclient := func(addresses []string) rueidis.Client {
			client, err := rueidis.NewClient(rueidis.ClientOption{
				InitAddress: addresses,
			})
			if err != nil {
				panic(err)
			}
			return client
		}

		// prepare keys
		keys := make(map[string]string, n)
		cmds := make(rueidis.Commands, 0, n)

		{
			client := makeclient(addresses)
			for i := 0; i < n; i++ {
				keys[randstr(10)] = randstr(50)
			}
			for k, v := range keys {
				cmds = append(cmds, client.B().Set().Key(k).Value(v).Build())
			}
			if err := client.Do(context.Background(), client.B().Flushall().Build()).Error(); err != nil {
				panic(err)
			}
			resps := client.DoMulti(context.Background(), cmds...)
			if len(resps) != len(keys) {
				panic("worn")
			}
			client.Close()
		}
		b.Run(ns+" RueidisSingle", func(b *testing.B) {
			client := makeclient(addresses)
			defer client.Close()
			b.ResetTimer()
			b.RunParallel(func(pb *testing.PB) {
				for pb.Next() {
					for k := range keys {
						_, err := client.Do(context.Background(), client.B().Get().Key(k).Build()).ToString()
						if err != nil {
							panic(err)
						}
						break
					}
				}
			})
			b.StopTimer()
		})
		b.Run(ns+" RueidisDoMulti", func(b *testing.B) {
			client := makeclient(addresses)
			defer client.Close()
			commands := make([]rueidis.Completed, 0, len(keys))
			for k := range keys {
				commands = append(commands, client.B().Get().Key(k).Build().Pin())
			}
			b.ResetTimer()
			b.RunParallel(func(pb *testing.PB) {
				for pb.Next() {
					ret := client.DoMulti(context.Background(), commands...)
					for _, result := range ret {
						_, err := result.ToString()
						if err != nil {
							panic(err)
						}
					}
				}
			})
			b.StopTimer()
		})
	}

	b.Run("OneNode", func(b *testing.B) {
		for _, n := range []int{128} {
			testfn(b, n, []string{"10.140.0.7:6379"})
		}
	})
}

@codecov-commenter
Copy link

codecov-commenter commented Jul 1, 2023

Codecov Report

Patch coverage: 100.00% and project coverage change: +0.03 🎉

Comparison is base (47c3d7c) 97.67% compared to head (1aba42b) 97.71%.

❗ Your organization is not using the GitHub App Integration. As a result you may experience degraded service beginning May 15th. Please install the Github App Integration for your organization. Read more.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #291      +/-   ##
==========================================
+ Coverage   97.67%   97.71%   +0.03%     
==========================================
  Files          70       70              
  Lines       29683    29702      +19     
==========================================
+ Hits        28993    29022      +29     
+ Misses        583      577       -6     
+ Partials      107      103       -4     
Impacted Files Coverage Δ
mux.go 100.00% <100.00%> (ø)
pipe.go 100.00% <100.00%> (ø)
ring.go 100.00% <100.00%> (ø)
rueidis.go 100.00% <100.00%> (ø)

... and 3 files with indirect coverage changes

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

@rueian rueian force-pushed the perf-reduce-domulti-ch-send branch 3 times, most recently from 3a861f3 to 6b5b154 Compare July 2, 2023 08:21
@rueian rueian force-pushed the perf-reduce-domulti-ch-send branch from 6b5b154 to 1aba42b Compare July 2, 2023 09:46
@rueian rueian changed the title perf: improve DoMulti by reducing chansends perf: improve throughput of the Do and DoMulti when -cpu=1 Jul 2, 2023
@rueian rueian merged commit 9f8a06d into main Jul 2, 2023
1 check passed
@rueian rueian deleted the perf-reduce-domulti-ch-send branch July 8, 2023 00:32
@rueian rueian mentioned this pull request Aug 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants