etcdctl/check: create new check command for memory usage
Create a new command similar to check perf that can check the memory consumption for putting different workloads on a given endpoint. If no endpoint is provided, localhost will be used. Return user with a message that whether there are enough memory for a given workload with pass or fail. Fixed #9121
This commit is contained in:
@ -18,7 +18,11 @@ import (
|
||||
"context"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
pb "github.com/coreos/etcd/internal/mvcc/mvccpb"
|
||||
|
||||
@ -75,3 +79,42 @@ func commandCtx(cmd *cobra.Command) (context.Context, context.CancelFunc) {
|
||||
}
|
||||
return context.WithTimeout(context.Background(), timeOut)
|
||||
}
|
||||
|
||||
// get the process_resident_memory_bytes from <server:2379>/metrics
|
||||
func endpointMemoryMetrics(host string) float64 {
|
||||
residentMemoryKey := "process_resident_memory_bytes"
|
||||
var residentMemoryValue string
|
||||
if !strings.HasPrefix(host, `http://`) {
|
||||
host = "http://" + host
|
||||
}
|
||||
url := host + "/metrics"
|
||||
resp, err := http.Get(url)
|
||||
if err != nil {
|
||||
fmt.Println(fmt.Sprintf("fetch error: %v", err))
|
||||
return 0.0
|
||||
}
|
||||
byts, readerr := ioutil.ReadAll(resp.Body)
|
||||
resp.Body.Close()
|
||||
if readerr != nil {
|
||||
fmt.Println(fmt.Sprintf("fetch error: reading %s: %v", url, readerr))
|
||||
return 0.0
|
||||
}
|
||||
|
||||
for _, line := range strings.Split(string(byts), "\n") {
|
||||
if strings.HasPrefix(line, residentMemoryKey) {
|
||||
residentMemoryValue = strings.TrimSpace(strings.TrimPrefix(line, residentMemoryKey))
|
||||
break
|
||||
}
|
||||
}
|
||||
if residentMemoryValue == "" {
|
||||
fmt.Println(fmt.Sprintf("could not find: %v", residentMemoryKey))
|
||||
return 0.0
|
||||
}
|
||||
residentMemoryBytes, parseErr := strconv.ParseFloat(residentMemoryValue, 64)
|
||||
if parseErr != nil {
|
||||
fmt.Println(fmt.Sprintf("parse error: %v", parseErr))
|
||||
return 0.0
|
||||
}
|
||||
|
||||
return residentMemoryBytes
|
||||
}
|
||||
|
Reference in New Issue
Block a user