Skip to content
This repository was archived by the owner on Jun 23, 2025. It is now read-only.

Commit 4d1a919

Browse files
committed
add: benchmark test for start method in worker.go
1 parent a2f709b commit 4d1a919

File tree

4 files changed

+120
-77
lines changed

4 files changed

+120
-77
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ require (
4242
github.com/cespare/xxhash/v2 v2.3.0
4343
github.com/cockroachdb/swiss v0.0.0-20240612210725-f4de07ae6964
4444
github.com/dgryski/go-farm v0.0.0-20240924180020-3414d57e47da
45-
github.com/dicedb/dicedb-go v0.0.0-20241015181607-d31c1df12107
45+
github.com/dicedb/dicedb-go v0.0.0-20241026093718-570de4575be3
4646
github.com/gobwas/glob v0.2.3
4747
github.com/google/btree v1.1.3
4848
github.com/google/go-cmp v0.6.0

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/r
3232
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
3333
github.com/dicedb/dicedb-go v0.0.0-20241015181607-d31c1df12107 h1:sL5dXtCsogSMP/afS2K2vVMMYFqJy02EezeRXpZnGy0=
3434
github.com/dicedb/dicedb-go v0.0.0-20241015181607-d31c1df12107/go.mod h1:iaOsphlvjJ87VL/5d32ZgeQYxhYS51k7/bvFKro7lWk=
35+
github.com/dicedb/dicedb-go v0.0.0-20241026093718-570de4575be3 h1:JvnAibMNGA0vQH+T47Y/d5/POURIvfJl3fFk0GIEBkQ=
36+
github.com/dicedb/dicedb-go v0.0.0-20241026093718-570de4575be3/go.mod h1:p7x5/3S6wBEmiRMwxavj1I1P1xsSVQS6fcSbeai5ic4=
3537
github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8=
3638
github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
3739
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
package server
2+
3+
import(
4+
"testing"
5+
"runtime"
6+
"context"
7+
"sync"
8+
"log/slog"
9+
"fmt"
10+
"errors"
11+
"net/http"
12+
"time"
13+
14+
15+
dice "github.com/dicedb/dicedb-go"
16+
diceerrors "github.com/dicedb/dice/internal/errors"
17+
"github.com/dicedb/dice/internal/shard"
18+
"github.com/dicedb/dice/config"
19+
dstore "github.com/dicedb/dice/internal/store"
20+
"github.com/dicedb/dice/internal/server/resp"
21+
"github.com/dicedb/dice/internal/worker"
22+
"github.com/dicedb/dice/internal/server/abstractserver"
23+
24+
)
25+
26+
func BenchmarkStart(b *testing.B){
27+
numShards := runtime.NumCPU()
28+
runtime.GOMAXPROCS(numShards)
29+
30+
var queryWatchChan chan dstore.QueryWatchEvent
31+
var cmdWatchChan chan dstore.CmdWatchEvent
32+
var serverErrCh = make(chan error, 2)
33+
34+
shardManager := shard.NewShardManager(uint8(numShards), queryWatchChan, cmdWatchChan, serverErrCh)
35+
36+
var ctx, cancel = context.WithCancel(context.Background())
37+
38+
var wg = sync.WaitGroup{}
39+
40+
wg.Add(1)
41+
go func() {
42+
defer wg.Done()
43+
shardManager.Run(ctx)
44+
}()
45+
46+
var serverWg sync.WaitGroup
47+
48+
workerManager := worker.NewWorkerManager(config.DiceConfig.Performance.MaxClients, shardManager)
49+
respServer := resp.NewServer(shardManager, workerManager, cmdWatchChan, serverErrCh)
50+
51+
serverWg.Add(1)
52+
go runServer(ctx, &serverWg, respServer, serverErrCh)
53+
54+
time.Sleep(10 * time.Second)
55+
56+
commands := []string{"PING", "SET foo bar", "GET foo", "INCR counter", "DEL foo"}
57+
concurrencyLevels := []int{10, 50, 100, 200}
58+
59+
for _, concurrency := range concurrencyLevels {
60+
b.Run("Concurrency="+fmt.Sprintf("%d",concurrency), func(b *testing.B) {
61+
client := dice.NewClient(&dice.Options{
62+
Addr: "localhost:7379",
63+
Password: "",
64+
DB: 0,
65+
})
66+
defer client.Close()
67+
68+
b.ResetTimer()
69+
for i := 0; i < b.N; i++ {
70+
var cmdWg sync.WaitGroup
71+
cmdWg.Add(concurrency)
72+
for j := 0; j < concurrency; j++ {
73+
go func() {
74+
defer cmdWg.Done()
75+
for _, cmd := range commands {
76+
switch cmd {
77+
case "PING":
78+
client.Ping(ctx)
79+
case "SET foo bar":
80+
client.Set(ctx, "foo", "bar", 0)
81+
case "GET foo":
82+
client.Get(ctx, "foo")
83+
case "INCR counter":
84+
client.Incr(ctx, "counter")
85+
case "DEL foo":
86+
client.Del(ctx, "foo")
87+
}
88+
}
89+
}()
90+
}
91+
cmdWg.Wait()
92+
}
93+
})
94+
}
95+
cancel()
96+
wg.Wait()
97+
serverWg.Wait()
98+
}
99+
100+
func runServer(ctx context.Context, wg *sync.WaitGroup, srv abstractserver.AbstractServer, errCh chan<- error) {
101+
defer wg.Done()
102+
if err := srv.Run(ctx); err != nil {
103+
switch {
104+
case errors.Is(err, context.Canceled):
105+
slog.Debug(fmt.Sprintf("%T was canceled", srv))
106+
case errors.Is(err, diceerrors.ErrAborted):
107+
slog.Debug(fmt.Sprintf("%T received abort command", srv))
108+
case errors.Is(err, http.ErrServerClosed):
109+
slog.Debug(fmt.Sprintf("%T received abort command", srv))
110+
default:
111+
slog.Error(fmt.Sprintf("%T error", srv), slog.Any("error", err))
112+
}
113+
errCh <- err
114+
} else {
115+
slog.Debug("bye.")
116+
}
117+
}

internal/worker/worker_test.go

Lines changed: 0 additions & 76 deletions
This file was deleted.

0 commit comments

Comments
 (0)