From f25f1bb06b33a43cc0b96b3ce44b63f46b225087 Mon Sep 17 00:00:00 2001 From: Matt Reiferson Date: Sun, 11 Aug 2013 18:40:39 -0400 Subject: [PATCH] nsqd: refactor stats collection for regression introduced in #242 --- nsqd/stats.go | 32 ++++++++++++++------------------ nsqd/stats_test.go | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 18 deletions(-) create mode 100644 nsqd/stats_test.go diff --git a/nsqd/stats.go b/nsqd/stats.go index 8e764d173..02c819341 100644 --- a/nsqd/stats.go +++ b/nsqd/stats.go @@ -89,38 +89,34 @@ func (n *NSQd) getStats() []TopicStats { n.RLock() defer n.RUnlock() - realTopics := make([]*Topic, len(n.topicMap)) - topics := make([]TopicStats, len(n.topicMap)) - topic_index := 0 + realTopics := make([]*Topic, 0, len(n.topicMap)) for _, t := range n.topicMap { - realTopics[topic_index] = t - topic_index++ + realTopics = append(realTopics, t) } - sort.Sort(TopicsByName{realTopics}) - for topic_index, t := range realTopics { + + topics := make([]TopicStats, 0, len(n.topicMap)) + for _, t := range realTopics { t.RLock() - realChannels := make([]*Channel, len(t.channelMap)) - channel_index := 0 + realChannels := make([]*Channel, 0, len(t.channelMap)) for _, c := range t.channelMap { - realChannels[channel_index] = c - channel_index++ + realChannels = append(realChannels, c) } sort.Sort(ChannelsByName{realChannels}) - channels := make([]ChannelStats, len(t.channelMap)) - for channel_index, c := range realChannels { + channels := make([]ChannelStats, 0, len(t.channelMap)) + for _, c := range realChannels { c.RLock() - clients := make([]ClientStats, len(c.clients)) - for client_index, client := range c.clients { - clients[client_index] = client.Stats() + clients := make([]ClientStats, 0, len(c.clients)) + for _, client := range c.clients { + clients = append(clients, client.Stats()) } - channels[channel_index] = NewChannelStats(c, clients) + channels = append(channels, NewChannelStats(c, clients)) c.RUnlock() } - topics[topic_index] = NewTopicStats(t, channels) + topics = append(topics, NewTopicStats(t, channels)) t.RUnlock() } diff --git a/nsqd/stats_test.go b/nsqd/stats_test.go new file mode 100644 index 000000000..592b0a6af --- /dev/null +++ b/nsqd/stats_test.go @@ -0,0 +1,38 @@ +package main + +import ( + "github.com/bitly/nsq/nsq" + "github.com/bmizerany/assert" + "io/ioutil" + "log" + "os" + "strconv" + "testing" + "time" +) + +func TestStats(t *testing.T) { + log.SetOutput(ioutil.Discard) + defer log.SetOutput(os.Stdout) + + options := NewNsqdOptions() + tcpAddr, _, nsqd := mustStartNSQd(options) + defer nsqd.Exit() + + topicName := "test_stats" + strconv.Itoa(int(time.Now().Unix())) + topic := nsqd.GetTopic(topicName) + msg := nsq.NewMessage(<-nsqd.idChan, []byte("test body")) + topic.PutMessage(msg) + + conn, err := mustConnectNSQd(tcpAddr) + assert.Equal(t, err, nil) + + identify(t, conn) + sub(t, conn, topicName, "ch") + + stats := nsqd.getStats() + assert.Equal(t, len(stats), 1) + assert.Equal(t, len(stats[0].Channels), 1) + assert.Equal(t, len(stats[0].Channels[0].Clients), 1) + log.Printf("stats: %+v", stats) +}