Skip to content

Commit

Permalink
nsqd: refactor stats collection for regression introduced in nsqio#242
Browse files Browse the repository at this point in the history
  • Loading branch information
mreiferson committed Aug 11, 2013
1 parent 1154c59 commit f25f1bb
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 18 deletions.
32 changes: 14 additions & 18 deletions nsqd/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
38 changes: 38 additions & 0 deletions nsqd/stats_test.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit f25f1bb

Please sign in to comment.