Skip to content

Commit

Permalink
Updated metrics to map
Browse files Browse the repository at this point in the history
Signed-off-by: Plamen Petrov <plamb0brt@gmail.com>
  • Loading branch information
plamenmpetrov committed Jul 27, 2020
1 parent 62c5b35 commit b838e9b
Show file tree
Hide file tree
Showing 7 changed files with 169 additions and 543 deletions.
100 changes: 0 additions & 100 deletions metrics/loadSnapshotStat.go

This file was deleted.

160 changes: 160 additions & 0 deletions metrics/metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
// MIT License
//
// Copyright (c) 2020 Plamen Petrov
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

package metrics

import (
"bufio"
"fmt"
"os"
"sort"
"time"

"gonum.org/v1/gonum/stat"
)

const (
// FcResume Time it takes to resume a VM from containerd
FcResume = "FcResume"
// ReconnectFuncClient Time it takes to reconnect function client
ReconnectFuncClient = "ReconnectFuncClient"

// Full Used when there is no breakdown
Full = "Full"

// AddInstance Time to add instance - load snap or start vm
AddInstance = "AddInstance"
// GetResponse Time to get response from function
GetResponse = "GetResponse"
// RetireOld Time to offload/stop instance if threshold exceeded
RetireOld = "RetireOld"

// GetImage Time to pull docker image
GetImage = "GetImage"
// FcCreateVM Time to create VM
FcCreateVM = "FcCreateVM"
// NewContainer Time to create new container
NewContainer = "NewContainer"
// NewTask Time to create new task
NewTask = "NewTask"
// TaskWait Time to wait for task to be ready
TaskWait = "TaskWait"
// TaskStart Time to start task
TaskStart = "TaskStart"
)

// Metric A general metric
type Metric struct {
MetricMap map[string]float64
}

// NewMetric Create a new metric
func NewMetric() *Metric {
m := new(Metric)
m.MetricMap = make(map[string]float64)

return m
}

// Total Calculates the total time per stat
func (m *Metric) Total() float64 {
var sum float64
for _, v := range m.MetricMap {
sum += v
}

return sum
}

// PrintTotal Prints the total time
func (m *Metric) PrintTotal() {
fmt.Printf("Total: %.1f us\n", m.Total())
}

// PrintAll Prints a breakdown of the time
func (m *Metric) PrintAll() {
for k, v := range m.MetricMap {
fmt.Printf("%s:\t%.1f\n", k, v)
}
fmt.Printf("Total\t%.1f\n", m.Total())
}

// PrintMeanStd prints the mean and standard
//deviation of each component of Metric
func PrintMeanStd(resultsPath string, metricsList ...*Metric) {
var (
mean, std float64
f *os.File
err error
agg map[string][]float64 = make(map[string][]float64)
totals []float64 = make([]float64, 0, len(metricsList))
keys []string = make([]string, 0, 1)
)

if len(metricsList) == 0 {
return
}

if resultsPath == "" {
f = os.Stdout
} else {
f, err = os.Create(resultsPath)
if err != nil {
panic(err)
}
defer f.Close()
}

for k := range metricsList[0].MetricMap {
keys = append(keys, k)
agg[k] = make([]float64, 0, len(metricsList))
}
sort.Strings(keys)

for _, m := range metricsList {
totals = append(totals, m.Total())

for k, v := range m.MetricMap {
agg[k] = append(agg[k], v)
}
}

w := bufio.NewWriter(f)

fmt.Fprintf(w, "Stats \tMean(us) \tStdDev(us)\n")

for _, k := range keys {
v := agg[k]
mean, std = stat.MeanStdDev(v, nil)
fmt.Fprintf(w, "%s \t%12.1f \t%12.1f\n", k, mean, std)
w.Flush()
}

mean, std = stat.MeanStdDev(totals, nil)
fmt.Fprintf(w, "Total \t%12.1f \t%12.1f\n", mean, std)
w.Flush()
}

// ToUS Converts Duration to microseconds
func ToUS(dur time.Duration) float64 {
return float64(dur.Microseconds())
}
63 changes: 9 additions & 54 deletions metrics/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,60 +28,15 @@ import (
"github.com/stretchr/testify/require"
)

func TestStartVMStats(t *testing.T) {
s1 := NewStartVMStat()
s1.GetImage = 10
s1.TaskStart = 15
require.Equal(t, int64(25), s1.Total(), "Total is incorrect")
func TestMetrics(t *testing.T) {
s1 := NewMetric()
s1.MetricMap[GetImage] = 10.0
s1.MetricMap[TaskStart] = 15.0
require.Equal(t, float64(25.0), s1.Total(), "Total is incorrect")

s2 := NewStartVMStat()
s2.GetImage = 10
s2.TaskStart = 15
s2.NewContainer = 150
s2 := NewMetric()
s2.MetricMap[GetImage] = 40.0
s2.MetricMap[TaskStart] = 25.0

agg := NewStartVMStat()
agg.Aggregate(s1, s2)
require.Equal(t, int64(20), agg.GetImage, "GetImage value is incorrect")
require.Equal(t, int64(30), agg.TaskStart, "TaskStart value is incorrect")
require.Equal(t, int64(150), agg.NewContainer, "NewContainer value is incorrect")
require.Equal(t, int64(0), agg.TaskWait, "TaskWait value is incorrect")
require.Equal(t, int64(0), agg.NewTask, "NewTask value is incorrect")
require.Equal(t, int64(0), agg.FcCreateVM, "FcCreateVM value is incorrect")
require.Equal(t, int64(200), agg.Total(), "Aggregate Total is incorrect")

PrintStartVMStats("", s1, s2)
}

func TestLoadSnapshotStats(t *testing.T) {
s1 := NewLoadSnapshotStat()
s1.Full = 10
require.Equal(t, int64(10), s1.Total(), "Total is incorrect")

s2 := NewLoadSnapshotStat()
s2.Full = 10

agg := NewLoadSnapshotStat()
agg.Aggregate(s1, s2)
require.Equal(t, int64(20), agg.Full, "Full Total is incorrect")
require.Equal(t, int64(20), agg.Total(), "Aggregate Total is incorrect")

PrintLoadSnapshotStats("", s1, s2)
}

func TestServeStats(t *testing.T) {
s1 := NewServeStat()
s1.GetResponse = 25
s1.RetireOld = 10
require.Equal(t, int64(35), s1.Total(), "Total is incorrect")

PrintServeStats("", s1)
}

func TestResumeVMStats(t *testing.T) {
s1 := NewResumeVMStat()
s1.FcResume = 25
s1.ReconnectFuncClient = 10
require.Equal(t, int64(35), s1.Total(), "Total is incorrect")

PrintResumeVMStats("", s1)
PrintMeanStd("", s1, s2)
}
Loading

0 comments on commit b838e9b

Please sign in to comment.