func BenchmarkTopicPut(b *testing.B) { topic, err := New("bench-put", c) if err != nil { b.Fatal(err) } msg := mmq.NewMessageByData(mmq.NewMessageData([]byte(utils.RandString(256)))) reply := make(chan []error) var wg sync.WaitGroup go func() { for _ = range reply { wg.Done() } }() b.ResetTimer() buffer := []*mmq.Message{} for i := 0; i < b.N; i++ { m, _ := mmq.NewMessage(msg.Bytes(), true) buffer = append(buffer, m) if len(buffer) >= MaxPutBenchSize { wg.Add(1) topic.Put(buffer, reply) buffer = nil } } wg.Wait() }
func BenchmarkHttpPut(b *testing.B) { var data []byte batch := 200 for i := 0; i < batch; i++ { msg := mmq.NewMessageByData(mmq.NewMessageData([]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() } }
func TestTopic(t *testing.T) { topic, err := New("topicTest", c) if err != nil { t.Fatal(err) } var wg sync.WaitGroup var testSource = [][]byte{ []byte("hello!"), []byte("who are you"), []byte("oo?"), } wg.Add(len(testSource)) go func() { incoming := make(chan []*mmq.Message, len(testSource)) errChan := make(chan error) topic.Get(0, len(testSource), incoming, errChan) idx := 0 for { select { case msg := <-incoming: for _, m := range msg { if string(m.Data) != string(testSource[idx]) { t.Error("result not except", string(m.Data)) } wg.Done() idx++ } case err := <-errChan: logex.Error("get:", err) } } }() go func() { for _, m := range testSource { msg := mmq.NewMessageByData(mmq.NewMessageData(m)) errs := topic.PutSync([]*mmq.Message{msg}) logex.Error(errs) } }() wg.Wait() }
func BenchmarkTopicGet(b *testing.B) { n := b.N topic, err := New("bench-get", c) if err != nil { b.Fatal(err) } var wg2 sync.WaitGroup replyErrs := make(chan []error) go func() { for _ = range replyErrs { wg2.Done() } }() msg := mmq.NewMessageByData(mmq.NewMessageData([]byte(utils.RandString(256)))) var buffer []*mmq.Message for i := 0; i < n; i++ { buffer = append(buffer, msg) if len(buffer) > MaxPutBenchSize { wg2.Add(1) topic.Put(buffer, replyErrs) buffer = nil } } wg2.Wait() close(replyErrs) b.ResetTimer() reply := make(chan []*mmq.Message, 1024) size := 0 off := int64(0) var wg sync.WaitGroup go func() { for msgs := range reply { wg.Add(1) for _, m := range msgs { off += int64(len(m.Bytes())) wg.Done() } size -= len(msgs) wg.Done() if size == 0 { continue } } }() for i := 0; i < n; i++ { if size < MaxGetBenchSize { size++ continue } wg.Add(size) if err := topic.GetSync(off, size, reply); err != nil { b.Fatal(err) } wg.Wait() } close(reply) }