feat: call the HTTP API to get master state information

This commit is contained in:
徐聪
2022-07-15 15:33:53 +08:00
parent 17617ee13e
commit f2799aef53
4 changed files with 51 additions and 11 deletions

View File

@@ -491,11 +491,20 @@ func (b *Boomer) Stop() error {
return b.masterRunner.stop()
}
// GetWorkersInfo gets workers
// GetWorkersInfo gets workers information
func (b *Boomer) GetWorkersInfo() []WorkerNode {
return b.masterRunner.server.getAllWorkers()
}
// GetMasterInfo gets master information
func (b *Boomer) GetMasterInfo() map[string]interface{} {
masterInfo := make(map[string]interface{})
masterInfo["state"] = getStateName(b.masterRunner.getState())
masterInfo["workers"] = b.masterRunner.server.getClientsLength()
masterInfo["target_users"] = b.masterRunner.getSpawnCount()
return masterInfo
}
func (b *Boomer) GetCloseChan() chan bool {
switch b.mode {
case DistributedWorkerMode:

View File

@@ -1064,11 +1064,26 @@ func (r *masterRunner) clientListener() {
workerInfo.setState(int32(builtin.BytesToInt64(msg.Data["state"])))
}
workerInfo.updateHeartbeat(3)
workerInfo.updateCPUUsage(builtin.ByteToFloat64(msg.Data["current_cpu_usage"]))
workerInfo.updateWorkerCPUUsage(builtin.ByteToFloat64(msg.Data["current_pid_cpu_usage"]))
workerInfo.updateMemoryUsage(builtin.ByteToFloat64(msg.Data["current_memory_usage"]))
workerInfo.updateWorkerMemoryUsage(builtin.ByteToFloat64(msg.Data["current_pid_memory_usage"]))
workerInfo.updateUserCount(builtin.BytesToInt64(msg.Data["current_users"]))
currentCPUUsage, ok := msg.Data["current_cpu_usage"]
if ok {
workerInfo.updateCPUUsage(builtin.ByteToFloat64(currentCPUUsage))
}
currentPidCpuUsage, ok := msg.Data["current_pid_cpu_usage"]
if ok {
workerInfo.updateWorkerCPUUsage(builtin.ByteToFloat64(currentPidCpuUsage))
}
currentMemoryUsage, ok := msg.Data["current_memory_usage"]
if ok {
workerInfo.updateMemoryUsage(builtin.ByteToFloat64(currentMemoryUsage))
}
currentPidMemoryUsage, ok := msg.Data["current_pid_memory_usage"]
if ok {
workerInfo.updateWorkerMemoryUsage(builtin.ByteToFloat64(currentPidMemoryUsage))
}
currentUsers, ok := msg.Data["current_users"]
if ok {
workerInfo.updateUserCount(builtin.BytesToInt64(currentUsers))
}
case typeSpawning:
workerInfo.setState(StateSpawning)
case typeSpawningComplete:

View File

@@ -7,6 +7,7 @@ import (
"time"
"github.com/httprunner/httprunner/v4/hrp/internal/boomer/grpc/messager"
"github.com/httprunner/httprunner/v4/hrp/internal/builtin"
"github.com/stretchr/testify/assert"
)
@@ -529,7 +530,7 @@ func TestHeartbeatWorker(t *testing.T) {
runner.server.recvChannel() <- &genericMessage{
Type: typeHeartbeat,
NodeID: "testID2",
Data: map[string]int64{"state": 3},
Data: map[string][]byte{"state": builtin.Int64ToBytes(3)},
}
worker2, ok := runner.server.getClients().Load("testID2")
if !ok {