-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add: benchmark test for start method in worker.go
- Loading branch information
Showing
4 changed files
with
120 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
package server | ||
|
||
import( | ||
"testing" | ||
"runtime" | ||
"context" | ||
"sync" | ||
"log/slog" | ||
"fmt" | ||
"errors" | ||
"net/http" | ||
"time" | ||
|
||
|
||
dice "github.com/dicedb/dicedb-go" | ||
diceerrors "github.com/dicedb/dice/internal/errors" | ||
"github.com/dicedb/dice/internal/shard" | ||
"github.com/dicedb/dice/config" | ||
dstore "github.com/dicedb/dice/internal/store" | ||
"github.com/dicedb/dice/internal/server/resp" | ||
"github.com/dicedb/dice/internal/worker" | ||
"github.com/dicedb/dice/internal/server/abstractserver" | ||
|
||
) | ||
|
||
func BenchmarkStart(b *testing.B){ | ||
numShards := runtime.NumCPU() | ||
runtime.GOMAXPROCS(numShards) | ||
|
||
var queryWatchChan chan dstore.QueryWatchEvent | ||
var cmdWatchChan chan dstore.CmdWatchEvent | ||
var serverErrCh = make(chan error, 2) | ||
|
||
shardManager := shard.NewShardManager(uint8(numShards), queryWatchChan, cmdWatchChan, serverErrCh) | ||
|
||
var ctx, cancel = context.WithCancel(context.Background()) | ||
|
||
var wg = sync.WaitGroup{} | ||
|
||
wg.Add(1) | ||
go func() { | ||
defer wg.Done() | ||
shardManager.Run(ctx) | ||
}() | ||
|
||
var serverWg sync.WaitGroup | ||
|
||
workerManager := worker.NewWorkerManager(config.DiceConfig.Performance.MaxClients, shardManager) | ||
respServer := resp.NewServer(shardManager, workerManager, cmdWatchChan, serverErrCh) | ||
|
||
serverWg.Add(1) | ||
go runServer(ctx, &serverWg, respServer, serverErrCh) | ||
|
||
time.Sleep(10 * time.Second) | ||
|
||
commands := []string{"PING", "SET foo bar", "GET foo", "INCR counter", "DEL foo"} | ||
concurrencyLevels := []int{10, 50, 100, 200} | ||
|
||
for _, concurrency := range concurrencyLevels { | ||
b.Run("Concurrency="+fmt.Sprintf("%d",concurrency), func(b *testing.B) { | ||
client := dice.NewClient(&dice.Options{ | ||
Addr: "localhost:7379", | ||
Password: "", | ||
DB: 0, | ||
}) | ||
defer client.Close() | ||
|
||
b.ResetTimer() | ||
for i := 0; i < b.N; i++ { | ||
var cmdWg sync.WaitGroup | ||
cmdWg.Add(concurrency) | ||
for j := 0; j < concurrency; j++ { | ||
go func() { | ||
defer cmdWg.Done() | ||
for _, cmd := range commands { | ||
switch cmd { | ||
case "PING": | ||
client.Ping(ctx) | ||
case "SET foo bar": | ||
client.Set(ctx, "foo", "bar", 0) | ||
case "GET foo": | ||
client.Get(ctx, "foo") | ||
case "INCR counter": | ||
client.Incr(ctx, "counter") | ||
case "DEL foo": | ||
client.Del(ctx, "foo") | ||
} | ||
} | ||
}() | ||
} | ||
cmdWg.Wait() | ||
} | ||
}) | ||
} | ||
cancel() | ||
wg.Wait() | ||
serverWg.Wait() | ||
} | ||
|
||
func runServer(ctx context.Context, wg *sync.WaitGroup, srv abstractserver.AbstractServer, errCh chan<- error) { | ||
defer wg.Done() | ||
if err := srv.Run(ctx); err != nil { | ||
switch { | ||
case errors.Is(err, context.Canceled): | ||
slog.Debug(fmt.Sprintf("%T was canceled", srv)) | ||
case errors.Is(err, diceerrors.ErrAborted): | ||
slog.Debug(fmt.Sprintf("%T received abort command", srv)) | ||
case errors.Is(err, http.ErrServerClosed): | ||
slog.Debug(fmt.Sprintf("%T received abort command", srv)) | ||
default: | ||
slog.Error(fmt.Sprintf("%T error", srv), slog.Any("error", err)) | ||
} | ||
errCh <- err | ||
} else { | ||
slog.Debug("bye.") | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.