From 41728c6a0daa1ba2caeabb96359a52c2fbad3342 Mon Sep 17 00:00:00 2001 From: Sven Nierlein Date: Tue, 26 Nov 2024 00:21:36 +0100 Subject: [PATCH] add benchmark/test tool --- .gitignore | 1 + Makefile | 4 +++ buildtools/lb.go | 86 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 91 insertions(+) create mode 100644 buildtools/lb.go diff --git a/.gitignore b/.gitignore index cc639e0..df60e3a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ /lmd +/lb lmd.ini lmd.log *.swp diff --git a/Makefile b/Makefile index 9d5a969..3aa7284 100644 --- a/Makefile +++ b/Makefile @@ -229,6 +229,10 @@ zip: clean ls -la lmd-$$FILE.gz; \ echo "lmd-$$FILE.gz created"; +lb: vendor buildtools/lb.go + $(GO) build $(BUILD_FLAGS) -o ./lb ./buildtools/lb.go + + # just skip unknown make targets .DEFAULT: @if [[ "$(MAKECMDGOALS)" =~ ^testf ]]; then \ diff --git a/buildtools/lb.go b/buildtools/lb.go new file mode 100644 index 0000000..3bae816 --- /dev/null +++ b/buildtools/lb.go @@ -0,0 +1,86 @@ +package main + +import ( + "flag" + "fmt" + "net" + "time" +) + +type Cmd struct { + shutdownChannel chan bool + flags struct { + flagParallel int64 + flagDuration time.Duration + flagPort int64 + flagPath string + } +} + +func main() { + cmd := &Cmd{ + shutdownChannel: make(chan bool, 5), + } + flag.Int64Var(&cmd.flags.flagParallel, "parallel", 10, "set number of parallel requests") + flag.DurationVar(&cmd.flags.flagDuration, "duration", 10*time.Second, "set run duration") + flag.Int64Var(&cmd.flags.flagPort, "port", 0, "set port to connect to") + flag.StringVar(&cmd.flags.flagPath, "path", "", "set unix socket path to connect to") + flag.Parse() + + if cmd.flags.flagPort == 0 && cmd.flags.flagPath == "" { + fmt.Printf("ERROR: must specify either port or path\n") + return + } + + for i := 0; i < int(cmd.flags.flagParallel); i++ { + go cmd.run() + } + + time.Sleep(cmd.flags.flagDuration) + cmd.shutdownChannel <- true +} + +func (cmd *Cmd) run() { + conn := cmd.getConnection() + for { + select { + case <-cmd.shutdownChannel: + cmd.shutdownChannel <- true + if conn != nil { + conn.Close() + conn = nil + } + return + default: + if conn == nil { + conn = cmd.getConnection() + } + if conn == nil { + return + } + query := cmd.randomQuery() + _, err := fmt.Fprintf(conn, "%s\n", query) + if err != nil { + if conn != nil { + conn.Close() + conn = nil + } + fmt.Printf("ERROR: %s\n", err.Error()) + conn = cmd.getConnection() + } + } + } +} + +func (cmd *Cmd) getConnection() net.Conn { + conn, err := net.DialTimeout("unix", cmd.flags.flagPath, 60*time.Second) + if err != nil { + fmt.Printf("ERROR: %s\n", err.Error()) + return nil + } + return conn +} + +func (cmd *Cmd) randomQuery() string { + return "GET status\nKeepAlive: on\n" +}