func TestRateCountPckt(t *testing.T) { cases := []Case{ Case{"foo.bar.unit=yes.baz", "foo.bar.unit=Pckt.baz.orig_unit=yes.pckt_type=sent.direction=in"}, Case{"foo.bar.unit=yes", "foo.bar.unit=Pckt.orig_unit=yes.pckt_type=sent.direction=in"}, Case{"unit=yes.foo.bar", "unit=Pckt.foo.bar.orig_unit=yes.pckt_type=sent.direction=in"}, Case{"target_type=count.foo.unit=ok.bar", "target_type=count.foo.unit=Pckt.bar.orig_unit=ok.pckt_type=sent.direction=in"}, } for _, c := range cases { assert.Equal(t, Count_Pckt(c.in, "prefix."), c.out) c = Case{ c.in, strings.Replace(strings.Replace(c.out, "unit=Pckt", "unit=Pcktps", -1), "target_type=count", "target_type=rate", -1), } assert.Equal(t, Rate_Pckt(c.in, "prefix."), c.out) } for _, c := range cases { c = Case{ strings.Replace(c.in, "=", "_is_", -1), strings.Replace(c.out, "=", "_is_", -1), } assert.Equal(t, Count_Pckt(c.in, "prefix."), c.out) c = Case{ c.in, strings.Replace(strings.Replace(c.out, "unit_is_Pckt", "unit_is_Pcktps", -1), "target_type_is_count", "target_type_is_rate", -1), } assert.Equal(t, Rate_Pckt(c.in, "prefix."), c.out) } }
// only 1 kind of stat is enough, cause they all behave the same func TestStat(t *testing.T) { cases := []Case{ Case{"foo.bar.unit=yes.baz", "foo.bar.unit=yes.baz.stat=upper_90"}, Case{"foo.bar.unit=yes", "foo.bar.unit=yes.stat=upper_90"}, Case{"unit=yes.foo.bar", "unit=yes.foo.bar.stat=upper_90"}, Case{"target_type=count.foo.unit=ok.bar", "target_type=count.foo.unit=ok.bar.stat=upper_90"}, } for _, c := range cases { assert.Equal(t, Upper(c.in, "prefix.", "90"), c.out) } // same but with equals and no percentile for i, c := range cases { cases[i] = Case{ strings.Replace(c.in, "=", "_is_", -1), strings.Replace(strings.Replace(c.out, "=", "_is_", -1), "upper_90", "upper", 1), } } for _, c := range cases { assert.Equal(t, Upper(c.in, "prefix.", ""), c.out) } }
func TestDerive_Count(t *testing.T) { // undefined edge case: // assert.Equal(t, Derive_Count("foo.bar.aunit=no.baz", "prefix."), "prefix.foo.bar.aunit=no.baz") // assert.Equal(t, Derive_Count("foo.bar.UNIT=no.baz", "prefix."), "prefix.foo.bar.UNIT=no.baz") // not metrics2.0, use prefix assert.Equal(t, Derive_Count("foo.bar.unita=no.bar", "prefix."), "prefix.foo.bar.unita=no.bar") assert.Equal(t, Derive_Count("foo.bar.target_type=count.baz", "prefix."), "prefix.foo.bar.target_type=count.baz") assert.Equal(t, Derive_Count("foo.bar.target_type=count", "prefix."), "prefix.foo.bar.target_type=count") assert.Equal(t, Derive_Count("target_type=count.foo.bar", "prefix."), "prefix.target_type=count.foo.bar") // metrics 2.0 cases with equals cases := []Case{ Case{"foo.bar.unit=yes.baz", "foo.bar.unit=yesps.baz"}, Case{"foo.bar.unit=yes", "foo.bar.unit=yesps"}, Case{"unit=yes.foo.bar", "unit=yesps.foo.bar"}, Case{"target_type=count.foo.unit=ok.bar", "target_type=rate.foo.unit=okps.bar"}, } for _, c := range cases { assert.Equal(t, Derive_Count(c.in, "prefix."), c.out) } // same but with equals for i, c := range cases { cases[i] = Case{ strings.Replace(c.in, "=", "_is_", -1), strings.Replace(c.out, "=", "_is_", -1), } } for _, c := range cases { assert.Equal(t, Derive_Count(c.in, "prefix."), c.out) } }
func TestHostPool(t *testing.T) { log.SetOutput(ioutil.Discard) defer log.SetOutput(os.Stdout) dummyErr := errors.New("Dummy Error") p := New([]string{"a", "b", "c"}) assert.Equal(t, p.Get().Host(), "a") assert.Equal(t, p.Get().Host(), "b") assert.Equal(t, p.Get().Host(), "c") respA := p.Get() assert.Equal(t, respA.Host(), "a") respA.Mark(dummyErr) respB := p.Get() respB.Mark(dummyErr) respC := p.Get() assert.Equal(t, respC.Host(), "c") respC.Mark(nil) // get again, and verify that it's still c assert.Equal(t, p.Get().Host(), "c") // now try to mark b as success; should fail because already marked respB.Mark(nil) assert.Equal(t, p.Get().Host(), "c") // would be b if it were not dead // now restore a respA = &standardHostPoolResponse{host: "a", pool: p} respA.Mark(nil) assert.Equal(t, p.Get().Host(), "a") assert.Equal(t, p.Get().Host(), "c") // ensure that we get *something* back when all hosts fail for _, host := range []string{"a", "b", "c"} { response := &standardHostPoolResponse{host: host, pool: p} response.Mark(dummyErr) } resp := p.Get() assert.NotEqual(t, resp, nil) }
func TestEpsilonGreedy(t *testing.T) { log.SetOutput(ioutil.Discard) defer log.SetOutput(os.Stdout) rand.Seed(10) iterations := 12000 p := NewEpsilonGreedy([]string{"a", "b"}, 0, &LinearEpsilonValueCalculator{}).(*epsilonGreedyHostPool) timings := make(map[string]int64) timings["a"] = 200 timings["b"] = 300 hitCounts := make(map[string]int) hitCounts["a"] = 0 hitCounts["b"] = 0 log.Printf("starting first run (a, b)") for i := 0; i < iterations; i += 1 { if i != 0 && i%100 == 0 { p.performEpsilonGreedyDecay() } hostR := p.Get() host := hostR.Host() hitCounts[host]++ timing := timings[host] p.timer = &mockTimer{t: int(timing)} hostR.Mark(nil) } for host := range hitCounts { log.Printf("host %s hit %d times (%0.2f percent)", host, hitCounts[host], (float64(hitCounts[host])/float64(iterations))*100.0) } assert.Equal(t, hitCounts["a"] > hitCounts["b"], true) hitCounts["a"] = 0 hitCounts["b"] = 0 log.Printf("starting second run (b, a)") timings["a"] = 500 timings["b"] = 100 for i := 0; i < iterations; i += 1 { if i != 0 && i%100 == 0 { p.performEpsilonGreedyDecay() } hostR := p.Get() host := hostR.Host() hitCounts[host]++ timing := timings[host] p.timer = &mockTimer{t: int(timing)} hostR.Mark(nil) } for host := range hitCounts { log.Printf("host %s hit %d times (%0.2f percent)", host, hitCounts[host], (float64(hitCounts[host])/float64(iterations))*100.0) } assert.Equal(t, hitCounts["b"] > hitCounts["a"], true) }