Esempio n. 1
0
func BenchmarkHttpPut(b *testing.B) {
	var data []byte
	batch := 200
	for i := 0; i < batch; i++ {
		msg := message.NewByData(message.NewData([]byte(utils.RandString(200))))
		data = append(data, msg.Bytes()...)
	}
	r := bytes.NewReader(data)
	client := &http.Client{}
	buffer := 0
	url := "http://localhost:8611/put?topic=http-test&size=200"
	for i := 0; i < b.N; i++ {
		if buffer < batch {
			buffer++
			continue
		}
		buffer = 0
		r.Seek(0, 0)
		resp, err := client.Post(url, "", r)
		if err != nil {
			b.Fatal(err)
		}
		io.Copy(ioutil.Discard, resp.Body)
		resp.Body.Close()
	}
}
Esempio n. 2
0
func apiSyncPut(topic string, b *testing.B) {
	var data []*message.Ins
	batch := 200
	for i := 0; i < batch; i++ {
		d := message.NewData([]byte(utils.RandString(200)))
		data = append(data, message.NewByData(d))
	}

	client, err := api.New(":12345")
	if err != nil {
		b.Fatal(err)
	}
	defer client.Close()

	if b.N < batch {
		_, err := client.Put(topic, data[:b.N])
		if err != nil {
			b.Error(err)
		}
		return
	}
	for i := 0; i < b.N; i += batch {
		_, err := client.Put(topic, data)
		if err != nil {
			b.Error(err)
			return
		}
	}
}
Esempio n. 3
0
func TestConsumer(t *testing.T) {
	que, ln := runServer(t)
	defer closeServer(que, ln)

	config := &Config{
		Endpoint: ":12345",
		Size:     100,
		Topic:    "test-consumer",
	}
	if a, err := New(config.Endpoint); err != nil {
		logex.Fatal(err)
	} else if err := a.Delete(config.Topic); err != nil && !logex.Equal(err, ErrTopicNotFound) {
		logex.Fatal(err)
	}

	c, err := NewConsumer(config)
	if err != nil {
		logex.Fatal(err)
	}
	c.Start()

	var wg sync.WaitGroup
	wg.Add(config.Size)

	go func() {
		for reply := range c.ReplyChan {
			for _ = range reply.Msgs {
				wg.Done()
			}
		}
	}()

	a, err := New(config.Endpoint)
	if err != nil {
		logex.Fatal(err)
	}
	m := message.NewByData(message.NewData([]byte(utils.RandString(256))))
	msgs := make([]*message.Ins, config.Size)
	for i := 0; i < len(msgs); i++ {
		msgs[i] = m
	}
	n, err := a.Put(config.Topic, msgs)
	if err != nil {
		logex.Fatal(err)
	}
	if n != len(msgs) {
		logex.Fatal("not match")
	}
	wg.Wait()
}