From 620b492467cbf552cf19d82b6e1234fdf962f69a Mon Sep 17 00:00:00 2001 From: Matt Reiferson Date: Sun, 11 Aug 2013 17:29:48 -0400 Subject: [PATCH] nsqd: refactor stats collection for regression introduced in #242 --- nsqd/stats.go | 32 ++++++++++++++------------------ nsqd/stats_test.go | 12 +++++++++++- 2 files changed, 25 insertions(+), 19 deletions(-) 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 index 6ca6838fe..592b0a6af 100644 --- a/nsqd/stats_test.go +++ b/nsqd/stats_test.go @@ -1,7 +1,14 @@ package main import ( + "github.com/bitly/nsq/nsq" + "github.com/bmizerany/assert" + "io/ioutil" + "log" + "os" + "strconv" "testing" + "time" ) func TestStats(t *testing.T) { @@ -24,5 +31,8 @@ func TestStats(t *testing.T) { sub(t, conn, topicName, "ch") stats := nsqd.getStats() - assert.Equal(t, false, true) + 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) }