Exemple #1
0
func TestCache(t *testing.T) {

	c := New()

	c.Add(points.OnePoint("hello.world", 42, 10))

	if c.Size() != 1 {
		t.FailNow()
	}

	c.Add(points.OnePoint("hello.world", 15, 12))

	if c.Size() != 2 {
		t.FailNow()
	}

	values := c.Pop()

	if values.Metric != "hello.world" {
		t.FailNow()
	}

	if len(values.Data) != 2 {
		t.FailNow()
	}

	if c.Size() != 0 {
		t.FailNow()
	}
}
Exemple #2
0
// Stat sends internal statistics to cache
func (p *Whisper) Stat(metric string, value float64) {
	p.in <- points.OnePoint(
		fmt.Sprintf("%spersister.%s", p.graphPrefix, metric),
		value,
		time.Now().Unix(),
	)
}
Exemple #3
0
// Stat sends internal statistics to cache
func (rcv *UDP) Stat(metric string, value float64) {
	rcv.out <- points.OnePoint(
		fmt.Sprintf("%s%s", rcv.graphPrefix, metric),
		value,
		time.Now().Unix(),
	)
}
Exemple #4
0
func TestStopConnectedTCP(t *testing.T) {
	test := newTCPTestCase(t, false)
	defer test.Finish()

	ch := test.rcvChan
	test.Send("hello.world 42.15 1422698155\n")
	time.Sleep(10 * time.Millisecond)

	select {
	case msg := <-ch:
		test.Eq(msg, points.OnePoint("hello.world", 42.15, 1422698155))
	default:
		t.Fatalf("Message #0 not received")
	}

	test.receiver.Stop()
	test.receiver = nil
	time.Sleep(10 * time.Millisecond)

	test.Send("metric.name -72.11 1422698155\n")
	time.Sleep(10 * time.Millisecond)

	select {
	case <-ch:
		t.Fatalf("Message #0 received")
	default:
	}
}
Exemple #5
0
func TestThrottleChan(t *testing.T) {
	perSecond := 100
	timestamp := time.Now().Unix()

	chIn := make(chan *points.Points)
	chOut := throttleChan(chIn, perSecond, nil)
	wait := time.After(time.Second)

	bw := 0

LOOP:
	for {
		select {
		case <-wait:
			break LOOP
		default:
		}
		chIn <- points.OnePoint("metric", 1, timestamp)
		<-chOut
		bw++
	}
	close(chIn)

	max := float64(perSecond) * 1.05
	min := float64(perSecond) * 0.95

	assert.True(t, float64(bw) >= min)
	assert.True(t, float64(bw) <= max)
}
Exemple #6
0
func TestCacheCheckpoint(t *testing.T) {
	cache := New()
	cache.Start()
	cache.SetOutputChanSize(0)

	defer cache.Stop()

	startTime := time.Now().Unix() - 60*60

	sizes := []int{1, 15, 42, 56, 22, 90, 1}

	for index, value := range sizes {
		metricName := fmt.Sprintf("metric%d", index)

		for i := value; i > 0; i-- {
			cache.In() <- points.OnePoint(metricName, float64(i), startTime+int64(i))
		}

	}

	time.Sleep(100 * time.Millisecond)
	cache.doCheckpoint()

	d := <-cache.Out()
	if d.Metric != "metric0" {
		t.Fatal("wrong metric received")
	}

	systemMetrics := []string{
		"carbon.cache.inputLenAfterCheckpoint",
		"carbon.cache.inputLenBeforeCheckpoint",
		"carbon.cache.checkpointTime",
		"carbon.cache.overflow",
		"carbon.cache.queries",
		"carbon.cache.metrics",
		"carbon.cache.size",
	}

	for _, metricName := range systemMetrics {
		d = <-cache.Out()
		if d.Metric != metricName {
			t.Fatalf("%#v != %#v", d.Metric, metricName)
		}
	}

	result := sizes[1:]
	sort.Sort(sort.Reverse(sort.IntSlice(result)))

	for _, size := range result {
		d = <-cache.Out()
		if len(d.Data) != size {
			t.Fatalf("wrong metric received. Waiting metric with %d points, received with %d", size, len(d.Data))
		}
	}
}
Exemple #7
0
func TestUDP2(t *testing.T) {
	test := newUDPTestCase(t)
	defer test.Finish()

	test.Send("hello.world 42.15 1422698155\nmetric.name -72.11 1422698155\n")

	select {
	case msg := <-test.rcvChan:
		test.Eq(msg, points.OnePoint("hello.world", 42.15, 1422698155))
	default:
		t.Fatalf("Message #0 not received")
	}

	select {
	case msg := <-test.rcvChan:
		test.Eq(msg, points.OnePoint("metric.name", -72.11, 1422698155))
	default:
		t.Fatalf("Message #1 not received")
	}
}
Exemple #8
0
func randomPoints(num int, out chan *points.Points) {
	var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
	var i int
	for i = 0; i < num; i++ {
		b := make([]rune, 32)
		for i := range b {
			b[i] = letters[rand.Intn(len(letters))]
		}
		metric := string(b)
		p := points.OnePoint(metric, rand.Float64(), rand.Int63())
		out <- p
	}
}
Exemple #9
0
func TestTCP2(t *testing.T) {
	test := newTCPTestCase(t, false)
	defer test.Finish()

	test.Send("hello.world 42.15 1422698155\nmetric.name -72.11 1422698155\n")

	time.Sleep(10 * time.Millisecond)

	select {
	case msg := <-test.rcvChan:
		test.Eq(msg, points.OnePoint("hello.world", 42.15, 1422698155))
	default:
		t.Fatalf("Message #0 not received")
	}

	select {
	case msg := <-test.rcvChan:
		test.Eq(msg, points.OnePoint("metric.name", -72.11, 1422698155))
	default:
		t.Fatalf("Message #1 not received")
	}
}
Exemple #10
0
// Stat sends internal statistics to cache
func (rcv *TCP) Stat(metric string, value float64) {
	var protocolPrefix string

	if rcv.isPickle {
		protocolPrefix = "pickle"
	} else {
		protocolPrefix = "tcp"
	}

	rcv.out <- points.OnePoint(
		fmt.Sprintf("%s%s.%s", rcv.graphPrefix, protocolPrefix, metric),
		value,
		time.Now().Unix(),
	)
}
Exemple #11
0
func TestStat(t *testing.T) {
	fixture := Whisper{
		graphPrefix: "bing.bang.",
	}
	fixture.in = make(chan *points.Points)
	go func() {
		output := <-fixture.in
		expected := points.OnePoint(
			"bing.bang.persister.foo.bar",
			1.5,
			time.Now().Unix(),
		)
		assert.Equal(t, output, expected)
	}()
	fixture.Stat("foo.bar", 1.5)

}
Exemple #12
0
func TestStat(t *testing.T) {
	mock := clock.NewMock()
	app.Clock = mock
	fixture := Whisper{
		graphPrefix: "bing.bang.",
	}
	fixture.in = make(chan *points.Points)
	go func() {
		output := <-fixture.in
		expected := points.OnePoint(
			"bing.bang.persister.foo.bar",
			1.5,
			0,
		)
		assert.Equal(t, output, expected)
	}()
	fixture.Stat("foo.bar", 1.5)

}
Exemple #13
0
func TestPickle(t *testing.T) {
	// > python
	// >>> import pickle, struct
	// >>> listOfMetricTuples = [("hello.world", (1452200952, 42))]
	// >>> payload = pickle.dumps(listOfMetricTuples, protocol=2)
	// >>> header = struct.pack("!L", len(payload))
	// >>> message = header + payload
	// >>> print repr(message)
	// '\x00\x00\x00#\x80\x02]q\x00U\x0bhello.worldq\x01J\xf8\xd3\x8eVK*\x86q\x02\x86q\x03a.'

	test := newTCPTestCase(t, true)
	defer test.Finish()

	test.Send("\x00\x00\x00#\x80\x02]q\x00U\x0bhello.worldq\x01J\xf8\xd3\x8eVK*\x86q\x02\x86q\x03a.")

	time.Sleep(10 * time.Millisecond)

	select {
	case msg := <-test.rcvChan:
		test.Eq(msg, points.OnePoint("hello.world", 42, 1452200952))
	default:
		t.Fatalf("Message #0 not received")
	}
}
Exemple #14
0
// stat send internal statistics of cache
func (c *Cache) stat(metric string, value float64) {
	key := fmt.Sprintf("%scache.%s", c.graphPrefix, metric)
	c.Add(points.OnePoint(key, value, time.Now().Unix()))
	c.queue = append(c.queue, &queueItem{key, 1})
}
Exemple #15
0
func TestCarbonlink(t *testing.T) {
	assert := assert.New(t)

	cache := New()
	cache.Start()
	cache.SetOutputChanSize(0)

	msg1 := points.OnePoint(
		"carbon.agents.carbon_agent_server.cache.size",
		42.17,
		1422797285,
	)

	msg2 := points.OnePoint(
		"carbon.agents.carbon_agent_server.param.size",
		-42.14,
		1422797267,
	)

	msg3 := points.OnePoint(
		"carbon.agents.carbon_agent_server.param.size",
		15,
		1422795966,
	)

	cache.In() <- msg1
	cache.In() <- msg2
	cache.In() <- msg3

	defer cache.Stop()

	time.Sleep(50 * time.Millisecond)

	addr, err := net.ResolveTCPAddr("tcp", "localhost:0")
	assert.NoError(err)

	carbonlink := NewCarbonlinkListener(cache.Query())
	defer carbonlink.Stop()

	assert.NoError(carbonlink.Listen(addr))

	conn, err := net.Dial("tcp", carbonlink.Addr().String())
	assert.NoError(err)

	conn.SetDeadline(time.Now().Add(time.Second))
	defer conn.Close()

	var replyLength int32
	var data []byte

	/* MESSAGE 1 */

	_, err = conn.Write([]byte(sampleCacheQuery))
	assert.NoError(err)

	err = binary.Read(conn, binary.BigEndian, &replyLength)
	assert.NoError(err)

	data = make([]byte, replyLength)

	err = binary.Read(conn, binary.BigEndian, data)
	assert.NoError(err)

	// {u'datapoints': [(1422797285, 42.17)]}
	assert.Equal("\x80\x02}(X\n\x00\x00\x00datapoints](J\xe5)\xceTG@E\x15\xc2\x8f\\(\xf6\x86eu.", string(data))

	/* MESSAGE 2 */
	_, err = conn.Write([]byte(sampleCacheQuery2))
	assert.NoError(err)

	err = binary.Read(conn, binary.BigEndian, &replyLength)
	assert.NoError(err)

	data = make([]byte, replyLength)

	err = binary.Read(conn, binary.BigEndian, data)
	assert.NoError(err)

	// {u'datapoints': [(1422797267, -42.14), (1422795966, 15.0)]}
	assert.Equal("\x80\x02}(X\n\x00\x00\x00datapoints](J\xd3)\xceTG\xc0E\x11\xeb\x85\x1e\xb8R\x86J\xbe$\xceTG@.\x00\x00\x00\x00\x00\x00\x86eu.",
		string(data))

	/* MESSAGE 3 */
	/* Remove carbon.agents.carbon_agent_server.param.size from cache and request again */

	for {
		c := <-cache.Out()
		if c.Metric == "carbon.agents.carbon_agent_server.param.size" {
			break
		}
	}

	_, err = conn.Write([]byte(sampleCacheQuery2))
	assert.NoError(err)

	err = binary.Read(conn, binary.BigEndian, &replyLength)
	assert.NoError(err)

	data = make([]byte, replyLength)

	err = binary.Read(conn, binary.BigEndian, data)
	assert.NoError(err)

	assert.Equal("\x80\x02}(X\n\x00\x00\x00datapoints](eu.", string(data))

	/* WRONG MESSAGE TEST */
	logging.Test(func(log *bytes.Buffer) { // silent logs
		_, err = conn.Write([]byte("\x00\x00\x00\x05aaaaa"))
		assert.NoError(err)

		err = binary.Read(conn, binary.BigEndian, &replyLength)

		assert.Error(err)
		assert.Equal(io.EOF, err)
	})
}