Commit ac60b37a authored by Kevin Di Lallo's avatar Kevin Di Lallo
Browse files

improved metric store unit tests

parent 473c1b3f
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -48,7 +48,7 @@ func (ms *MetricStore) GetLastEventMetric(eventType string) (event string, err e
		"type": eventType,
	}
	fields := []string{"event"}
	valuesArray, err := ms.GetMetric(metricEvent, tags, fields, "", "", 1)
	valuesArray, err := ms.GetMetric(metricEvent, tags, fields, "", 1)
	if err != nil {
		log.Error("Failed to retrieve metrics with error: ", err.Error())
		return event, err
+77 −0
Original line number Diff line number Diff line
/*
 * Copyright (c) 2019  InterDigital Communications, Inc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package metricstore

import (
	"fmt"
	"testing"

	log "github.com/InterDigitalInc/AdvantEDGE/go-packages/meep-logger"
)

const eventsStoreName string = "eventsStore"
const eventsStoreAddr string = "http://localhost:30986"

func TestEventsMetricsGetSet(t *testing.T) {
	fmt.Println("--- ", t.Name())
	log.MeepTextLogInit(t.Name())

	fmt.Println("Create valid Metric Store")
	ms, err := NewMetricStore(eventsStoreName, eventsStoreAddr)
	if err != nil {
		t.Errorf("Unable to create Metric Store")
	}

	fmt.Println("Flush store metrics")
	ms.Flush()

	fmt.Println("Set event metric")
	err = ms.SetEventMetric("MOBILITY", "event1")
	if err != nil {
		t.Errorf("Unable to set event metric")
	}
	err = ms.SetEventMetric("NETWORK-CHARACTERISTIC-UPDATE", "event2")
	if err != nil {
		t.Errorf("Unable to set event metric")
	}
	err = ms.SetEventMetric("POAS-IN-RANGE", "event3")
	if err != nil {
		t.Errorf("Unable to set event metric")
	}

	fmt.Println("Get event metrics")
	event, err := ms.GetLastEventMetric("MOBILITY")
	if err != nil {
		t.Errorf("Event metric should exist")
	} else if event != "event1" {
		t.Errorf("Invalid metric values")
	}
	event, err = ms.GetLastEventMetric("NETWORK-CHARACTERISTIC-UPDATE")
	if err != nil {
		t.Errorf("Event metric should exist")
	} else if event != "event2" {
		t.Errorf("Invalid metric values")
	}
	event, err = ms.GetLastEventMetric("POAS-IN-RANGE")
	if err != nil {
		t.Errorf("Event metric should exist")
	} else if event != "event3" {
		t.Errorf("Invalid metric values")
	}

	// t.Errorf("DONE")
}
+9 −16
Original line number Diff line number Diff line
@@ -73,7 +73,7 @@ func (ms *MetricStore) connectDB(addr string) error {
	}
	log.Debug("InfluxDB Connector connecting to ", ms.addr)

	client, err := influxclient.NewHTTPClient(influxclient.HTTPConfig{Addr: ms.addr})
	client, err := influxclient.NewHTTPClient(influxclient.HTTPConfig{Addr: ms.addr, InsecureSkipVerify: true})
	if err != nil {
		log.Error("InfluxDB Connector unable to connect ", ms.addr)
		return err
@@ -131,6 +131,8 @@ func (ms *MetricStore) SetMetric(metric string, tags map[string]string, fields m
		return err
	}

	// start = time.Now()

	// Create a new point batch
	bp, _ := influxclient.NewBatchPoints(influxclient.BatchPointsConfig{
		Database:  ms.name,
@@ -145,8 +147,6 @@ func (ms *MetricStore) SetMetric(metric string, tags map[string]string, fields m
	}
	bp.AddPoint(pt)

	// logTimeLapse("Created point: ")

	// Write the batch
	err = (*ms.client).Write(bp)
	if err != nil {
@@ -154,13 +154,13 @@ func (ms *MetricStore) SetMetric(metric string, tags map[string]string, fields m
		return err
	}

	// logTimeLapse("Write complete: ")
	// logTimeLapse("SetMetric duration: ")

	return nil
}

// GetMetric - Generic metric getter
func (ms *MetricStore) GetMetric(metric string, tags map[string]string, fields []string, startTime string, stopTime string, count int) (values []map[string]interface{}, err error) {
func (ms *MetricStore) GetMetric(metric string, tags map[string]string, fields []string, duration string, count int) (values []map[string]interface{}, err error) {
	// Make sure we have set a store
	if ms.name == "" {
		err := errors.New("Store name not specified")
@@ -191,18 +191,11 @@ func (ms *MetricStore) GetMetric(metric string, tags map[string]string, fields [
			tagStr += " AND " + k + "='" + v + "'"
		}
	}
	if startTime != "" {
		if tagStr == "" {
			tagStr = " WHERE time > " + startTime
		} else {
			tagStr += " AND time > " + startTime
		}
	}
	if stopTime != "" {
	if duration != "" {
		if tagStr == "" {
			tagStr = " WHERE time < " + stopTime
			tagStr = " WHERE time > now() - " + duration
		} else {
			tagStr += " AND time < " + stopTime
			tagStr += " AND time > now() - " + duration
		}
	}

@@ -245,6 +238,6 @@ func (ms *MetricStore) GetMetric(metric string, tags map[string]string, fields [

// func logTimeLapse(logStr string) {
// 	stop := time.Now()
// 	fmt.Println(logStr + strconv.FormatFloat(stop.Sub(start).Seconds()*1000, 'f', -1, 64))
// 	log.Debug(logStr, strconv.FormatFloat(stop.Sub(start).Seconds()*1000, 'f', 3, 64), " ms")
// 	start = stop
// }
+77 −208
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@
package metricstore

import (
	"encoding/json"
	"fmt"
	"testing"

@@ -27,11 +28,18 @@ const metricStore1Name string = "metricStore1"
const metricStore2Name string = "metricStore2"
const metricStoreAddr string = "http://localhost:30986"

func TestNewMetricStore(t *testing.T) {
const metric = "metric1"
const tag1 = "tag1"
const tag2 = "tag2"
const field1 = "field1"
const field2 = "field2"
const field3 = "field3"
const field4 = "field4"

func TestMetricStoreNew(t *testing.T) {
	fmt.Println("--- ", t.Name())
	log.MeepTextLogInit(t.Name())

	// Keep this one first...
	fmt.Println("Invalid Metric Store address")
	ms, err := NewMetricStore("", "ExpectedFailure-InvalidStoreAddr")
	if err == nil {
@@ -70,246 +78,107 @@ func TestNewMetricStore(t *testing.T) {
	// t.Errorf("DONE")
}

func TestGetSetMetric(t *testing.T) {
func TestMetricStoreGetSet(t *testing.T) {
	fmt.Println("--- ", t.Name())
	log.MeepTextLogInit(t.Name())

	// start = time.Now()

	fmt.Println("Create valid Metric Store")
	ms, err := NewMetricStore(metricStore1Name, metricStoreAddr)
	if err != nil {
		t.Errorf("Unable to create Metric Store")
	}

	// logTimeLapse("Created Metric store: ")

	fmt.Println("Flush store metrics")
	ms.Flush()

	// logTimeLapse("Flush: ")

	fmt.Println("Get empty metric")
	lat, mean, err := ms.GetLastLatencyMetric("node1", "node2")
	if err == nil || lat != 0 || mean != 0 {
	getTags := map[string]string{tag1: "tag1", tag2: "tag2"}
	getFields := []string{field1, field2, field3, field4}
	_, err = ms.GetMetric(metric, getTags, getFields, "", 1)
	if err == nil {
		t.Errorf("Net metric should not exist")
	}

	// logTimeLapse("Get empty metric: ")

	fmt.Println("Set network metrics")
	err = ms.SetLatencyMetric("node1", "node2", 0, 1)
	if err != nil {
		t.Errorf("Unable to set net metric")
	}
	err = ms.SetTrafficMetric("node1", "node2", 0.1, 1.1)
	if err != nil {
		t.Errorf("Unable to set net metric")
	}
	err = ms.SetLatencyMetric("node1", "node3", 1, 2)
	if err != nil {
		t.Errorf("Unable to set net metric")
	}
	err = ms.SetTrafficMetric("node1", "node3", 1.1, 2.1)
	if err != nil {
		t.Errorf("Unable to set net metric")
	}
	err = ms.SetLatencyMetric("node2", "node1", 2, 3)
	if err != nil {
		t.Errorf("Unable to set net metric")
	}
	err = ms.SetTrafficMetric("node2", "node1", 2.1, 3.1)
	if err != nil {
		t.Errorf("Unable to set net metric")
	}
	err = ms.SetLatencyMetric("node2", "node3", 3, 4)
	if err != nil {
		t.Errorf("Unable to set net metric")
	}
	err = ms.SetTrafficMetric("node2", "node3", 3.1, 4.1)
	if err != nil {
		t.Errorf("Unable to set net metric")
	}
	err = ms.SetLatencyMetric("node3", "node1", 4, 5)
	fmt.Println("Set metrics")
	setTags := map[string]string{tag1: "tag1", tag2: "tag2"}
	setFields := map[string]interface{}{field1: true, field2: "val1", field3: 0, field4: 0.0}
	err = ms.SetMetric(metric, setTags, setFields)
	if err != nil {
		t.Errorf("Unable to set net metric")
		t.Errorf("Failed to set metric")
	}
	err = ms.SetTrafficMetric("node3", "node1", 4.5, 5.5)
	setTags = map[string]string{tag1: "tag1", tag2: "tag2"}
	setFields = map[string]interface{}{field1: false, field2: "val2", field3: 1, field4: 1.1}
	err = ms.SetMetric(metric, setTags, setFields)
	if err != nil {
		t.Errorf("Unable to set net metric")
		t.Errorf("Failed to set metric")
	}
	err = ms.SetLatencyMetric("node3", "node2", 5, 6)
	if err != nil {
		t.Errorf("Unable to set net metric")
	}
	err = ms.SetTrafficMetric("node3", "node2", 5.5, 6.5)
	if err != nil {
		t.Errorf("Unable to set net metric")
	}
	err = ms.SetLatencyMetric("node1", "node2", 6, 7)
	if err != nil {
		t.Errorf("Unable to set net metric")
	}
	err = ms.SetTrafficMetric("node1", "node2", 6.1, 7.1)
	if err != nil {
		t.Errorf("Unable to set net metric")
	}
	err = ms.SetLatencyMetric("node1", "node3", 7, 8)
	if err != nil {
		t.Errorf("Unable to set net metric")
	}
	err = ms.SetTrafficMetric("node1", "node3", 7.1, 8.1)
	if err != nil {
		t.Errorf("Unable to set net metric")
	}
	err = ms.SetLatencyMetric("node2", "node1", 8, 9)
	if err != nil {
		t.Errorf("Unable to set net metric")
	}
	err = ms.SetTrafficMetric("node2", "node1", 8.1, 9.1)
	if err != nil {
		t.Errorf("Unable to set net metric")
	}
	err = ms.SetLatencyMetric("node2", "node3", 9, 0)
	if err != nil {
		t.Errorf("Unable to set net metric")
	}
	err = ms.SetTrafficMetric("node2", "node3", 9.1, 0.1)
	if err != nil {
		t.Errorf("Unable to set net metric")
	}
	err = ms.SetLatencyMetric("node3", "node1", 0, 1)
	if err != nil {
		t.Errorf("Unable to set net metric")
	}
	err = ms.SetTrafficMetric("node3", "node1", 0.1, 1.1)
	if err != nil {
		t.Errorf("Unable to set net metric")
	}
	err = ms.SetLatencyMetric("node3", "node2", 1, 2)
	if err != nil {
		t.Errorf("Unable to set net metric")
	}
	err = ms.SetTrafficMetric("node3", "node2", 1.1, 2.1)
	if err != nil {
		t.Errorf("Unable to set net metric")
	}

	// logTimeLapse("Set network metrics: ")

	fmt.Println("Get network metrics")
	lat, mean, err = ms.GetLastLatencyMetric("node1", "node2")
	if err != nil {
		t.Errorf("Net metric should exist")
	} else if lat != 6 || mean != 7 {
		t.Errorf("Invalid metric values")
	fmt.Println("Get last metric")
	getTags = map[string]string{tag1: "tag1", tag2: "tag2"}
	getFields = []string{field1, field2, field3, field4}
	result, err := ms.GetMetric(metric, getTags, getFields, "", 1)
	if err != nil || len(result) != 1 {
		t.Errorf("Failed to get metric")
	}
	tput, loss, err := ms.GetLastTrafficMetric("node1", "node2")
	if err != nil {
		t.Errorf("Net metric should exist")
	} else if tput != 6.1 || loss != 7.1 {
		t.Errorf("Invalid metric values")
	if !validateMetric(result[0], false, "val2", 1, 1.1) {
		t.Errorf("Invalid result")
	}
	lat, mean, err = ms.GetLastLatencyMetric("node1", "node3")
	if err != nil {
		t.Errorf("Net metric should exist")
	} else if lat != 7 || mean != 8 {
		t.Errorf("Invalid metric values")
	}
	tput, loss, err = ms.GetLastTrafficMetric("node1", "node3")
	if err != nil {
		t.Errorf("Net metric should exist")
	} else if tput != 7.1 || loss != 8.1 {
		t.Errorf("Invalid metric values")
	}
	lat, mean, err = ms.GetLastLatencyMetric("node2", "node1")
	if err != nil {
		t.Errorf("Net metric should exist")
	} else if lat != 8 || mean != 9 {
		t.Errorf("Invalid metric values")
	}
	tput, loss, err = ms.GetLastTrafficMetric("node2", "node1")
	if err != nil {
		t.Errorf("Net metric should exist")
	} else if tput != 8.1 || loss != 9.1 {
		t.Errorf("Invalid metric values")
	}
	lat, mean, err = ms.GetLastLatencyMetric("node2", "node3")
	if err != nil {
		t.Errorf("Net metric should exist")
	} else if lat != 9 || mean != 0 {
		t.Errorf("Invalid metric values")

	fmt.Println("Get all metrics")
	getTags = map[string]string{tag1: "tag1", tag2: "tag2"}
	getFields = []string{field1, field2, field3, field4}
	result, err = ms.GetMetric(metric, getTags, getFields, "", 0)
	if err != nil || len(result) != 2 {
		t.Errorf("Failed to get metric")
	}
	tput, loss, err = ms.GetLastTrafficMetric("node2", "node3")
	if err != nil {
		t.Errorf("Net metric should exist")
	} else if tput != 9.1 || loss != 0.1 {
		t.Errorf("Invalid metric values")
	if !validateMetric(result[0], false, "val2", 1, 1.1) {
		t.Errorf("Invalid result")
	}
	lat, mean, err = ms.GetLastLatencyMetric("node3", "node1")
	if err != nil {
		t.Errorf("Net metric should exist")
	} else if lat != 0 || mean != 1 {
		t.Errorf("Invalid metric values")
	if !validateMetric(result[1], true, "val1", 0, 0.0) {
		t.Errorf("Invalid result")
	}
	tput, loss, err = ms.GetLastTrafficMetric("node3", "node1")
	if err != nil {
		t.Errorf("Net metric should exist")
	} else if tput != 0.1 || loss != 1.1 {
		t.Errorf("Invalid metric values")

	fmt.Println("Get all metrics from the last 10 seconds")
	getTags = map[string]string{tag1: "tag1", tag2: "tag2"}
	getFields = []string{field1, field2, field3, field4}
	_, err = ms.GetMetric(metric, getTags, getFields, "10s", 0)
	if err != nil || len(result) != 2 {
		t.Errorf("Failed to get metric")
	}
	lat, mean, err = ms.GetLastLatencyMetric("node3", "node2")
	if err != nil {
		t.Errorf("Net metric should exist")
	} else if lat != 1 || mean != 2 {
		t.Errorf("Invalid metric values")
	if !validateMetric(result[0], false, "val2", 1, 1.1) {
		t.Errorf("Invalid result")
	}
	tput, loss, err = ms.GetLastTrafficMetric("node3", "node2")
	if err != nil {
		t.Errorf("Net metric should exist")
	} else if tput != 1.1 || loss != 2.1 {
		t.Errorf("Invalid metric values")
	if !validateMetric(result[1], true, "val1", 0, 0.0) {
		t.Errorf("Invalid result")
	}

	// logTimeLapse("Get network metrics: ")

	fmt.Println("Set event metric")
	err = ms.SetEventMetric("MOBILITY", "event1")
	if err != nil {
		t.Errorf("Unable to set event metric")
	}
	err = ms.SetEventMetric("NETWORK-CHARACTERISTIC-UPDATE", "event2")
	if err != nil {
		t.Errorf("Unable to set event metric")
	}
	err = ms.SetEventMetric("POAS-IN-RANGE", "event3")
	if err != nil {
		t.Errorf("Unable to set event metric")
	fmt.Println("Get all metrics from the last millisecond (none)")
	getTags = map[string]string{tag1: "tag1", tag2: "tag2"}
	getFields = []string{field1, field2, field3, field4}
	_, err = ms.GetMetric(metric, getTags, getFields, "1ms", 0)
	if err == nil {
		t.Errorf("Net metric list should be empty")
	}

	// logTimeLapse("Set event metrics: ")
	// t.Errorf("DONE")
}

	fmt.Println("Get event metrics")
	event, err := ms.GetLastEventMetric("MOBILITY")
	if err != nil {
		t.Errorf("Event metric should exist")
	} else if event != "event1" {
		t.Errorf("Invalid metric values")
func validateMetric(result map[string]interface{}, v1 bool, v2 string, v3 int32, v4 float64) bool {
	if result[field1] != v1 {
		fmt.Println("Invalid " + field1)
		return false
	}
	event, err = ms.GetLastEventMetric("NETWORK-CHARACTERISTIC-UPDATE")
	if err != nil {
		t.Errorf("Event metric should exist")
	} else if event != "event2" {
		t.Errorf("Invalid metric values")
	if result[field2] != v2 {
		fmt.Println("Invalid " + field2)
		return false
	}
	event, err = ms.GetLastEventMetric("POAS-IN-RANGE")
	if err != nil {
		t.Errorf("Event metric should exist")
	} else if event != "event3" {
		t.Errorf("Invalid metric values")
	if val, ok := result[field3].(json.Number); !ok || JsonNumToInt32(val) != v3 {
		fmt.Println("Invalid " + field3)
		return false
	}

	// logTimeLapse("Get event metrics: ")

	// t.Errorf("DONE")
	if val, ok := result[field4].(json.Number); !ok || JsonNumToFloat64(val) != v4 {
		fmt.Println("Invalid " + field4)
		return false
	}
	return true
}
+21 −2
Original line number Diff line number Diff line
@@ -45,7 +45,7 @@ func (ms *MetricStore) GetLastLatencyMetric(src string, dest string) (lat int32,
	tags := map[string]string{"src": src, "dest": dest}
	fields := []string{"lat", "mean"}
	var valuesArray []map[string]interface{}
	valuesArray, err = ms.GetMetric(metricLatency, tags, fields, "", "", 1)
	valuesArray, err = ms.GetMetric(metricLatency, tags, fields, "", 1)
	if err != nil {
		log.Error("Failed to retrieve metrics with error: ", err.Error())
		return
@@ -58,6 +58,25 @@ func (ms *MetricStore) GetLastLatencyMetric(src string, dest string) (lat int32,
	return
}

// GetLatencyMetrics
func (ms *MetricStore) GetLatencyMetrics(src string, dest string, duration string, count int) (metrics []map[string]interface{}, err error) {
	// Make sure we have set a store
	if ms.name == "" {
		err = errors.New("Store name not specified")
		return
	}

	// Get Latency metrics
	tags := map[string]string{"src": src, "dest": dest}
	fields := []string{"lat", "mean"}
	metrics, err = ms.GetMetric(metricLatency, tags, fields, duration, count)
	if err != nil {
		log.Error("Failed to retrieve metrics with error: ", err.Error())
		return
	}
	return
}

// SetTrafficMetric
func (ms *MetricStore) SetTrafficMetric(src string, dest string, tput float64, loss float64) error {
	tags := map[string]string{"src": src, "dest": dest}
@@ -77,7 +96,7 @@ func (ms *MetricStore) GetLastTrafficMetric(src string, dest string) (tput float
	tags := map[string]string{"src": src, "dest": dest}
	fields := []string{"tput", "loss"}
	var valuesArray []map[string]interface{}
	valuesArray, err = ms.GetMetric(metricTraffic, tags, fields, "", "", 1)
	valuesArray, err = ms.GetMetric(metricTraffic, tags, fields, "", 1)
	if err != nil {
		log.Error("Failed to retrieve metrics with error: ", err.Error())
		return
Loading