refac benchmarks

mine
derailed 2019-06-20 15:20:19 -06:00
parent da43251c67
commit cd7258fa23
3 changed files with 69 additions and 64 deletions

View File

@ -5,7 +5,6 @@ import (
"fmt" "fmt"
"strconv" "strconv"
"strings" "strings"
"sync"
"github.com/derailed/k9s/internal/k8s" "github.com/derailed/k9s/internal/k8s"
v1 "k8s.io/api/core/v1" v1 "k8s.io/api/core/v1"
@ -17,31 +16,27 @@ type (
Container struct { Container struct {
*Base *Base
pod *v1.Pod pod *v1.Pod
isInit bool instance v1.Container
instance v1.Container metrics *mv1beta1.PodMetrics
MetricsServer MetricsServer
metrics *mv1beta1.PodMetrics
mx sync.RWMutex
} }
) )
// NewContainerList returns a collection of container. // NewContainerList returns a collection of container.
func NewContainerList(c Connection, mx MetricsServer, pod *v1.Pod) List { func NewContainerList(c Connection, pod *v1.Pod) List {
return NewList( return NewList(
"", "",
"co", "co",
NewContainer(c, mx, pod), NewContainer(c, pod),
0, 0,
) )
} }
// NewContainer returns a new set of containers. // NewContainer returns a new set of containers.
func NewContainer(c Connection, mx MetricsServer, pod *v1.Pod) *Container { func NewContainer(c Connection, pod *v1.Pod) *Container {
co := Container{ co := Container{
Base: &Base{Connection: c, Resource: k8s.NewPod(c)}, Base: &Base{Connection: c, Resource: k8s.NewPod(c)},
pod: pod, pod: pod,
MetricsServer: mx,
} }
co.Factory = &co co.Factory = &co
@ -50,7 +45,7 @@ func NewContainer(c Connection, mx MetricsServer, pod *v1.Pod) *Container {
// New builds a new Container instance from a k8s resource. // New builds a new Container instance from a k8s resource.
func (r *Container) New(i interface{}) Columnar { func (r *Container) New(i interface{}) Columnar {
co := NewContainer(r.Connection, r.MetricsServer, r.pod) co := NewContainer(r.Connection, r.pod)
co.instance = i.(v1.Container) co.instance = i.(v1.Container)
co.path = r.namespacedName(r.pod.ObjectMeta) + ":" + co.instance.Name co.path = r.namespacedName(r.pod.ObjectMeta) + ":" + co.instance.Name
@ -90,7 +85,6 @@ func (r *Container) List(ns string) (Columnars, error) {
cc := make(Columnars, 0, len(icos)+len(cos)) cc := make(Columnars, 0, len(icos)+len(cos))
for _, co := range icos { for _, co := range icos {
ci := r.New(co) ci := r.New(co)
ci.(*Container).isInit = true
cc = append(cc, ci) cc = append(cc, ci)
} }
for _, co := range cos { for _, co := range cos {
@ -134,47 +128,10 @@ func (r *Container) Fields(ns string) Row {
ff := make(Row, 0, len(r.Header(ns))) ff := make(Row, 0, len(r.Header(ns)))
i := r.instance i := r.instance
scpu, smem, pcpu, pmem := NAValue, NAValue, NAValue, NAValue scpu, smem, pcpu, pmem := gatherMetrics(i, r.metrics)
if r.metrics != nil {
var (
cpu int64
mem float64
)
for _, co := range r.metrics.Containers {
if co.Name == i.Name {
cpu = co.Usage.Cpu().MilliValue()
mem = k8s.ToMB(co.Usage.Memory().Value())
break
}
}
scpu, smem = ToMillicore(cpu), ToMi(mem)
rcpu, rmem := containerResources(i)
if rcpu != nil {
pcpu = AsPerc(toPerc(float64(cpu), float64(rcpu.MilliValue())))
}
if rmem != nil {
pmem = AsPerc(toPerc(mem, k8s.ToMB(rmem.Value())))
}
}
var cs *v1.ContainerStatus
for _, c := range r.pod.Status.ContainerStatuses {
if c.Name == i.Name {
cs = &c
break
}
}
if cs == nil {
for _, c := range r.pod.Status.InitContainerStatuses {
if c.Name == i.Name {
cs = &c
break
}
}
}
ready, state, restarts := "false", MissingValue, "0" ready, state, restarts := "false", MissingValue, "0"
cs := getContainerStatus(i.Name, r.pod.Status)
if cs != nil { if cs != nil {
ready, state, restarts = boolToStr(cs.Ready), toState(cs.State), strconv.Itoa(int(cs.RestartCount)) ready, state, restarts = boolToStr(cs.Ready), toState(cs.State), strconv.Itoa(int(cs.RestartCount))
} }
@ -198,6 +155,53 @@ func (r *Container) Fields(ns string) Row {
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// Helpers... // Helpers...
func gatherMetrics(co v1.Container, mx *mv1beta1.PodMetrics) (scpu string, smem string, pcpu string, pmem string) {
scpu, smem, pcpu, pmem = NAValue, NAValue, NAValue, NAValue
if mx == nil {
return
}
var (
cpu int64
mem float64
)
for _, c := range mx.Containers {
if c.Name != co.Name {
continue
}
cpu = c.Usage.Cpu().MilliValue()
mem = k8s.ToMB(c.Usage.Memory().Value())
break
}
scpu, smem = ToMillicore(cpu), ToMi(mem)
rcpu, rmem := containerResources(co)
if rcpu != nil {
pcpu = AsPerc(toPerc(float64(cpu), float64(rcpu.MilliValue())))
}
if rmem != nil {
pmem = AsPerc(toPerc(mem, k8s.ToMB(rmem.Value())))
}
return
}
func getContainerStatus(co string, status v1.PodStatus) *v1.ContainerStatus {
for _, c := range status.ContainerStatuses {
if c.Name == co {
return &c
}
}
for _, c := range status.InitContainerStatuses {
if c.Name == co {
return &c
}
}
return nil
}
func toStrPorts(pp []v1.ContainerPort) string { func toStrPorts(pp []v1.ContainerPort) string {
ports := make([]string, len(pp)) ports := make([]string, len(pp))
for i, p := range pp { for i, p := range pp {

View File

@ -211,13 +211,8 @@ func (v *benchView) hydrate() resource.TableData {
log.Error().Err(err).Msgf("Unable to load bench file %s", f.Name()) log.Error().Err(err).Msgf("Unable to load bench file %s", f.Name())
continue continue
} }
tokens := strings.Split(f.Name(), "_") fields := make(resource.Row, len(benchHeader))
fields := resource.Row{ initRow(fields, f)
0: tokens[0],
1: tokens[1],
7: f.Name(),
8: time.Since(f.ModTime()).String(),
}
augmentRow(fields, bench) augmentRow(fields, bench)
data.Rows[f.Name()] = &resource.RowEvent{ data.Rows[f.Name()] = &resource.RowEvent{
Action: resource.New, Action: resource.New,
@ -229,6 +224,14 @@ func (v *benchView) hydrate() resource.TableData {
return data return data
} }
func initRow(row resource.Row, f os.FileInfo) {
tokens := strings.Split(f.Name(), "_")
row[0] = tokens[0]
row[1] = tokens[1]
row[7] = f.Name()
row[8] = time.Since(f.ModTime()).String()
}
func (v *benchView) getTV() *tableView { func (v *benchView) getTV() *tableView {
if vu, ok := v.GetPrimitive("table").(*tableView); ok { if vu, ok := v.GetPrimitive("table").(*tableView); ok {
return vu return vu

View File

@ -6,7 +6,6 @@ import (
"fmt" "fmt"
"strings" "strings"
"github.com/derailed/k9s/internal/k8s"
"github.com/derailed/k9s/internal/resource" "github.com/derailed/k9s/internal/resource"
"github.com/derailed/k9s/internal/watch" "github.com/derailed/k9s/internal/watch"
"github.com/gdamore/tcell" "github.com/gdamore/tcell"
@ -77,8 +76,7 @@ func (v *podView) listContainers(app *appView, _, res, sel string) {
} }
pod := po.(*v1.Pod) pod := po.(*v1.Pod)
mx := k8s.NewMetricsServer(app.conn()) list := resource.NewContainerList(app.conn(), pod)
list := resource.NewContainerList(app.conn(), mx, pod)
title := skinTitle(fmt.Sprintf(containerFmt, "Containers", sel), app.styles.Frame()) title := skinTitle(fmt.Sprintf(containerFmt, "Containers", sel), app.styles.Frame())
// Stop my updater // Stop my updater