Skip to content

Commit

Permalink
fix: add boomer mode(standalone/master/worker)
Browse files Browse the repository at this point in the history
  • Loading branch information
xucong053 committed May 12, 2022
1 parent 29b1086 commit 91d511f
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 3 deletions.
2 changes: 1 addition & 1 deletion hrp/cmd/boom.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ var boomCmd = &cobra.Command{
hrpBoomer.AddOutput(boomer.NewConsoleOutput())
}
if prometheusPushgatewayURL != "" {
hrpBoomer.AddOutput(boomer.NewPrometheusPusherOutput(prometheusPushgatewayURL, "hrp"))
hrpBoomer.AddOutput(boomer.NewPrometheusPusherOutput(prometheusPushgatewayURL, "hrp", hrpBoomer.GetMode()))
}
hrpBoomer.SetDisableKeepAlive(disableKeepalive)
hrpBoomer.SetDisableCompression(disableCompression)
Expand Down
45 changes: 45 additions & 0 deletions hrp/internal/boomer/boomer.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,26 @@ import (
"syscall"
"time"

"github.com/pkg/errors"
"github.com/rs/zerolog/log"
)

// Mode is the running mode of boomer, both standalone and distributed are supported.
type Mode int

const (
// DistributedMasterMode requires being connected by each worker.
DistributedMasterMode Mode = iota
// DistributedWorkerMode requires connecting to a master.
DistributedWorkerMode
// StandaloneMode will run without a master.
StandaloneMode
)

// A Boomer is used to run tasks.
type Boomer struct {
mode Mode

localRunner *localRunner

cpuProfile string
Expand All @@ -24,9 +39,39 @@ type Boomer struct {
disableCompression bool
}

// SetMode only accepts boomer.DistributedMasterMode、boomer.DistributedWorkerMode and boomer.StandaloneMode.
func (b *Boomer) SetMode(mode Mode) {
switch mode {
case DistributedMasterMode:
b.mode = DistributedMasterMode
case DistributedWorkerMode:
b.mode = DistributedWorkerMode
case StandaloneMode:
b.mode = StandaloneMode
default:
log.Error().Err(errors.New("Invalid mode, ignored!"))
}
}

// GetMode returns boomer operating mode
func (b *Boomer) GetMode() string {
switch b.mode {
case DistributedMasterMode:
return "master"
case DistributedWorkerMode:
return "worker"
case StandaloneMode:
return "standalone"
default:
log.Error().Err(errors.New("Invalid mode, ignored!"))
return ""
}
}

// NewStandaloneBoomer returns a new Boomer, which can run without master.
func NewStandaloneBoomer(spawnCount int, spawnRate float64) *Boomer {
return &Boomer{
mode: StandaloneMode,
localRunner: newLocalRunner(spawnCount, spawnRate),
}
}
Expand Down
6 changes: 4 additions & 2 deletions hrp/internal/boomer/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -471,10 +471,12 @@ var (
)

// NewPrometheusPusherOutput returns a PrometheusPusherOutput.
func NewPrometheusPusherOutput(gatewayURL, jobName string) *PrometheusPusherOutput {
func NewPrometheusPusherOutput(gatewayURL, jobName string, mode string) *PrometheusPusherOutput {
nodeUUID, _ := uuid.NewUUID()
return &PrometheusPusherOutput{
pusher: push.New(gatewayURL, jobName).Grouping("instance", nodeUUID.String()),
pusher: push.New(gatewayURL, jobName).
Grouping("instance", nodeUUID.String()).
Grouping("mode", mode),
}
}

Expand Down

0 comments on commit 91d511f

Please sign in to comment.