func TestMeasureQueues(t *testing.T) { // Setup Listen apiConfig := coco.ApiConfig{ Bind: "127.0.0.1:26081", } var tiers []coco.Tier blacklisted := map[string]map[string]int64{} go coco.Api(apiConfig, &tiers, &blacklisted) poll(t, apiConfig.Bind) // Setup Measure chans := map[string]chan collectd.Packet{ "a": make(chan collectd.Packet, 1000), "b": make(chan collectd.Packet, 1000), "c": make(chan collectd.Packet, 1000), } measureConfig := coco.MeasureConfig{ TickInterval: *new(coco.Duration), } measureConfig.TickInterval.UnmarshalText([]byte("10ms")) go coco.Measure(measureConfig, chans, &tiers) // Test pushing packets for _, c := range chans { for i := 0; i < 950; i++ { c <- collectd.Packet{} } } time.Sleep(10 * time.Millisecond) // Test resp, err := http.Get("http://127.0.0.1:26081/debug/vars") if err != nil { t.Fatalf("HTTP GET failed: %s", err) } body, err := ioutil.ReadAll(resp.Body) var result map[string]interface{} err = json.Unmarshal(body, &result) if err != nil { t.Errorf("Error when decoding JSON %+v.", err) t.Errorf("Response body: %s", string(body)) t.FailNow() } counts := result["coco"].(map[string]interface{})["queues"].(map[string]interface{}) expected := 950 for k, v := range counts { c := int(v.(float64)) if c != expected { t.Errorf("Expected %s to equal %d, got %.2f", k, expected, v) } } }
func main() { kingpin.Version("1.0.0") kingpin.Parse() var config coco.Config if _, err := toml.DecodeFile(*configPath, &config); err != nil { log.Fatalln("fatal:", err) return } // Setup data structures to be shared across components blacklisted := map[string]map[string]int64{} raw := make(chan collectd.Packet, 1000000) filtered := make(chan collectd.Packet, 1000000) items := make(chan coco.BlacklistItem, 1000000) var tiers []coco.Tier for k, v := range config.Tiers { tier := coco.Tier{Name: k, Targets: v.Targets} tiers = append(tiers, tier) } if len(tiers) == 0 { log.Fatal("No tiers configured. Exiting.") } chans := map[string]chan collectd.Packet{ "raw": raw, "filtered": filtered, //"blacklist_items": items, } go coco.Measure(config.Measure, chans, &tiers) // Launch components to do the work go coco.Listen(config.Listen, raw) for i := 0; i < 4; i++ { go coco.Filter(config.Filter, raw, filtered, items) } go coco.Blacklist(items, &blacklisted) go coco.Send(&tiers, filtered) coco.Api(config.Api, &tiers, &blacklisted) }
func TestMeasureDistributionSummaryStats(t *testing.T) { // Setup tiers tierConfig := make(map[string]coco.TierConfig) tierConfig["a"] = coco.TierConfig{Targets: []string{"127.0.0.1:25811", "127.0.0.1:25812", "127.0.0.1:25813"}} tierConfig["b"] = coco.TierConfig{Targets: []string{"127.0.0.1:25821", "127.0.0.1:25822", "127.0.0.1:25823"}} tierConfig["c"] = coco.TierConfig{Targets: []string{"127.0.0.1:25831", "127.0.0.1:25832", "127.0.0.1:25833"}} for _, v := range tierConfig { for _, target := range v.Targets { go MockListener(t, target) } } var tiers []coco.Tier for k, v := range tierConfig { tier := coco.Tier{Name: k, Targets: v.Targets} tiers = append(tiers, tier) } // Setup Api apiConfig := coco.ApiConfig{ Bind: "127.0.0.1:26810", } blacklisted := map[string]map[string]int64{} go coco.Api(apiConfig, &tiers, &blacklisted) poll(t, apiConfig.Bind) // Setup Measure chans := map[string]chan collectd.Packet{} measureConfig := coco.MeasureConfig{ TickInterval: *new(coco.Duration), } measureConfig.TickInterval.UnmarshalText([]byte("100ms")) go coco.Measure(measureConfig, chans, &tiers) // Setup Send filtered := make(chan collectd.Packet) go coco.Send(&tiers, filtered) // Push packets to Send // 1000 hosts for i := 0; i < 1000; i++ { // up to 24 cpus per host iter := rand.Intn(24) for n := 0; n < iter; n++ { types := []string{"user", "system", "steal", "wait"} for _, typ := range types { filtered <- collectd.Packet{ Hostname: "foo" + string(i), Plugin: "cpu-" + strconv.Itoa(n), Type: "cpu-" + typ, } } } } time.Sleep(10 * time.Millisecond) // Fetch exposed expvars resp, err := http.Get("http://127.0.0.1:26810/debug/vars") if err != nil { t.Fatalf("HTTP GET failed: %s", err) } body, err := ioutil.ReadAll(resp.Body) var result map[string]interface{} err = json.Unmarshal(body, &result) if err != nil { t.Errorf("Error when decoding JSON %+v.", err) t.Errorf("Response body: %s", string(body)) t.FailNow() } // Test the exposed expvars data looks sane tierProps := result["coco"].(map[string]interface{})["hash.metrics_per_host"].(map[string]interface{}) t.Logf("Expvar tier props: %+v\n", tierProps) if len(tiers) != len(tierProps) { t.Errorf("Expected %d tiers to be exposed, got %d\n", len(tiers), len(tierProps)) //t.Errorf("Tiers: %+v\n", tiers) t.Errorf("Exposed tiers: %+v\n", tierProps) } for _, tier := range tiers { targetProps := tierProps[tier.Name].(map[string]interface{}) if len(targetProps) != len(tier.Targets)+1 { t.Errorf("Expected %d targets to be exposed, got %d\n", len(tier.Targets)+1, len(targetProps)) t.Errorf("Tier targets: %+v\n", tier.Targets) t.Errorf("Exposed target properties: %+v\n", targetProps) } for _, target := range tier.Targets { props := targetProps[target].(map[string]interface{}) for _, k := range []string{"95e", "avg", "max", "min", "sum"} { if props[k] == nil { t.Errorf("Expected %s metric was not exposed on %s\n", k, target) } } } } }