From: Matt Joiner Date: Wed, 27 Aug 2014 23:32:49 +0000 (+1000) Subject: Add useful new metrics to connection X-Git-Tag: v1.0.0~1585 X-Git-Url: http://www.git.stargrave.org/?a=commitdiff_plain;h=aeee372506fd752d3cbf0eb273dee871be17a34f;p=btrtrc.git Add useful new metrics to connection --- diff --git a/client.go b/client.go index bd49d978..92087547 100644 --- a/client.go +++ b/client.go @@ -1255,8 +1255,10 @@ func (me *Client) replenishConnRequests(t *torrent, c *connection) { } } +// Handle a received chunk from a peer. func (me *Client) downloadedChunk(t *torrent, c *connection, msg *pp.Message) error { chunksDownloadedCount.Add(1) + c.ChunksReceived++ req := newRequest(msg.Index, msg.Begin, pp.Integer(len(msg.Piece))) @@ -1271,6 +1273,9 @@ func (me *Client) downloadedChunk(t *torrent, c *connection, msg *pp.Message) er return nil } + c.UsefulChunksReceived++ + c.lastUsefulChunkReceived = time.Now() + // Write the chunk out. err := t.WriteChunk(int(msg.Index), int64(msg.Begin), msg.Piece) if err != nil { diff --git a/connection.go b/connection.go index 323f88ed..6f84c854 100644 --- a/connection.go +++ b/connection.go @@ -33,6 +33,13 @@ type connection struct { post chan pp.Message writeCh chan []byte + ChunksReceived int + UsefulChunksReceived int + + lastMessageReceived time.Time + completedHandshake time.Time + lastUsefulChunkReceived time.Time + // Stuff controlled by the local peer. Interested bool Choked bool @@ -64,6 +71,8 @@ func newConnection(sock net.Conn, peb peerExtensionBytes, peerID [20]byte) (c *c closing: make(chan struct{}), writeCh: make(chan []byte), post: make(chan pp.Message), + + completedHandshake: time.Now(), } go c.writer() go c.writeOptimizer(time.Minute) @@ -119,7 +128,7 @@ func (cn *connection) setNumPieces(num int) error { } func (cn *connection) WriteStatus(w io.Writer) { - fmt.Fprintf(w, "%q: %s-%s: %s completed, reqs: %d-%d, flags: ", cn.PeerID, cn.Socket.LocalAddr(), cn.Socket.RemoteAddr(), cn.completedString(), len(cn.Requests), len(cn.PeerRequests)) + fmt.Fprintf(w, "%-90s: %s completed, good chunks: %d/%d reqs: %d-%d, last msg: %.0fs ago age: %.1fmin last useful chunk: %s ago flags: ", fmt.Sprintf("%q: %s-%s", cn.PeerID, cn.Socket.LocalAddr(), cn.Socket.RemoteAddr()), cn.completedString(), cn.UsefulChunksReceived, cn.ChunksReceived, len(cn.Requests), len(cn.PeerRequests), time.Now().Sub(cn.lastMessageReceived).Seconds(), time.Now().Sub(cn.completedHandshake).Minutes(), time.Now().Sub(cn.lastUsefulChunkReceived)) c := func(b byte) { fmt.Fprintf(w, "%c", b) }