Skip to content

Commit

Permalink
lb: code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
sni committed Nov 27, 2024
1 parent 4cd9217 commit 159ccf0
Showing 1 changed file with 38 additions and 28 deletions.
66 changes: 38 additions & 28 deletions cmd/lb/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ import (
"flag"
"fmt"
"os"
"pkg/lmd"
"strings"
"time"

"pkg/lmd"
)

// Build contains the current git commit id
Expand Down Expand Up @@ -103,9 +104,40 @@ func main() {
cmd.fillQueries()

for i := range cmd.flags.flagParallel {
go cmd.run(i)
go cmd.runWorker(i)
}

final := cmd.collectResults()
cmd.printResults(final)
}

func (cmd *Cmd) runWorker(i int64) {
ctx := context.Background()
name := fmt.Sprintf("w%02d", i)
peer := lmd.NewPeer(cmd.daemon, &lmd.Connection{Source: cmd.flags.flagConn.Value(), Name: name, ID: name})
for {
select {
case <-cmd.shutdownChannel:
cmd.shutdownChannel <- true

return
default:
query := <-cmd.queryChannel
_, _, err := peer.Query(ctx, query.req)
res := &result{
name: query.name,
total: 1,
}
if err != nil {
res.errors++
}
cmd.resultChannel <- res
cmd.queryChannel <- query
}
}
}

func (cmd *Cmd) collectResults() map[string]*result {
final := map[string]*result{}
end := time.NewTimer(cmd.flags.flagDuration)
keepRuning := true
Expand All @@ -128,6 +160,10 @@ func main() {
}
}

return final
}

func (cmd *Cmd) printResults(final map[string]*result) {
total := &result{}
totalRate := float64(0)
for _, t := range testQueries {
Expand All @@ -142,32 +178,6 @@ func main() {
fmt.Fprintf(os.Stdout, "%15s: total: %10d | errors: %3d | rate: %9.2f req/s\n", "total", total.total, total.errors, totalRate)
}

func (cmd *Cmd) run(i int64) {
ctx := context.Background()
name := fmt.Sprintf("w%02d", i)
peer := lmd.NewPeer(cmd.daemon, &lmd.Connection{Source: cmd.flags.flagConn.Value(), Name: name, ID: name})
for {
select {
case <-cmd.shutdownChannel:
cmd.shutdownChannel <- true

return
default:
query := <-cmd.queryChannel
_, _, err := peer.Query(ctx, query.req)
res := &result{
name: query.name,
total: 1,
}
if err != nil {
res.errors++
}
cmd.resultChannel <- res
cmd.queryChannel <- query
}
}
}

func (cmd *Cmd) fillQueries() {
ctx := context.TODO()
qIdx := 0
Expand Down

0 comments on commit 159ccf0

Please sign in to comment.