e2e: add a function to reset the stats in resource monitor

The function can be called in a long-running test to clear exsiting
stats (free the memory), and start over.
This commit is contained in:
Yu-Ju Hong 2015-09-22 15:08:53 -07:00
parent 795d6827c1
commit ab85b9a7d6

View File

@ -25,6 +25,7 @@ import (
"sort"
"strconv"
"strings"
"sync"
"text/tabwriter"
"time"
@ -346,6 +347,7 @@ func computeContainerResourceUsage(name string, oldStats, newStats *cadvisor.Con
// list of containers, computes and cache resource usage up to
// maxEntriesPerContainer for each container.
type resourceCollector struct {
lock sync.RWMutex
node string
containers []string
client *client.Client
@ -390,6 +392,8 @@ func (r *resourceCollector) collectStats(oldStats map[string]*cadvisor.Container
Logf("Error getting container info on %q, err: %v", r.node, err)
return
}
r.lock.Lock()
defer r.lock.Unlock()
for _, name := range r.containers {
info, ok := infos[name]
if !ok || len(info.Stats) < 1 {
@ -405,6 +409,8 @@ func (r *resourceCollector) collectStats(oldStats map[string]*cadvisor.Container
// LogLatest logs the latest resource usage of each container.
func (r *resourceCollector) LogLatest() {
r.lock.RLock()
defer r.lock.RUnlock()
stats := make(map[string]*containerResourceUsage)
for _, name := range r.containers {
s := r.buffers[name][len(r.buffers)-1]
@ -417,6 +423,15 @@ func (r *resourceCollector) LogLatest() {
Logf("\n%s", formatResourceUsageStats(r.node, stats))
}
// Reset frees the stats and start over.
func (r *resourceCollector) Reset() {
r.lock.Lock()
defer r.lock.Unlock()
for _, name := range r.containers {
r.buffers[name] = []*containerResourceUsage{}
}
}
type resourceUsageByCPU []*containerResourceUsage
func (r resourceUsageByCPU) Len() int { return len(r) }
@ -428,6 +443,8 @@ var percentiles = [...]float64{0.05, 0.50, 0.90, 0.95}
// GetBasicCPUStats returns the 5-th, 50-th, and 95-th, percentiles the cpu
// usage in cores for containerName. This method examines all data currently in the buffer.
func (r *resourceCollector) GetBasicCPUStats(containerName string) map[float64]float64 {
r.lock.RLock()
defer r.lock.RUnlock()
result := make(map[float64]float64, len(percentiles))
usages := r.buffers[containerName]
sort.Sort(resourceUsageByCPU(usages))
@ -476,6 +493,12 @@ func (r *resourceMonitor) Stop() {
}
}
func (r *resourceMonitor) Reset() {
for _, collector := range r.collectors {
collector.Reset()
}
}
func (r *resourceMonitor) LogLatest() {
for _, collector := range r.collectors {
collector.LogLatest()