Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

add mmvdump and more client tests #18

Merged
merged 17 commits into from Jul 28, 2016
View
@@ -4,6 +4,8 @@ import (
"fmt"
"os"
"testing"
+
+ "github.com/performancecopilot/speed/mmvdump"
)
func TestMmvFileLocation(t *testing.T) {
@@ -117,3 +119,122 @@ func TestMapping(t *testing.T) {
EraseFileOnStop = false
}
+
+func TestWritingSingletonMetric(t *testing.T) {
+ c, err := NewPCPClient("test", ProcessFlag)
+ if err != nil {
+ t.Error(err)
+ return
+ }
+
+ met, err := NewPCPSingletonMetric(10, "test.1", Int32Type, CounterSemantics, OneUnit, "test", "")
+ if err != nil {
+ t.Error(err)
+ return
+ }
+ c.MustRegister(met)
+
+ c.MustStart()
+ defer c.MustStop()
+
+ h, toc, m, v, i, ind, s, err := mmvdump.Dump(c.buffer.Bytes())
+
+ if int(h.Toc) != len(toc) {
+ t.Errorf("expected the number of tocs specified in the header and the number of tocs in the toc array to be the same, h.Toc = %d, len(tocs) = %d", h.Toc, len(toc))
+ }
+
+ if h.Toc != 3 {
+ t.Errorf("expected client to write %d tocs, written %d", 3, h.Toc)
+ }
+
+ if h.Flag != int32(ProcessFlag) {
+ t.Errorf("expected client to write a ProcessFlag, writing %v", MMVFlag(h.Flag))
+ }
+
+ if len(m) != 1 {
+ t.Errorf("expected to write %d metrics, writing %d", 1, len(m))
+ }
+
+ // metrics
+
+ off := c.r.metricsoffset
+ metric, ok := m[uint64(off)]
+ if !ok {
+ t.Errorf("expected the metric to exist at offset %v", off)
+ }
+
+ if metric.Indom != mmvdump.NoIndom {
+ t.Error("expected indom to be null")
+ }
+
+ if int32(metric.Sem) != int32(CounterSemantics) {
+ t.Errorf("expected semantics to be %v, got %v", CounterSemantics, MetricSemantics(metric.Sem))
+ }
+
+ if int32(metric.Typ) != int32(Int32Type) {
+ t.Errorf("expected type to be %v, got %v", Int32Type, MetricType(metric.Typ))
+ }
+
+ if int32(metric.Unit) != int32(OneUnit) {
+ t.Errorf("expected unit to be %v, got %v", OneUnit, metric.Unit)
+ }
+
+ if metric.Shorttext == 0 {
+ t.Error("expected shorttext offset to not be 0")
+ }
+
+ if metric.Shorttext != uint64(c.r.stringsoffset) {
+ t.Errorf("expected shorttext offset to be %v", c.r.stringsoffset)
+ }
+
+ if metric.Longtext != 0 {
+ t.Errorf("expected longtext offset to be 0")
+ }
+
+ // values
+
+ mv, ok := v[uint64(c.r.valuesoffset)]
+ if !ok {
+ t.Errorf("expected a value to be written at offset %v", c.r.valuesoffset)
+ }
+
+ if mv.Metric != uint64(off) {
+ t.Errorf("expected value's metric to be at %v", off)
+ }
+
+ if av, err := mmvdump.FixedVal(mv.Val, mmvdump.Int32Type); err != nil || av.(int32) != 10 {
+ t.Errorf("expected the value to be %v, got %v", 10, av)
+ }
+
+ if mv.Instance != 0 {
+ t.Errorf("expected value instance to be 0")
+ }
+
+ // strings
+
+ if len(s) != 1 {
+ t.Error("expected one string")
+ }
+
+ str, ok := s[uint64(c.r.stringsoffset)]
+ if !ok {
+ t.Errorf("expected a string to be written at offset %v", c.r.stringsoffset)
+ }
+
+ sv := string(str.Payload[:4])
+ if sv != "test" {
+ t.Errorf("expected payload to be %v, got %v", "test", sv)
+ }
+
+ // instances
+
+ if len(i) != 0 {
+ t.Error("expected no instances when writing a singleton metric")
+ }
+
+ // indoms
+
+ if len(ind) != 0 {
+ t.Error("expected no indoms when writing a singleton metric")
+ }
+}
View
@@ -0,0 +1,55 @@
+package mmvdump
+
+import (
+ "os"
+ "testing"
+)
+
+func TestMmvDump1(t *testing.T) {
+ f, err := os.Open("testdata/test1.mmv")
+ if err != nil {
+ panic(err)
+ }
+
+ s, err := os.Stat("testdata/test1.mmv")
+ if err != nil {
+ panic(err)
+ }
+
+ data := make([]byte, s.Size())
+ f.Read(data)
+
+ h, tocs, metrics, values, instances, indoms, strings, err := Dump(data)
+ if err != nil {
+ t.Error(err)
+ return
+ }
+
+ if h.G1 != h.G2 {
+ t.Error("Invalid Header")
+ }
+
+ if len(tocs) != 3 {
+ t.Errorf("expected number of tocs %d, got %d", 3, len(tocs))
+ }
+
+ if len(indoms) != 0 {
+ t.Errorf("expected number of indoms %d, got %d", 0, len(indoms))
+ }
+
+ if len(strings) != 2 {
+ t.Errorf("expected number of strings %d, got %d", 2, len(strings))
+ }
+
+ if len(metrics) != 1 {
+ t.Errorf("expected number of strings %d, got %d", 1, len(metrics))
+ }
+
+ if len(values) != 1 {
+ t.Errorf("expected number of strings %d, got %d", 1, len(values))
+ }
+
+ if len(instances) != 0 {
+ t.Errorf("expected number of strings %d, got %d", 0, len(instances))
+ }
+}